Servo Control

I am trying to get my arduino to control a micro servo with a button. I want it so that when I push the button the servo goes one way and when I press it again it goes the other way. I think that there may be something wrong with my code. Here is my code..

#include <Servo.h>

int switchPin = 8;
int servoPin = 7;
boolean lastButton = LOW;
boolean change = false;
Servo servo1;

void setup()
{
  
  pinMode(switchPin, INPUT);
  servo1.attach(servoPin);
}

void loop()
{
  
  if(digitalRead(switchPin) == HIGH && lastButton == LOW){
    servo1.writeMicroseconds(1000);
    delay(1000);
    change== true;
    lastButton = HIGH;
  }else if(digitalRead(switchPin) == HIGH && lastButton == LOW && change){
    servo1.writeMicroseconds(2000);
    delay(1000);
    lastButton = HIGH;
    change==false;
  }
  
}

currently the servo goes off when the arduino restarts and only goes once when I press the button then the entire system is unresponsive.

Have a look at this post.
http://forum.arduino.cc/index.php?topic=234577.new;topicseen#new
and this one,
http://forum.arduino.cc/index.php?topic=233619.15

Basic servo/button toggle code.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

mattleal96:
I am trying to get my arduino to control a micro servo with a button. I want it so that when I push the button the servo goes one way and when I press it again it goes the other way. I think that there may be something wrong with my code. Here is my code..

#include <Servo.h>

int switchPin = 8;
int servoPin = 7;
boolean lastButton = LOW;
boolean change = false;
Servo servo1;

void setup()
{
 
  pinMode(switchPin, INPUT);
  servo1.attach(servoPin);
}

void loop()
{
 
  if(digitalRead(switchPin) == HIGH && lastButton == LOW){
    servo1.writeMicroseconds(1000);
    delay(1000);
    change== true;
    lastButton = HIGH;
  }else if(digitalRead(switchPin) == HIGH && lastButton == LOW && change){
    servo1.writeMicroseconds(2000);
    delay(1000);
    lastButton = HIGH;
    change==false;
  }
 
}




currently the servo goes off when the arduino restarts and only goes once when I press the button then the entire system is unresponsive.

Under the If statement you have change==true the double equals doesn't set the variable to true, try a single equals sign, likewise under the else if statement you have change==false which I believe should be a single equals sign to set the variable to false.

The if else statement is dependant on the variable change but doesn't specify the condition of the variable, there should be a double equals and true or false.

I believe you need to be setting the lastbutton state to low in another if statement otherwise the lastbutton state is set to high after the first loop and nothing is done after that because the statements that control the servo are never true.

Thank You this fixed it and it works fine now, I can't believe I made such a rookie mistake. I have been programming with java for 2 years now.