Beating head against wall with a simple project...

I am trying to get a simple button project to work and its making me crazy...
Push button and scroll between All/Red/Blue/Green on a RGB LED
It gets stuck in a loop between state 1 & 3

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const int redPin = 9;   // Red LED,   connected to digital pin 9
const int grnPin = 10;  // Green LED, connected to digital pin 10
const int bluPin = 11;  // Blue LED,  connected to digital pin 11

// Variables will change:
int stateNum = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int redVal = 0;   // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(redPin, OUTPUT); 
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) 
    { stateNum++;
      if (stateNum>3) stateNum=0;
      Serial.println("on");
      Serial.print("State Number:  ");
      Serial.println(stateNum);  
      digitalWrite(ledPin, HIGH);
    } 
    else 
    { Serial.println("off");
      digitalWrite(ledPin, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  
  if (stateNum = 0) //White
 {  Serial.println("1 White");                
    grnVal = 255; 
    bluVal = 255;        
    redVal = 255;             
  }

else if (stateNum = 1) //Green
 {  Serial.println("1 Green");               
    grnVal = 255; 
    bluVal = 0;        
    redVal = 0;             
  }

else if (stateNum = 2) //Blue
 {  Serial.println("2 Blue");              
    bluVal = 255; 
    redVal = 0;        
    grnVal = 0;             
  }

    
else (stateNum = 3); //Red
 {  Serial.println("3 Red");           
    redVal = 255; 
    grnVal = 0;        
    bluVal = 0;          
  }

 analogWrite(redPin, redVal);   // Write values to LED pins
 analogWrite(grnPin, grnVal); 
 analogWrite(bluPin, bluVal);

}

else if (stateNum = 1) //Green

dont u mean----

else if (stateNum == 1) //Green

Yup - that's the issue.

= is assignment.
== is comparison.

Thanks, but that didnt resolve it :frowning:
its still stuck in a loop between 0&3 (not 1, I was wrong)

the first half is fine, I tested it... its in here:

if (stateNum == 0)
{ Serial.println("0 White");
grnVal = 255;
bluVal = 255;
redVal = 255;
}

else if (stateNum == 1) //Green
{ Serial.println("1 Green");
grnVal = 255;
bluVal = 0;
redVal = 0;
}

else if (stateNum == 2) //Blue
{ Serial.println("2 Blue");
bluVal = 255;
redVal = 0;
grnVal = 0;
}

else (stateNum == 3); //Red
{ Serial.println("3 Red");
redVal = 255;
grnVal = 0;
bluVal = 0;
}

analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);

}

Responding to Reply #3

You have = in other places where it should be ==. Check your program carefully.

...R

I found it, thanks!