servo if... else statement

Ok, so i had some help with adding a button to some servos for a robotic arm, however, lot of bouncing going on. The code goes through the loop, but when i press the button (sometimes due to bounce) it stops the motion of the servos. This is not what I want. I want a button, when pressed, to stat the program and stop at the end of the loop, and then wait til the button is pressed again. Here is the code:

#include <Servo.h>  // servo library

#define ButtonPin  4

unsigned long nowMillis=0;
unsigned long debounceTimer=0;

Servo servo1;  // servo control object
Servo servo2;




void setup()
{
  pinMode(ButtonPin,INPUT);
  
  servo1.attach(9);
  servo2.attach(10);
}


void loop()
{
  nowMillis=millis();
  if ((digitalRead(ButtonPin)==1) && ((nowMillis - debounceTimer) > 250))
    {
  int position;
  
  servo1.write(90);    

  delay(3000);        

  servo2.write(90);   

  delay(3000);
 
  servo2.write(0);   

  delay(3000); 
         

  servo1.write(180);     

  delay(3000);         
  
  servo2.write(90);    

  delay(3000);
 
  servo2.write(0);    

  delay(3000); 
  
   
    }
}

I have been going over my references, and i am thinking i need an else statement, but not sure. I really am new to the programming aspect. Any assistance would be great!

You need to create a flag variable. After the bouncing is done set the flag to true. When the flag is true, do whatever you want. Set the flag back to false so that it waits until the next button press.

Pseudo code:

loop()
if (debounce_done) {
   action_flag = true
}

if (action_flag==true) {
  // do whatever
  action_flag==false
}

James, the ide says that "debounce_done was not declared in scope. now I apologize if i did not use your code correctly. I have never seen action flag before, nor do i understand exactly how to weave this code into my existing one. Could you explain please?

Look at the state change detection example. It shows how to do something when the switch BECOMES pressed, rather than when the switch IS pressed (as your code does now).

Bstanko6:
James, the ide says that "debounce_done was not declared in scope.

“pseudo-code” isn’t real code. It is meant to give you an idea of how to write real code.

There’s nothing special about a “flag variable.” You just make another variable. You can even call it “flag.”

You can even call it "flag."

But, don't. Unless it really represents whether you've hoisted a flag, or not.

I figured it out thank you guys very much!