Problem with servo button debounce

Hi im having problem with button debounce here's my sample code.

if(buttonPress){                                   //if buttonPress is high do the sweep example.
    for(pos = 46; pos < 136; pos += 1)  
  {                                 
    myservo.write(pos);              
    delay(15);                     
  } 
  for(pos = 136; pos>=46; pos-=1)    
  {                                
    myservo.write(pos);             
    delay(15);                    
  } 
  }
else{                                                 //else when buttonPress is LOW go to angle 90
    myservo.write(90);

  }

Here's the problem when i first pressed the button the servo sweeps but when i press the button again it still sweeps. what am i doing wrong. but i tried to alter the code and tested if button is working.

if(buttonPress){              //if HIGH
   myservo.write(180)                  
  } 
else{                             //else if LOW
    myservo.write(0);

  }

this code work just fine.. when i press the button it goes to 180 and when button is pressed again it returns to 0 degrees. Hope someone can help me. Or is it because of the delay?.. if yes how can i modify it so when i press the button again it will go to 90 instantly without delay?. Hope someone can help me. thanks a lot.

Here's my variables.

const int buttonPin = 2;
int ledStatus = HIGH;
int lastButtonState = LOW;
int buttonPress = LOW;
int buttonState;
int readme;
long lastDebounceTime = 0;
long debounceDelay = 50;

thanks and more power.
boolean presses = false;

This:

int buttonPress = LOW;

and this:

if(buttonPress)

really don't go together. buttonPress is not a boolean, and shouldn't be used as if it was. Explicit checking is better.

If you are having problems with a switch it is almost always because it isn't wired correctly, so sometimes the pin it is connected to is floating.

You need to show a schematic for how the switch is connected and ALL of the code.

Thanks for the reply here's the complete code which im practicing with.

#include <Servo.h> 
//servo
Servo myservo;
int pos = 46;
int potPin1 = 4;  
//button
const int buttonPin = 2;
int ledStatus = HIGH;
int lastButtonState = LOW;
int buttonPress = LOW;
boolean Presses = false;
int buttonState;
int readme; 
long lastDebounceTime = 0; 
long debounceDelay = 50;   
//Dc motor

int transistorPin1 = 11;   

int potValue1 = 0;  

void setup() {    
  pinMode(buttonPin, INPUT);  
  pinMode(transistorPin1, OUTPUT);
  myservo.attach(9);
  //pinMode(transistorPin2, OUTPUT);
}
void loop() {  
  readme = digitalRead(buttonPin);
  potValue1 = analogRead(potPin1) / 4;  
  analogWrite(11, potValue1);


  if (readme != lastButtonState) {

    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
   
     buttonState = readme;
    
    if(Presses) {
       if(buttonState == LOW) {
         if(buttonPress == HIGH)
            buttonPress = LOW;
          else
            buttonPress = HIGH;    
       }
    }
    Presses = buttonState == HIGH;
  }

if(buttonPress){
 for(pos = 46; pos < 136; pos += 1)  
  {                                 
    myservo.write(pos);              
    delay(15);                     
  } 
  for(pos = 136; pos>=46; pos-=1)    
  {                                
    myservo.write(pos);             
    delay(15);                    
  }  
  
}
  else{
    myservo.write(90);
  }
 
  lastButtonState = readme;
}

Im controlling both a servo and a dc motor. its just that im having a problem with the servo the dc motor works well.. when the button is pressed the servo sweeps but when i press the button again it still sweeps. but when i modify the code just like this:

if(buttonPress){
    
    myservo.write(180);             
                       
  
}
  else{
    myservo.write(0);
  }

it works well. Hope you can help me with this one. thanks a lot. and btw the schem is just like in the debounce example its just that im using an external battery to supply the servo. the ground of the external battery is connected with the ground of the arduino and the 5v of arduino is neglected cause im using the positive voltage of the external battery.

when the button is pressed the servo sweeps but when i press the button again it still sweeps. but when i modify the code just like this...it works well

I don't understand what you mean by "when i press the button again it still sweeps". What do you want it to do when you press the switch again?

All i want to happen is when button is pushed the first time it will execute the sweep example and when the button i pushed again it will stop and go to angle 90(center).

just like this.

button pushed -> execute sweep
button pushed again -> go to angle 90 and stop
button pushed again -> execute sweep again
button pushed again -> got to angle 90 and stop again

it just loops out.. that's how i want my button to do. thanks for the reply..

Your code is very hard to read. Please put each { on a new line, and use Tools + Auto Format to correct the random indenting.

Then, add some comments. What the heck is that mess with Presses about?

Third, stop treating ints as bools. If you want a bool, use a bool. If you want an int, use an int. If you use an int in an if statement, it needs to be compared to something.

When you make these changes, post your code again.

#include <Servo.h> 

Servo myservo;           
int pos = 46;               
const int buttonPin = 2;    
int lastButtonState = LOW; 
int buttonPress = LOW;
boolean Presses = false;            //if button is pushed
int buttonState;
int readme; 
long lastDebounceTime = 0; 
long debounceDelay = 50;   

void setup() {    
  pinMode(buttonPin, INPUT);  
  myservo.attach(9);

}
void loop() {  
  readme = digitalRead(buttonPin);

  if (readme != lastButtonState) {

    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    buttonState = readme;

    if(Presses) {
      if(buttonState == LOW) {
        if(buttonPress == HIGH)
          buttonPress = LOW;
        else
          buttonPress = HIGH;    
      }
    }
    Presses = buttonState == HIGH;
  }

  if(buttonPress==HIGH){
    for(pos = 46; pos < 136; pos += 1)  
    {                                 
      myservo.write(pos);              
      delay(15);                     
    } 
    for(pos = 136; pos>=46; pos-=1)    
    {                                
      myservo.write(pos);             
      delay(15);                    
    }  

  }
  else{
    myservo.write(90);
  }

  lastButtonState = readme;
}

there I've remove the code for the dc motor i only put the code for the servo because it's the only problem im having. Presses variable is when button is pushed. Hope this is ok now.

Please put each { on a new line

void setup() {    
void loop() {  
  if (readme != lastButtonState) {
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if(Presses) {
      if(buttonState == LOW) {

Nice job.

Then, add some comments. What the heck is that mess with Presses about?


Well, that certainly clears up any misunderstand.

  readme = digitalRead(buttonPin);
  if (readme != lastButtonState) {

Wouldn't currButtonState make more sense than readme?

  if (readme != lastButtonState)
  {
    lastDebounceTime = millis();
  }

If the current switch state is not the same as the previous switch state, you want to do more than record the time. As a matter of fact, you want to to nothing if the current switch state is the same as last time.

The time that the transition occurs should be saved in an appropriately named variable, like transitionTime.

You want to determine if the transition that did occur was a transition to pressed. That mess with Presses may be trying to do that, but it is hard to follow.

Presses implies, to me, that it is a count. Pressed implies a state.

Is there really anything to do it the switch was released? If not, the do-something-when-the-switch-is-pressed goes in the body of the if statement, rather than diddling with boolean variables.

Presses, buttonState, readme, and buttonPress all seem to be trying to do the same thing. That's 3 variables too many.

Go back and read what I wrote about comments. Dump everything you have in loop now. Fill loop() with nothing but comments.

void loop()
{
   // See if the switch is pressed
   //   If it is, see if it wasn't before
   //     A transition occurred, see how long it has been since the last transition
   //       If this transition is long enough after the previous one
   //         Wave the servo around
   //         Save current transition time as previous transition time
   //       End If
   // Save current switch state as previous switch state
}

Think about whether there are other things that need to be considered/done. If so, add another comment.

Only then should you go back and add code. Leave the comments in place. Add code after each one.

That way, if you have difficulties, and need others to look at your code, your thought processes are evident. We can help you see, then, whether the problem is with your thought processes or with how you created/manipulated the code to implement those thoughts.

Sorry for my long delay to reply been busy doing the mechanical part of our project.

Here's the code with the comments.

void loop()
{
  readme = digitalRead(buttonPin)     // See if the switch is pressed
  if (readme != lastButtonState) {    //   If it is, see if it wasn't before
  lastDebounceTime = millis();        //     A transition occurred, see how long it has been since the last transition
  }  
  if ((millis() - lastDebounceTime) > debounceDelay) {      //   If this transition is long enough after the previous one

    buttonState = readme;                                 // Save current buttonState to buttonState variable e.g HIG or LOW

    if(Presses) {                                        // CHeck if user is pressing e.g from HIGH to LOW or LOW to HIGH
      if(buttonState == LOW) {                           
        if(buttonPress == HIGH)                          
          buttonPress = LOW;
        else
          buttonPress = HIGH;    
      }
    }
    Presses = buttonState == HIGH;                                                      
  }
     
    
}  
if(buttonPress==HIGH){                          // Wave the Servo around if Button is HIgh making the servo do the sweep example.
    for(pos = 46; pos < 136; pos += 1)  
    {                                 
      myservo.write(pos);              
      delay(15);                     
    } 
    for(pos = 136; pos>=46; pos-=1)    
    {                                
      myservo.write(pos);             
      delay(15);                    
    }  

  }
  else{                                         // If it goes from HIGH to LOW stop the servo and position it to angle 90 making the servo stop the sweep example even if it isn't complete in executing it. 
 myservo.write(90);      
  }

  lastButtonState = readme;                     // Save current switch state as previous switch state  
}                                               // End If

Thanks for helping me really appreciate it. :blush:

Go back and read what I wrote about comments. Dump everything you have in loop now. Fill loop() with nothing but comments.

Write the comments first and then add the code. This is the most foolproof technique and it applies to any programming language.

Don

Here's the code with the comments.

I still can't make heads or tails of that mess with Presses. Either properly comment that to explain what you think every single line is doing, or get rid of it. I am convinced that it is far more complicated than it needs to be, and it the root of your problem.

When we say write the comments first, that means that they don't get tagged on after some code. Those comments are nearly useless.