button trouble?

This area of the code need some attention too.

  if (reading1 != lastButtonState) 
  {
    lastDebounceTime1 = millis();
  )
  if (reading2 != lastButtonState) 
  {
  lastDebounceTime2 = millis();
  }

Look closely at the brackets.
Elsewhere in the code a curly bracket has gone missing too. Auto format will give you a clue.
NOTE - I have put each curly bracket on its own line as I find it easier to read the program structure that way.

The good news is that you are going the right way by having different variables for each button, but that will soon get tedious and difficult to maintain. Your variables such as lastButtonState1 and lastButtonState2 would be better held in an array like lastButtonState[1] and lastButtonState[2]. All the other variables relating to specific buttons can also be put in arrays.

This would allow you to process the button presses and actions in a for loop using the loop index variable to access the array. In practice the array index will be better if it starts at zero so your variables become lastButtonState[0] and lastButtonState[1] etc

Thank you SO much guys, i figured out the code thanks to all of you, it was a matter of correcting the INT, however now I'm having an issue with the program not saving the last button press, i press on the button and it doesn't stay on like it should, any thoughts? here's the corrected code

by the way I pretty much modified the code provided in the tutorial http://arduino.cc/en/Tutorial/Debounce all i wanted to do was figure out how it works, and I'm trying to add another button to do the same thing, this is the best way that i can learn.

const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin1 =  12;      // the number of the LED pin
const int ledPin2 =  13;      // the number of the LED pin

// Variables will change:
int ledState1 = HIGH;         // the current state of the output pin
int ledState2 = HIGH;         // the current state of the output pin
int buttonState1;             // the current reading from the input pin
int buttonState2;             // the current reading from the input pin
int lastButtonState1 = LOW;   // the previous reading from the input pin
int lastButtonState2 = LOW;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime1 = 0;  // the last time the output pin was toggled
long lastDebounceTime2 = 0;  // the last time the output pin was toggled
long debounceDelay1 = 50;    // the debounce time; increase if the output flickers
long debounceDelay2 = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading1 = digitalRead(buttonPin1);
  int reading2 = digitalRead(buttonPin2);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
  }
  if (reading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
  }
  
  if ((millis() - lastDebounceTime1) > debounceDelay1) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState1 = reading1;
  }
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState2 = reading2;
  }
 
  // set the LED using the state of the button:
  digitalWrite(ledPin1, buttonState1);
  digitalWrite(ledPin2, buttonState2);
  
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState1 = reading1;
  lastButtonState2 = reading2;
}

lastButtonState1 = reading1; //<<<<<<<<<<<<<<<<<<<< This happens every time you go through loop
lastButtonState2 = reading2; //<<<<<<<<<<<<<<<<<<<< This happens every time you go through loop

Perhaps they should be made equal only when something is detected.

instead of :

lastButtonState1 = reading1;
lastButtonState2 = reading2

;

try :

lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;

yea nothing has worked, i have tried many things such as
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;

its just not saving my button press...

i want the led to stay on when i press the button, and turn off when i press the button again.

my overall goal is to figure out a way to take these two buttons and create a timer attached to the buttons to regulate my home lights.

so what i want is when i press the button once, it starts a timer and turns on the led, i want this for 2 buttons so that they are independent from each other.

however what i want is to learn how this works, how each button is working, but im jammed up i want this to stay in memory when i press the button but what its doing is as i press the button the led comes on, when i release the button it turns off, i want this to stay on untill i press the button again. my code is as fallows

const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin1 =  12;      // the number of the LED pin
const int ledPin2 =  13;      // the number of the LED pin

// Variables will change:
int ledState1 = HIGH;         // the current state of the output pin
int ledState2 = HIGH;         // the current state of the output pin
int buttonState1;             // the current reading from the input pin
int buttonState2;             // the current reading from the input pin
int lastButtonState1 = LOW;   // the previous reading from the input pin
int lastButtonState2 = LOW;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime1 = 0;  // the last time the output pin was toggled
long lastDebounceTime2 = 0;  // the last time the output pin was toggled
long debounceDelay1 = 50;    // the debounce time; increase if the output flickers
long debounceDelay2 = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading1 = digitalRead(buttonPin1);
  int reading2 = digitalRead(buttonPin2);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
  }
  
  if (reading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
  }
  
  if ((millis() - lastDebounceTime1) > debounceDelay1) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState1 = reading1;
  }
  
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState2 = reading2;
  }
 
  // set the LED using the state of the button:
  digitalWrite(ledPin1, buttonState1);
  
  digitalWrite(ledPin2, buttonState2);
  
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState1 = reading1;
  lastButtonState2 = reading2;
}

i was also curious if i could use something like this somewhere

  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }

Use a variable to hold the state of the LED. When a button is pressed just invert the state of that variable and use it to control the LED each time through loop.

// I'd probably take this sort of approach

int ledState = LOW;  // declare a variable to keep our led's state

// In setup, right after setting the ledpin's mode write the ledState to it 

digitalWrite (ledPin, ledState); //synchronise the variable to the real world

// Then in loop when a button press is valid flip the state of the variable

ledState = !ledState;

// then during loop just write the ledState each time

digitalWrite (ledPin, ledState);  //

Hope that makes sense and is helpful.

zurek22:
i was also curious if i could use something like this somewhere

  val = digitalRead(inPin);  // read input value

if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }

digitalWrite(pinLED, !digitalRead(pinBUTTON));

Jimmy60:
Use a variable to hold the state of the LED. When a button is pressed just invert the state of that variable and use it to control the LED each time through loop.

// I'd probably take this sort of approach

int ledState = LOW;  // declare a variable to keep our led's state

// In setup, right after setting the ledpin's mode write the ledState to it

digitalWrite (ledPin, ledState); //synchronise the variable to the real world

// Then in loop when a button press is valid flip the state of the variable

ledState = !ledState;

// then during loop just write the ledState each time

digitalWrite (ledPin, ledState);  //




Hope that makes sense and is helpful.

No state variable as the LED pin already knows its last written state -

digitalWrite(pinLED, !digitalRead(pinLED));

yea i just cant figure out why the led isnt staying on when i press the button i just dont get it =(

i just dont get it

Then perhaps the time has come to explain how your switches are wired.

if you look at the original post attached is how i have things wired, its an attached diagram... from fritzing

Those buttons might need turning thru 90 degrees...Have a look at the pic below to see how they switch internally... Might not be switching the connections you expect.

switch2.jpg

never mind that's wrong, the signal wires are above the resistors as shown in this attachment

my mistake sorry, you seem like you know what you are doing and i hope that everyone on this forum can help me out

Trace the flow of current. How does an electron get from the Arduino's power source to ground? You don't have any connection to ground in that schematic. Hopefully, it's just the schematic that is wrong.

It's far simpler, though, to use the internal pullup resistors. After the pinMode() statement, add digitalWrite(pinNumber, HIGH) to turn them on.

Then, connect one side of the switch to ground and the other side to the digital pin. HIGH will be not pressed; LOW will be pressed.

stupid me, i was drawing the specs to fast, its on the ground, black is ground, im retarded i should have checked before i posted it, wops, but yea, i tried to do that, its still not working so i pulled every thing off the proto shield and im re wiring everything ill see what that does any sugestions about wiring should i up the resistor? should i use new buttons? wire it differently?

Funny you should say this Paul:

It's far simpler, though, to use the internal pullup resistors...... HIGH will be not pressed; LOW will be pressed.

I always forget about them, and even as we speak have two buttons with external resistors on my board. I think one of the reasons I forget them, is that I still expect a HIGH from a pushed button, not a not-pushed one. But seeing as I'm working with interrupts, and the attachInterrupt method has a LOW mode but not a HIGH mode (on an Uno anyhow), a pullup makes more sense.

So thanks for reminding me....

I think one of the reasons I forget them, is that I still expect a HIGH from a pushed button, not a not-pushed one.

If you think about the top of the push-button switch, it is high when not pressed, and low when it is pressed. That's how I remember the logic behind the pullup-resistor-enabled switches.

and im re wiring everything ill see what that does any sugestions about wiring should i up the resistor? should i use new buttons? wire it differently?

You should be using a multimeter to make sure that the switches are wired correctly - that there is current flow at only the right times, etc.

You should be using a multimeter to make sure that the switches are wired correctly

Especially to get your head around how those 4-pin square pushbutton switches work. If you don't have a meter, at least hook up an LED and a resistor in series with a power source (you could use the Arduino's 5v for that....) , and probe those switch pins with the button pushed and not-pushed, and draw a sketch like the one I posted.