Pages: [1] 2   Go Down
Author Topic: button trouble?  (Read 884 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so I'm not sure what to do about this but I'm a total beginner and have no real background in c++ programing, I'm trying to figure out how to add another button on this circuit in the programing but im having trouble figuring it out, can anyone help me?

this is the program so far,

Code:
[code]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 ledState = LOW;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

// 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 lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 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 != lastButtonState) {
    lastDebounceTime1 = millis();
  )
  if (reading2 != lastButtonState) {
  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:
    buttonState = reading;
    
  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:
    buttonState = reading;
  }
 
  // 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;
}
[/code]

attached is a diagram of my hardware...


* hardware.jpg (447.44 KB, 1839x933 - viewed 13 times.)
« Last Edit: March 16, 2013, 01:09:54 pm by zurek22 » Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2185
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good for you for starting to learn the Arduino platform!

Note: when you compile the code you attached, there are numerous errors in the bottom of your IDE window.
Review these errors
Track these errors down one by one and correct them.
HINT: perhaps you failed to declare some variables.

Get your code to work using 1 button.

Finally add code for the 2nd button.
« Last Edit: March 12, 2013, 11:51:15 pm by LarryD » Logged

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi ,

I will suggest you to see the debounce state of push button using delays() first to get a good comprehension of the thing. and then move on with using millis().

Try using conditional functions as if ( reading == HIGH) { --- } instead of assigning everything to variables, just to grasp what happening behind the code.

....

taz
Logged


tz
.....

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3489
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This area of the code need some attention too.
Code:
  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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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;
}
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2185
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


  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.



Logged

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

instead of :
Code:
lastButtonState1 = reading1;
lastButtonState2 = reading2
;

try :
Code:
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
Logged


tz
.....

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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
  }
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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
  }


Code:
digitalWrite(pinLED, !digitalRead(pinBUTTON));
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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 -

Code:
digitalWrite(pinLED, !digitalRead(pinLED));
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yea i just cant figure out why the led isnt staying on when i press the button i just dont get it  smiley-cry
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i just dont get it
Then perhaps the time has come to explain how your switches are wired.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: