Button state not sticky

Hello programming gurus! :grin:

I am and extreme novice, and I have been struggling with a button press behavior. What I am trying to accomplish seems simple to me, but the logic just isn't working the way I expect. When I press my button I want a certain set of LEDs to come on, and stay on. When pressed again, to revert to the starting set of LEDs. The behavior that it exhibits is I have my starting LEDs on, when I press the button it changes to the other set, and changes back when I release the button. I tried a number of different methods for saving the state of the button press to no avail. This is a stepping stone to adding a fade-in effect, but I can't get the button to work the way I expect. I have changed where the lastButtonState variable sits in the brackets to no avail. I'm sure this is something very simple to an experienced programmer, but I'm just learning at this point. Below if the Code:

// Used to control the warp and impulse engines
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int warpredPin =  6;       // the number of the LED pin
const int warpbluePin = 5;
const int impulseredPin = 11;
const int impulsegreenPin = 10;


// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin


void setup() {
  // initialize the LED pin as an output:
  pinMode(warpredPin, OUTPUT);
  pinMode(warpbluePin, OUTPUT); 
  pinMode(impulseredPin, OUTPUT);
  pinMode(impulsegreenPin, OUTPUT); 
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  //digitalWrite(warpbluePin, HIGH);
  //digitalWrite(warpredPin, LOW);
  //digitalWrite(impulseredPin, LOW);
  //digitalWrite(impulsegreenPin, HIGH);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  //compare the buttonState to its previous state
  if (buttonState != lastButtonState){


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(warpredPin, HIGH);
    digitalWrite(warpbluePin, LOW);
    digitalWrite(impulsegreenPin, LOW);
    digitalWrite(impulseredPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(warpbluePin, HIGH);
    digitalWrite(warpredPin, LOW);
    digitalWrite(impulsegreenPin, HIGH);
    digitalWrite(impulseredPin, LOW);
 
  }
 
}
 // save the current state as the last state
  // for the next time through the loop
  lastButtonState = buttonState; 
}

Thanks in advance for the help!

How about a latch.

boolean latch = false;
byte button_pin = 2;
byte button;
byte LED_pin = 13;

void setup() {
.
.
.
}

void loop() {

button = digitalRead(button_pin);
//NOTE: you will need to debounce button
if(button == HIGH) {
   latch = !latch;
   if(latch == true) {
      digitalWrite(LED_pin, HIGH);
   }
   else {
      digitalWrite(LED_pin, LOW);
   }
}

One thing that I find helps is to put the { on a new line. Use Tools + Auto Format to properly indent the code, if it gets out of whack, as yours is.

void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  //compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
     // check if the pushbutton is pressed.
     // if it is, the buttonState is HIGH:
     if (buttonState == HIGH)
     {
     }
     else
     {
     }
  }
  lastButtonState = buttoneState;
}

is just easier to see the structure of.

Now, your code might not be the culprit at all. You are not using the internal pullup resistor which makes wiring switches trivial, so you must have an external pullup or pulldown resistor. How IS your switch wired?

Thanks for the quick reply guys! Yes I am wired direct to the 5v and Grnd with my button and a 1k resistor. Should this just be wired to pin2 and Grnd without the direct power?

Yes I am wired direct to the 5v and Grnd with my button and a 1k resistor.

So, we know what components you are using, but not how.

Should this just be wired to pin2 and Grnd without the direct power?

That is easier. Just turn on the pullup resistor, using digitalWrite(2, HIGH) after the pinMode() statement. Then, change HIGH to LOW to mean pressed.

I have not tried the pull-up resistor yet. Here is a drawing of my pro to layout:

Uploaded with ImageShack.us

Here are some observations:

Your green LED needs a current-limiting resistor

Your red LED is not connected to D11. You need to run the wire to the other side of the center rail.

If I were you, I would use the internal pull-up resistor on D2 and have the button go from D2 to ground, thus eliminating the external resistor.