I don't know why but for some reason my code does not loop. so basically, I set up a servo to turn 90 degrees when a button is pressed and to turn back into its original position if I click it once more. however, for some strange reason, I can make it turn 90 degrees and back but then I have to press the restart button to make it work again. I just want to be able to make the servo go back and forth everyclick
here is my code:
#include <Servo.h>
Servo myservo;
const int buttonPin = 2;
const int outputPin = 9;
int pos = 0;
int ledPin = 13;
int oldButtonState;
void setup()
{
pinMode(ledPin, OUTPUT);
myservo.attach(outputPin);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
if (oldButtonState == HIGH)
{
myservo.write(pos);
digitalWrite(ledPin, pos=180? HIGH:LOW);
delay(0);
pos = (90-pos);
}
}
oldButtonState = buttonState;
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
As indicated in this bit of code.
digitalWrite(ledPin, pos=180? HIGH:LOW);
Writing in shorthand doesn't save any code memory.
You need to have a variable that states if you are in 0 or 90 deg mode, and use the change of state of the button to toggle it from one to the other.
THEN
Use an if statement to control the 0 or 90 deg servo command.
Exactly. I'm still for whoever said any thread with "urgent" in the title should be stickied to the top of page 2. If it's in all caps like this ___ then maybe page 3
OP came, read the first two answers (see edit time, OP did modify the initial post to use code tags) and got the answer - probably won’t come back unless something else URGENT happens...