Turn 90 degrees when pressed and go back when pressed again

I was trying to make a servo turn 90 degrees when I pressed a button and then go back to start when I pressed it again. I¨m new to Arduino and programming so I didn't know exactly how to deal with variables. It goes to 0 when I start it and to 90 when I press the button, but nothing happens when I press it again.

#include <Servo.h>;
int pos = 0; // Start position
int laas = 0; // 0 = Gate down
 // pushbutton pin
 const int buttonPin = 8;
 // servo pin
 const int servoPin = 7;
 Servo servo;
//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
  servo.attach (servoPin);
  // Set up the pushbutton pins to be an input:
  pinMode(buttonPin, INPUT);
void loop()
 // local variable to hold the pushbutton states
  int buttonState;  
  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);
  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button
  if (buttonState == LOW) // If button is pressed
    if (laas = 1){
      laas = 0;
      servo.write(pos); // Go to start position
    if (laas = 0) {
      laas = 1;
      servo.write(90); // Go Down

Use state change detection (see examples, or use a library like Bounce2). Because you want to act when a button becomes pressed (single action) versus changing if a button is pressed (happens thousands of times per second for an Arduino).


if (laas = 1){

is a classic pitfall in C/C++. The single '=' is assignment, the doube '==' is a comparison of equality. What you are doing there is assigning the value 1 to the variable laas and then testing the result to see if it is true (non-zero) which it always will be.