|
本帖最后由 vn504856 于 2013-6-27 23:24 编辑
我是来自台湾的Arduino的新手
用词可能会和大家有些差异
请多包涵
-------------------------------------------------------------------------------------------------------------------------------------------------------
我照网路教学
做了一个用蓝芽控制直流马达正反转的电路
设定为转动3秒后就停止
我想在马达停止后
加入红外线感测功能
code该怎么修改
控制马达的code
int enablePin = 4; // pin 1 on L293D IC
int motorPin1 = 5; // pin 2 on L293D IC
int motorPin2 = 6; // pin 7 on L293D IC
int state;
int flag=0; //makes sure that the serial only prints once the state
void setup() {
// sets the pins as outputs:
pinMode(enablePin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '1' the motor will turn right
if (state == '1') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
if(flag == 0){
Serial.println("Motor: right");
flag=1;
delay(3000);
}
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
// if the state is '2' the motor will turn left
else if (state == '2') {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: left");
flag=1;
delay(3000);
}
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
}
|
|