weird behaviour

Hi there. I want to have a led that lights on and off using a pushbutton. my code is this

int led=2;
int button=9;
int val;
int val2;
int buttonState;
boolean armed=false;
void setup(){
  
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  pinMode(button,INPUT);
  digitalWrite(led,HIGH); 
  delay(100);
  digitalWrite(led,LOW);
  Serial.print("start");
}
void loop(){
 val=digitalRead(button);
 delay(10);
 val2=digitalRead(button);
 if (val==val2){
     buttonState=val;
}
 if (buttonState=HIGH){
   Serial.println("button pressed!");
   if (armed){
     Serial.println("disarmed");
     armed=false;
     digitalWrite(led,LOW);
   }
   else{
     Serial.println("armed");
     armed=true;
     digitalWrite(led,HIGH);
   }
 }
}

and the wiring can be seen here: ImageShack - Best place for all of your image hosting and image sharing needs
the only thing that happens is that the led blinks rapidly.what is wrong?(the resistor values are 10k for the switch and 1k for the led)

something is always HIGH...

what could it be?i have been working on this for hours :stuck_out_tongue:

HIGH <<

It seems to be a universal truth on this forum that whenever anyone describes the behavior of anything as weird in a post's title it turns out to be anything but and is just what is going to happen.

In your case have a look at that button, people have been known to have them rotated by 90 degrees and thus having a permanent short between the two contacts that they think is a switch.

the button is ok i have measured it.

And still the button state always equals to HIGH...

That was my last hint for today.

Go on he's not going to spot it.

Look to the if Luke!

i changed the '=' to a '==' and now it seems to work, although a little bit erratically.what can i do to improve it?

although a little bit erratically.

Can you explain a bit better what you mean by this.

Personally I would never connect a button like this. I would connect it between the input and ground and have the resistor go from the input to +5. Then invert the sense of the if statements.

why is the reverse setup better? by erraticaly i mean that it doesnt always switch states when i press it..

why is the reverse setup better?

See:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

it doesnt always switch states when i press it

Try a lower value resistor.