Please help with my code

Is it possible that when i press the button then the servo turns 0, when i press again the button it turns back
Here is the code:

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
#include <Servo.h>
Servo myservo;
// this constant won't change:
const int  buttonPin = 3;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  myservo.attach(9);
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin, HIGH);
    myservo.write(90);
    delay(1000);
    myservo.write(0);
    digitalWrite(ledPin, LOW);
  } else {
    digitalWrite(ledPin, LOW);
    myservo.write(0);
  }

}

My problem with the code is that it debounces and not doing the if i perss then turn this way but if i press again then it turns back

  if (buttonPushCounter % 2 == 0)
  {
    digitalWrite(ledPin, HIGH);
    myservo.write(90);
    delay(1000);
    myservo.write(0);
    digitalWrite(ledPin, LOW);
  }

Here you move the servo to 90, wait 1 second then move it to 0 every time test returns 0. What you need to do is to move it to 90 when the test is true and to 0 when it is false

Print the value of buttonPushCounter % 2 so that you can see what is going on

I think we should not use delay when using button since it makes Arduino missing some events. We can use a timestamp instead. See Blink Without Delay example

Your code can be simple like this

#include <ezButton.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
ezButton button(7);  // create Button object that attach to pin 7;

int angle = 0;

void setup() {
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()){
    if(angle == 0)
      angle = 90;
    else if(angle == 90)
      angle = 0;
  }

  myservo.write(angle); 
}

the above code does not include LED code and timestamp. It use this button library

If you don't want to use the library, you could pay attention to this post Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum

there is still debouncing and I have to press it 3 times to go back :frowning:

(UKHeliBob)

So i have to run the servo from a another power supply?

rego0116:
So i have to run the servo from a another power supply?

I think, If servo motor is light-weight servo motor (for example these motors), you can powers servo motor directly from Arduino for testing purposes only. However, for a long term use, you need to use the extra power supply for servo motor. See this port FAQ: How to use two power supplies. - Introductory Tutorials - Arduino Forum

rego0116:
there is still debouncing and I have to press it 3 times to go back :frowning:

(UKHeliBob)

What values did you see when you printed the value of buttonPushCounter % 2 before testing it ?

Nothing i found the problem

rego0116:
Nothing i found the problem

Please share what was wrong and how you solved the problem as it could help someone else in the future

I modified the code a litle bit and the servo is jumping back and forth
I dodnt understand why (probably beacuse of the fast changing angle variable)
Here is the code

#include <Button.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Button button(3);  // create Button object that attach to pin 7;

int angle = 0;


void setup() {
  button.setDebounceTime(1000); // set debounce time to 50 milliseconds
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()){
    Serial.println(".");
   tone(7,100,200);
   delay(150);
   Serial.println(".");
      tone(7,200,200);
   delay(150);
   Serial.println(".");
      tone(7,300,200);
   delay(150);
   Serial.println(".");
      tone(7,400,200);
   delay(150);
   Serial.println(".");
      tone(7,500,200);
   delay(150);
      Serial.println("Door ");
  if (angle == 0){ Serial.println("Unlocked");}
  if (angle == 90){ Serial.println("Locked");}

delay(1000);
for (int i = 0; i <= 100; i++) {
Serial.println(" ");
  }

 
   
    
    if(angle == 0)
      angle = 90;     
    else if(angle == 90)
      angle = 0;
      
  }
  


  myservo.write(angle);
}
Button button(3);  // create Button object that attach to pin 7;

Which pin is the button attached to and how is it wired ?

@rego0116, I recommend doing button with servo motor first, and then adding buzzer code later. Again, if you are using delay() function in your code. This may make Arduino miss some pressing events