HELP ME URGENT

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;
}
    delay(0);

Right there

  digitalWrite(ledPin, pos=180 oops

let me add a bit of a hint :slight_smile:

digitalWrite(ledPin, pos[color=red][b]=[/b][/color]180? HIGH:LOW);

To OP:

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here

Hi,
Welcome to the forum.

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.

Tom... :slight_smile:

It may well save memory, because the compiler can see that the conditional expression is always true :wink:

AWOL:
It may well save memory, because the compiler can see that the conditional expression is always true :wink:

:slight_smile:

"URGENT" for who?

gfvalvo:
"URGENT" for who?

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

AWOL:
It may well save memory, because the compiler can see that the conditional expression is always true :wink:

But not efficient for an OP who has problem reading code.... :slight_smile:

OP is probably waiting for the super efficient forum email notification system. :o :o :o :o

Tom... :slight_smile:

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

gfvalvo:
"URGENT" for who?