LED's won't do what I tell them to do

So I'm trying to make a code that does this:
When a button is pushed:
move servo to 170, turn LED on and LED2 off

When a button is released:
move servo to 10, turn LED off and LED2 on

The servo works, but LED is on constantly and LED2 doesn't turn on at all
I know it's not the wiring because a simpler code turns the LED when the button is pushed (although the wiring for LED2 might be a little bit off, but I don't think so).

Here's the code:

#include <Servo.h>

Servo myservo;

const int BUTTON = 7;
const int LED = 13;
const int LED2 = 2;
int pos = 0;
int val = 0;

void setup()
{
myservo.attach(9);
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
}

void loop () {
val = digitalRead(BUTTON);

if (val == HIGH) {
myservo.write (pos = 170);
digitalWrite(LED, HIGH);
digitalWrite(LED2, LOW);
} else {
myservo.write (pos = 10);
digitalWrite(LED, LOW);
digitalWrite(LED, HIGH);
}
}

On a slightly separate note, if anyone could tell me how to stop the servo from spazzing out when it gets to 170, that would be great. Thanks!

    myservo.write (pos = 170);

should be
    myservo.write(170);and

    myservo.write (pos = 10);

should be

    myservo.write(10);
    digitalWrite(LED, LOW);
    digitalWrite(LED, HIGH);

one of these should be LED2

How are the buttons wired ? Any pullup/down resistors, or are the input pins floating when the buttons are not pressed ?

Wow! Thanks so much! It was that missing 2 that broke things, and now everything is working! The (pos = 170) and (pos = 10) were working, but I like the simplicity of your version better. Thanks again!

And lastly, the little servo problem I posted at the end. When it gets to 170, it starts twitching and making clicking noises. It's as if it's trying to go farther but it can't. Know how I can solve this? Thanks in advance!

Have you tried changing to 'my' way of doing myservo.write(), which is the correct way of doing it, by the way.

I did indeed. But the problem persists!

Try this simple program

#include <Servo.h>

Servo myservo;

void setup() 
{ 
  myservo.attach(9);
  myservo.write(170);
} 

void loop () {}

Does the servo behave as expected or does it still jitter ?

Maybe the servo doesn't go to 170.
Try 160 or 150, or use the writeMicroseconds method to test the servo limits.

"LED's won't do what I tell them to do"

Have you tried another language other than English? Where were the LED's manufactured? They might be Chinese, Try Chinese :smiley:

The biter is bit :slight_smile:

Have you tried another language other than English? Where were the LED's manufactured? They might be Chinese, Try Chinese

LED's

Wrong

They might be Chinese, Try Chinese

Wrong