Switch-Case code not working

Hi there,

I am currently working on a piece of code to program an arduino redboard to do a number of thing using cases.

Somehow I keep getting the code mixed up so could anyone have a look and tell me where things go wrong?

Many thanks

The assignment:

State Behaviour
1 Rest: Nothing happens, LED is off and the servo is at 30 degrees
2 Step: Servo steps between 30 and 60 degrees
3 Follow: Servo follows potentiometer
4 Blink: LED toggles on/off once a second
5 Switch: LED is on when the switch is on

#include <Servo.h>
Servo myservo; // create servo object to control our servo
int state = 0;
int pos = 30;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable that reads the value from the analog pin
int buttonState = 0; // variable for reading the pushbutton status
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin, dependent on type of board

void setup() {
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 3 to the servo object
pinMode(LED_BUILTIN, OUTPUT);
// set the LED pin as output:
pinMode(ledPin, OUTPUT);
// set the pushbutton pin as input:
pinMode(buttonPin, INPUT);
}

void loop() {
char L = Serial.read();
switch (state) {
case 1:
{ /* myservo.write(30); // tell servo to go to 30 degrees
delay(10); */ // waits to make sure the transition takes 1 second
if (char L == ‘S’|| char L == ‘s’){
state = 2;
}
if (char L == ‘B’|| char L == ‘b’) {
state = 4;
}
}
break;

case 2:
{ /* for (pos = 30; pos <= 60; pos += 1) { // goes from 30 degrees to 60 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to 30 degrees
delay(50/3); // waits to make sure the transition takes 1 second
}
for (pos = 60; pos >= 30; pos -= 1) { // servo goes from 60 degrees to 30 degrees
myservo.write(30); // tell servo to go to 30 degrees
delay(50/3); // waits to make sure the transition takes 1 second
} */
if (char == ‘F’|| char == ‘f’) {
state = 3;
}
if (char == ‘R’|| char == ‘r’) {
state = 1;
}
}
break;

case 3:
{ /*val = analogRead(potpin); // reads the value of the potentiometer
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(10); */ // waits for the servo to get there
if (char == ‘S’|| char == ‘s’) {
state = 2;
}
}
break;

case 4:
{ /digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
/
if (char == ‘W’|| char == ‘w’) {
state = 5;
}
if (char == ‘R’|| char == ‘r’) {
state = 1;
}
}
break;

case 5:
{ /*// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
// digitalWrite(ledPin, LOW);
} */
if (char == ‘R’||char == ‘r’) {
state = 1;
}
}
break;
}
}

Final_Assignment.ino (3.09 KB)

Not working, or not compiling?

The latter seems more likely to meif (char L == 'S'|| char L == 's'){

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

You set state to 0 in setup()
In loop() you do switch (state)
You have no zero or default case, so which code will execute ?

Interesting: OP must be on the same course as this guy. Exact same problem statement. Maybe they should collaborate?

As AWOL pointed out, this statement:

  • if (char L == 'S'|| char L == 's'){*
  • state = 2;*
  • }*

isn't right. The char in the expression above is a keyword that is used as a type specifier, so you shouldn't use it as above, especially since you defined L earlier:

*char L = Serial.read(); *

So, you need to change it and the other similar expressions to:

  • if (L == 'S'|| L == 's'){*
  • state = 2;*
  • }*

If you make the following change:

  • char L = toupper(Serial.read());*

the statement now assigns L with upper case letters, which would allow you to simplify the subsequent statements to:

  • if (L == 'S'){ // Only need to check on upper case now...*
  • state = 2;*
  • }*

You do not need braces for the case statements. This syntax is standard:

case 1:
_ /* myservo.write(30); // tell servo to go to 30 degrees_
_ delay(10); */ // waits to make sure the transition takes 1 second_

  • if (L == 'S'){*
  • state = 2;*
  • } else { // Add the else*
  • if (L == 'B') {*
  • state = 4;*
  • }*
  • }*
  • break;*

Note the unneeded braces are removed. I did add an else clause because if L does equal 'S', there's no need to check it against 'B'.

Finally, use Ctrl-T in the IDE before posting your code using code tags. It will reformat your code to a more standard style, which makes it easier for us to read.

I wonder if this thread's OP or his presumed classmate have read what forum member Nick Gammon has to say on state machines?