Trouble creating a push button toggle switch

Hi,
I built a circuit meant to move a servo motor, from 0 degrees to 90 degrees by the push of a button.
My goal is that each time i push the button the servo will toggle between 0 and 90.
this is the code i wrote

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 
int button = 2;  // The button will be on Pin 7 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT); 
digitalWrite (button, LOW);
} 

void loop() 
{
  if( pos=90){
    if(digitalRead(button)== LOW){
       myservo.write(90); // stay at 90
        delay(50);
    }
  else if(digitalRead(button)== HIGH){
     for(pos=90; pos>0; pos-=5){
        myservo.write(pos); // go to 0
      }   
     delay(3000);
  }
  }
  
  else if(pos=0){
    if(digitalRead(button)== LOW){
      myservo.write(0); // stay at 0
        delay(50);
    }
  else if(digitalRead(button)== HIGH){
     for(pos=0; pos<90; pos+=5){
        myservo.write(pos); // go to 90
      }  
     delay(3000); 
  }
}
}

I added a large delay (I don’t mind the additional time) after each button HIGH to make sure pushing the button is included only in a single loop.

After uploading the following happens:

  • servo starts at 90
  • push button
  • servo goes to 0
  • 3 sec delay
  • servo goes back to 90

I don’t understand why it goes back to 90, since i defined that when the button is LOW and servo is at 0 it should stay at 0.

Any help is appreciated, thanks in Advance
Eran

  if ( pos = 90)

  else if (pos = 0)Fix these attempts at comparison then try the program again

UKHeliBob:

  if ( pos = 90)

  else if (pos = 0)Fix these attempts at comparison then try the program again

How do I fix the comparison attempt, what needs to be changed?

EranT:
How do I fix the comparison attempt, what needs to be changed?

assign:

=

compare:

==

Got it ! thank you both very much!