現在出現新的問題了 當我藍芽連線的時候2個伺服馬達會自己動作 要按下上升or下降的按鈕後才會停止
請求1. 能否請教大改要如何改??
請求2. 我現在加了避障模組上去但我的模組不會動作 下面是現在的程式
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
SoftwareSerial BT(11,12);
const int leftMotorIn1 = 2;
const int leftMotorIn2 = 3;
const int rightMotorIn3 = 4;
const int rightMotorIn4 = 5;
char command;
int ang1 = 80;
int ang2 = 80;
int IR_Objects = 8;
int IR = 0;
void setup()
{
pinMode(IR_Objects,INPUT);
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
Serial.begin(38400);
BT.begin(38400);
servo1.attach(6);
servo2.attach(7);
servo1.write(80);
servo2.write(80);
}
void loop()
{
IR=digitalRead(IR_Objects);
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
delay(IR);
if(BT.available() > 0)
{
command = BT.read();
Serial.println(command);
switch (command) {
case 'A':
backward();
break;
case 'B':
forward();
break;
case 'C':
right();
break;
case 'D':
left();
break;
case 'S':
motorstop();
break;
case 'E':
ang1 = ang1 + 3;
if (ang1 > 180) ang1 = 180;
ang2 = ang2 - 3;
if (ang2 < 0) ang2 = 0;
servo1.write(ang1);
servo2.write(ang2);
delay(40);
break;
case 'F':
ang1 = ang1 - 3;
if (ang1 < 0) ang1 = 0;
ang2 = ang2 + 3;
if (ang2 > 180) ang2 = 180;
servo1.write(ang1);
servo2.write(ang2);
delay(40);
break;
case 'G':
servo1.write(80);
servo2.write(80);
break;
}
}
}
void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}
void forward()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
}
void backward()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
}
void right()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
}
void left()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
} |