Servo myservo;const int leftbutton = 2; //variable to represent the pin that the left button is onconst int rightbutton = 4; //variable to represent the pin that the right button is onint casenum = 0; //case number to tell the servo which case to runint pos = 0; //positionint delaytime = 20; // increasing this slows down the servo movementunsigned long LastTimeButtonWasPressed; // last time updatelong interval = 3000UL; // interval at which to do something (milliseconds)

void setup(){

myservo.attach(9); // attaches the servo on pin 9 to the servo object

pinMode(leftbutton, INPUT); // the buttons are the inputs pinMode(rightbutton, INPUT);

}

void loop(){ unsigned long StartVoidLoopTime = millis(); if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum } if (digitalRead(rightbutton)) { casenum = 3; }

myservo.write(pos); // Move to next position delay(delaytime); // Short pause to allow it to move if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum break; } if (digitalRead(rightbutton)) { casenum = 3; break; } }

casenum = 1; //if none of the buttons are pressed then it goes to pan left break;

case 1: for(pos = pos; pos >= 0; pos -= 1) // pan left {

myservo.write(pos); // Move to next position delay(delaytime); // Short pause to allow it to move if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum break; } if (digitalRead(rightbutton)) { casenum = 3; break; }

} casenum = 0; break;

case 2: while (digitalRead(leftbutton)) //while the button is being pressed { myservo.write(pos); //rotates servo to its current positon delay(delaytime); pos-=1; //position becomes one less LastTimeButtonWasPressed = millis(); }

You're clinging to hard to Blink Without Delay example here. LastTimeButtonWasPressed shouldn't be set while the button is not pressed (If it was pressed, it would still be in the while loop).

You're problem is still with your understanding of the break statements. Unless you know exactly what you are doing, you should be using an UNCONDITIONAL break at the end of every case statement. Since none of your break conditions are met in case 2 or case 3, as soon as the left button is released, it will fall through all the way to the default section, which sets the casenum to 0.

Okay,its making more sense. So because my if statement for the "delay" is not being accomplished in the first pass it is jumping out and going to the default case? How do I make it reloop to accomplish that if statement?

Okay,its making more sense. So because my if statement for the "delay" is not being accomplished in the first pass it is jumping out and going to the default case?

Jumping out isn't the right phrase. More like it's failling through. A break statement says "I'm done executing code in this switch statement, proceed to the end of the switch statement, skipping all the code between here and there"

Quote

How do I make it reloop to accomplish that if statement?

It already loops by virtue of being within the loop() function. What you need it to do is only execute the code within the case 2: section of your code. To stop it from doing that, you need an unconditional break.

So, are you saying that once it gets to case 2 I should only do that code. or case 2 should be the only code. When in case 2, the only code in the switch statement that should run is the code in case 2. Any code outside the switch statement will still run without issue.

Quote

And then how would I make it keep running case 2?

I already answered that:

Quote

It already loops by virtue of being within the loop() function.

Quote

And I knew what the delete key was thank you haha. Are you saying get rid of the conditional breaks within cases 2 & 3?

Servo myservo;const int leftbutton = 2; //variable to represent the pin that the left button is onconst int rightbutton = 4; //variable to represent the pin that the right button is onint casenum = 0; //case number to tell the servo which case to runint pos = 0; //positionint delaytime = 20; // increasing this slows down the servo movementunsigned long LastTimeButtonWasPressed; // last time updatelong interval = 3000UL; // interval at which to do something (milliseconds)

void setup(){

myservo.attach(9); // attaches the servo on pin 9 to the servo object

pinMode(leftbutton, INPUT); // the buttons are the inputs pinMode(rightbutton, INPUT);

}

void loop(){ unsigned long StartVoidLoopTime = millis(); if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum } if (digitalRead(rightbutton)) { casenum = 3; }

myservo.write(pos); // Move to next position delay(delaytime); // Short pause to allow it to move if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum break; } if (digitalRead(rightbutton)) { casenum = 3; break; } }

casenum = 1; //if none of the buttons are pressed then it goes to pan left break;

case 1: for(pos = pos; pos >= 0; pos -= 1) // pan left {

myservo.write(pos); // Move to next position delay(delaytime); // Short pause to allow it to move if (digitalRead(leftbutton)) //checks to see if each button is being pressed { casenum = 2; //if so it switches the casenum break; } if (digitalRead(rightbutton)) { casenum = 3; break; }

} casenum = 0; break;

case 2: while (digitalRead(leftbutton)) //while the button is being pressed { myservo.write(pos); //rotates servo to its current positon delay(delaytime); pos-=1; //position becomes one less LastTimeButtonWasPressed = millis(); } myservo.write(pos); if(StartVoidLoopTime - LastTimeButtonWasPressed >= interval) //if the total time - the start time is greater than 3 sec { //casenum = 1; //this is where the code goes wrong, it seems like it reads this if statement //and immedietly say thinks its true //LastTimeButtonWasPressed = millis(); //LastTimeButtonWasPressed = StartVoidLoopTime; break; }

case 3: while (digitalRead(4)) { myservo.write(pos); delay(delaytime); pos +=1; LastTimeButtonWasPressed = millis(); } myservo.write(pos); if (StartVoidLoopTime - LastTimeButtonWasPressed >= interval); //if the total time - the start time is greater than 3 sec { casenum = 1; //this is where the code goes wrong, it seems like it reads this if statement //and immedietly say thinks its true