I am having troubles with programming a line tracking robot (with motors and using the Arduino Uno) and using a switch statement to declare the different movements for the motors.
So far, I have:
void loop() {
int sensorValueright = analogRead(A0);
int sensorValuecentre = analogRead(A1);
int sensorValueleft = analogRead(A2);
switch (direction1) {
case "right":
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 60); //Motor A at quarter speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 125); //Motor B at half speed
delay(1000);
break;
case "centre":
digitalWrite(12, HIGH); //Forward A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 100); //Motor A = Motor B speed
digitalWrite(13, HIGH); //Forward B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 100); //Motor A = Motor B speed
delay(500);
break;
case "left":
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 125); //Motor A at Half Speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 60); //Motor B at Quarter Speed
delay(1000);
break;
}
if (sensorValuecentre < 1){
direction1 == "centre"
}
else if (sensorValueright < 1){
direction1 == "right"
}
else if (sensorValueleft < 1){
direction1 == "left"
}
else{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 50); //Motor A at low speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 50); //Motor B at low speed
delay(500);
}
delay(1);
}
But I am getting the following error while compiling:
line_tracker_test_switch.ino: In function 'void loop()':
line_tracker_test_switch.ino:20:9: error: 'direction1' was not declared in this scope
line_tracker_test_switch.ino:60:3: error: expected ';' before '}' token
line_tracker_test_switch.ino:64:3: error: expected ';' before '}' token
line_tracker_test_switch.ino:68:3: error: expected ';' before '}' token
Error compiling.
Any help would be greatly appreciated!
"direction1" has not been declared anywhere in the code you pasted. Somewhere there needs to be a line of the form
type direction1;for examplechar *direction1;orint direction1;to tell the compiler what direction1 is.The other 3 errors indicate those lines are missing semicolons at the end.
direction1 == "left";This is also unlikely to be what you want.==is the equality operator.=is the assignment operator. This is also a strange way to use these operators because strings work differently from primitive types such asintandfloat. You cannot compare or assign them directly. You will be accessing the pointer value instead.Using strings with switch statements is not allowed in standard C.
Create integer constants instead
enum {LEFT, RIGHT, CENTRE};and replace "left" with LEFT and so on.