疯狂老鼠
Language | English |
---|
目的拍打老鼠的身体就会向前狂奔一段距离。 原理利用震动传感器检测老鼠被拍打,然后让电机带动老鼠向前运行一段距离。 设备
准备
代码
#define motor_pinB 6
#define motor_pinA 8
//#define servo_pinB 5
//#define servo_pinA 7
const int vibrationPin = 10; // the number of the pushbutton pin
int vibrationState = 0;
void setup() {
Serial.begin(9600);
pinMode(vibrationPin, INPUT);
pinMode(motor_pinB, OUTPUT);
pinMode(motor_pinA, OUTPUT);
}
void loop() {
vibrationState = digitalRead(vibrationPin);
if (vibrationState == HIGH) {
Serial.println("ONONONON");
fullForward();
} else {
stop();
}
}
//stop
void stop() {
digitalWrite(motor_pinB, LOW);
digitalWrite(motor_pinA, LOW);
//delay(2000);
}
//full forward
void fullForward() {
//digitalWrite(motor_pinB, HIGH);
analogWrite(motor_pinB,255);//0-255
digitalWrite(motor_pinA, LOW);
delay(3000);
}
软件调试
#define motor_pinB 6 //电机1
#define motor_pinA 8 //电机1
//#define servo_pinB 5 //电机2
//#define servo_pinA 7 //电机2
const int vibrationPin = 10; // 震动传感器
void fullForward() {
//digitalWrite(motor_pinB, HIGH);
analogWrite(motor_pinB,255);//0-255
digitalWrite(motor_pinA, LOW);
delay(3000);
}
搭建硬件
结果拍打老鼠的身体,震动传感器检测到震动,老鼠向前运行3秒钟。
|