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..
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;
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.