Adding a button switch to do 3 functions

Noob just starting out. I'm trying to add a button switch to do 3 functions on my project, power off, press once and first half of a group of leds turns on, press again and all leds are on, 3rd press all leds are off. Here's the syntax:

const int buttonPin = 2;

int mode = 0;
int previousButtonstate = 0;

unsigned long previousTime = 0;

unsigned long currentTime = 0;

unsigned long elapsedTime = 0;

int led1 = 9;

int led2 = 10;

int led3 = 11;

int brightness1 = 0;

int brightness2 = 0;

int brightness3 = 255;

int currentState = 0;

int stateSwapMillis = 200;

int loopDelay = 5;

int fadeAmount = 255 / (stateSwapMillis / loopDelay);

void setup() {
pinMode(buttonPin, INPUT);
pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

Serial.begin(9600);

}

void loop() {

currentTime = millis();

buttonState = digitalread(buttonPin);
if 9buttonState == HIGH && previousButtonSte == LOW) {
mode == (mode = 1) % 3;
}

if ( mode == 0 ) {
// mod state is off
} else if ( mode == 1 ) {
// turn on main exterior and interior lights
}
else if ( mode == 2 ) {
// turn on emergency lights
}

analogWrite(led1, brightness1);

analogWrite(led2, brightness2);

analogWrite(led3, brightness3);

previosButtonState = buttonState;

elapsedTime += (currentTime - previousTime);

if ( elapsedTime >= stateSwapMillis ) {

currentState = (currentState + 1) % 3;

elapsedTime = 0;

}

adjustBrightnesses();

delay(loopDelay);

previousTime = currentTime;

}

void adjustBrightnesses() {

if ( currentState == 0 ) {

brightness1 += fadeAmount;

brightness2 = 0;

brightness3 -= fadeAmount;

} else if ( currentState == 1 ) {

brightness1 -= fadeAmount;

brightness2 += fadeAmount;

brightness3 = 0;

} else if ( currentState == 2 ) {

brightness1 = 0;

brightness2 -= fadeAmount;

brightness3 += fadeAmount;

}

}

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.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please not a Fritzy picture.

How have you got your button wired?
If between the input pin and 5V, have you got a 10k pulldown resistor between the input and gnd?
If between the input pin and gnd, have you got a 10k pullup resistor between the input and 5V?

Thanks… Tom…

Correcting obvious syntax error should be your first step...if 9buttonState == HIGH && previousButtonSte == LOW) {...that nine is probably a ( right...

Are your == and = mixed up here?

mode == (mode = 1) % 3;

J-M-L:
Correcting obvious syntax error should be your first step...if 9buttonState == HIGH && previousButtonSte == LOW) {...that nine is probably a ( right...

And previousButtonSte is probably previousButtonState ?

When you're having compile errors it's a good idea to post complete error listing. That way we're less likely to miss something.

Steve

If you just want your LEDs ON/OFF, you have a lot of extra code in there...

Also, based on your code, you will need an external pull-down resistor on that button

const int buttonPin = 2;

int mode = 0;
int previousButtonState = 0;

const int led1 = 9;
const int led2 = 10;
const int led3 = 11;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  Serial.begin(9600);
}


void loop() {

  currentTime = millis();
  int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && previousButtonState == LOW) {
    previousButtonState = buttonState;
    mode = (mode + 1) % 3;

    if ( mode == 0 ) {
      // off
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
    } else if ( mode == 1 ) {
      // half on
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
    }
    else if ( mode == 2 ) {
      // all on
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
    }
  }