请有人帮忙,我不知道为什么我得到这些错误代码 该代码用于控制arduino汽车,其方向取决于安装在其上的光阻接收最多的光线
error: a function-definition is not allowed here before '{' token
void stop_all(){
^
对于C ++ / arduino非常新,请帮助
//motor A---left
int ENA = 5;
int IN1 = 6;
int IN2 = 7;
//motor B---right
int ENB = 10;
int IN3 = 8;
int IN4 = 9;
//setup motorspeed
int motorspeeda = 100;
int motorspeedb = 140;// robot was veering slightly to the left,, increasing left motor speed makes it drive straight
//define light resistor pins
int resistor1 = A0;
int resistor2 = A2;
int resistor3 = A4;
//largest resistor constantly changes but needs to be declared outside of the if statements
int largestResistor = 0;
void setup() {
//initialise pins as outputs
//pins defualt to input so light resistors don't need to be changed
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// turn serial port on
Serial.begin(9600);
//read resistor values from pins
int res1 = analogRead(resistor1);
int res2 = analogRead(resistor2);
int res3 = analogRead(resistor3);
//function for robot to stop
void stop_all(){
analogWrite(ENA,LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN1, LOW);
analogWrite(ENB,LOW);
digitalWrite(IN3, LOW );
digitalWrite(IN4, LOW);
}
//function for robot to go forward
void forward(){
analogWrite(ENA, motorspeeda);
digitalWrite(IN2, LOW);
digitalWrite(IN1, HIGH);
analogWrite(ENB, motorspeedb);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
//function for robot to go backwards
void backwards(){
analogWrite(ENA,motorspeeda);
digitalWrite(IN2, HIGH);
digitalWrite(IN1, LOW);
analogWrite(ENB,motorspeedb);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
//function for robot to go right
void turnRight(){
if(millis() < 600){
analogWrite(ENA,motorspeeda);
digitalWrite(IN2, LOW);
digitalWrite(IN1, HIGH);
analogWrite(ENB,motorspeedb);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if(millis() < 1500){
forward();
}
else{
stop_all();
}
}
//function for robot to go left
void turnLeft(){
if(millis() < 600){
analogWrite(ENA,motorspeeda);
digitalWrite(IN2, LOW);
digitalWrite(IN1, LOW);
analogWrite(ENB,motorspeedb);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if(millis() < 1500){
forward();
}
else{
stop_all();
}
}
}
void loop(){
if(millis() < 30000){ //if less than 30 secs elapsed run code,, otherwise run stop function
turnLeft();
}
else{stop_all();}
}
答案 0 :(得分:1)
您正在尝试在函数内部定义函数。
你不能那样做。
停止这样做。
改为在名称空间范围内定义函数。
如果是错误的原因是由于缺少}
,那是因为您的缩进很糟糕。
正确缩进代码,以便您可以发现它。
答案 1 :(得分:1)
您只是错过了}来关闭setup()函数。