Hey, so I a Zumo 32u4 and on button press I want to make sure that my for loop does not start at a value like 0 and goes to 180 for my servo. How would I save that value and use it in next button press for loop rather than make my servo start at something I declared. Reference my code:
void loop()
{
if (buttonA.isPressed())
{
// Whenever the button is pressed, turn on the yellow LED.
for (int angle = 0; angle <= 90; angle+=10) {
Myservo.write(angle);
delay (50);
}
ledGreen(1);
}
if (buttonB.isPressed())
{
for (int angle = 0; angle <= 180; angle+=10)
{
Myservo.write(180);
delay(50);
}
ledYellow(1);
}
if (buttonC.isPressed())
{
for (int angle = 180; angle <= 0; angle-=10)
{
Myservo.write(0);
delay(50);
}
delay(30);
ledRed(1);
}
}
You declared three separate int angle variables, one for each of your for loops.
If you want angle to persist between loops, you need to declare it in a wider scope or more persistent, such as within loop, within loop as static int angle, or globally.
And then instead of reassigning it in the initial ardument of the for loops, you could use the last value:
int angle = 42;
...
for ( ; angle <= 90; angle+=10){...}
...
for ( ; angle <= 180; angle+=10){...}
...
for ( ; angle <= 0; angle-=10){...}
...