Trouble with switch case not advancing

Hello all,

I am having trouble getting the "cases" to advance on a project I am working on.
Basically I want to advance the "delay" time each time I press a button.

It seems stuck on the first case no matter how many time I press the button. I am using tinkercad simulator. Could it be a problem with the simulator? I adapted my code from a youtube video that used switch cases to use a push button to advance between 3 led lights, and it compiles fine, just dosent execute in tinkercad like it should.

Here is the code:

#include <Servo.h>

// constants won't change

const int SERVO_PIN_A = 9;
const int SERVO_PIN_B = 10; 
const int SERVO_PIN_C = 11;
const int SERVO_PIN_D = 3;
const int button = 5;
const int angleA = 0;         
const int angleB = 90;
const int time1 = 500;
const int time2 = 3000;
const int time3 = 6000;
const int time4 = 9000;



Servo servo_A; // create servo object to control a servo
Servo servo_B;
Servo servo_C;
Servo servo_D;

// variables will change:
int state = 0;
int old = 0;
int buttonPoll = 0;

void setup() {
 
 
  pinMode(button,INPUT);
  
  servo_A.attach(SERVO_PIN_A);          
  servo_A.write(angleA);
  
  servo_B.attach(SERVO_PIN_B);           
  servo_B.write(angleA);

  servo_C.attach(SERVO_PIN_C);           
  servo_C.write(angleA);
  
  servo_D.attach(SERVO_PIN_D);           
  servo_D.write(angleA);
  

}


void loop() {
  
  buttonPoll = digitalRead(button);
  if(buttonPoll == 1){
    delay(100);
    buttonPoll = digitalRead(button);
    if(buttonPoll == 0){
    	state = old + 1;
}}
 else{
delay(100);
  }
    
    switch (state) {
    
    case 1:

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time1);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time1);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time1);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time1);
    
    old = state;
    break;
    
    
    case 2:

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time2);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time2);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time2);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time2);
    
    old = state;
    break;
    
    case 3:

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time3);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time3);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time3);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time3);
    
    old = state;
    break;
    
    
    case 4:

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time4);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time4);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time4);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time4);
    
    old = state;
    break;
    
   default:

 	servo_A.write(angleA);
 	servo_B.write(angleA);
 	servo_C.write(angleA);
    servo_D.write(angleA);
 
    old = 0;
    break;
    }
}

I would also like to make an additional "case" in which the "delay time" is random within a range (for example random delay between 1 min and 5 min).
Any guidance would be much appreciated!

Thanks!

James

Looks like you have to release the button in 1/10 of a second or it won't count it.

state should be limited to the switch values.

When you wire the servos they need their own dedicated 6v power supply.

I tried changing the delay time for the button. I couldnt get it to work. In the video they guy said that that part of the code was for "debouncing". I tried a different route and used a pot instead of a button and added leds to let me know what "case" I was on.

here is the code if anyone comes across needing something like what I have described. I also was able to add a case with random delays.

Thanks


// constants won't change

const int SERVO_PIN_A = 9;
const int SERVO_PIN_B = 10; 
const int SERVO_PIN_C = 11;
const int SERVO_PIN_D = 3;
const int button = 5;
const int angleA = 0;         
const int angleB = 90;
const int time1 = 1000;
const int time2 = 2000;
const int time3 = 3000;
const int min = 0;
const int max = 1023;
const int ledR = 0;
const int ledY = 1;
const int ledG = 2;
const int ledW = 4;



Servo servo_A; // create servo object to control a servo
Servo servo_B;
Servo servo_C;
Servo servo_D;



void setup() {
 
  servo_A.attach(SERVO_PIN_A);           // attaches the servo on pin 9 to the servo object
  servo_B.attach(SERVO_PIN_B);           // attaches the servo on pin 9 to the servo object
  servo_C.attach(SERVO_PIN_C);           // attaches the servo on pin 9 to the servo object
  servo_D.attach(SERVO_PIN_D);           // attaches the servo on pin 9 to the servo object
  
  pinMode(ledR, OUTPUT);
  pinMode(ledY, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(ledW, OUTPUT);
  
  randomSeed(analogRead(0));// seads ranodm number from analog pin from 0 to 1023)
  
}


void loop() {
  
  int sensorReading = analogRead(A0); //maps pin to pot
  int range = map(sensorReading, min, max, 0, 4); //maps pot into sections
  
 
 
    
    switch (range) {
    
    case 0:
      
    digitalWrite(ledG, HIGH);
	digitalWrite(ledY, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
      
      
   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time1);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time1);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time1);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time1);
    
    
    break;
    
    
    case 1:
      
    digitalWrite(ledG, LOW);
	digitalWrite(ledY, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW); 

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time2);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time2);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time2);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time2);
    
    
    break;
    
    case 2:
      
    digitalWrite(ledG, LOW);
	digitalWrite(ledY, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time3);
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(time3);
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(time3);

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(time3);
    
    
    break;
    

    case 3:
      
    digitalWrite(ledG, LOW);
	digitalWrite(ledY, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);

   	servo_A.write(angleB);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(random(100, 20000)); //random between min and max milliseconds
  
  	servo_A.write(angleA);
    servo_B.write(angleB);
    servo_C.write(angleA);
    servo_D.write(angleA);
 	delay(random(100, 2000));
 
	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleB);
    servo_D.write(angleA);
 	delay(random(100, 2000));

 	servo_A.write(angleA);
    servo_B.write(angleA);
    servo_C.write(angleA);
    servo_D.write(angleB);
	delay(random(100, 2000));
    
    
    break;

    }
}

If you’re interested, see if you can rework this project to implement the delays with millis.

It will teach you a lot, and ultimately answer the question in your next topic.

1 Like

I'm not a fan of that button code, but other are probably not a fan of my methods either.

I built your button and tried a different approach to the button code.

const byte BUTTON_PIN = 5;
long count = 0;
byte last_pos = LOW;

void setup() {
  Serial.begin(57600);
  pinMode(BUTTON_PIN, INPUT); 
}

void count_the_thing() {
  if (last_pos == LOW && digitalRead(BUTTON_PIN) == HIGH) {
    last_pos = HIGH;
    count++;
  } else {
    if (digitalRead(BUTTON_PIN) == LOW) {
      last_pos = LOW;
    }
  }
  Serial.println(count);
}

void loop() {
  count_the_thing();  // This is so you can switch to millis() timing later
  delay(100); // This needs to go away because it stops your Arduino, learn use millis()
}

This code increments the count variable only once per button press, no matter how long it's held down. It also does not need any debounce, no matter how fast you check it in your sketch.

Also, as others have mentioned, powering those servos off the board Will Not Work. You must fix this.

And I tend to run my buttons to ground and use the internal pullup resistor. Seems to me that it was made to work that way. Others with more experience will correct me if I'm mistaken.

Thanks for the input! I’ll give it a try tomorrow, as for the servos I will add separate power. One question I do have about that is can I add the power (buck converter 3amp) to the shield I will make for it and power the board through the VIN pin on that circuit, or does it need to be two completely separate power inputs?

Your servos are probably 6v maximum.

Vin needs to be 7 to 9v for operation.

That question is better expressed with a drawing so everyone is on the exact same page, and that's probably a question for a more qualified user.
Draw a diagram and see what people say before you hook it up, but other than the power wiring and working in the new button code you should be wrapping up this problem tomorrow sometime, hopefully.

Unless you need parts, we always somehow need more parts.