Raleigh, NC.
Offline
Full Member
Karma: 0
Posts: 129
Addicted to Arduino
|
 |
« on: October 26, 2012, 09:35:40 am » |
Hello Guys,
I am trying to configure first an Up and down or increment and decrement control for a display to show a "Volume" increasing by one or decreasing by one....I have attached my schematic and code...I can get the "Up" count working but then I tried some "--" in my code and it didn't work..Please help....
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
const int buttonPin = 5; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 6; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button void setup() {
// initialize the button pin as a input: pinMode(buttonPin, INPUT); pinMode(buttonPin1, INPUT); lcd.begin(16,2); lcd.setCursor(0,1); lcd.print("Volume:"); }
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) { buttonPushCounter++; lcd.setCursor(7,1); lcd.print(buttonPushCounter); }
} // save the current state as the last state, //for next time through the loop lastButtonState = buttonState;
}
schematic attached
|
|
|
|
|
Logged
|
|
|
|
|
Raleigh, NC.
Offline
Full Member
Karma: 0
Posts: 129
Addicted to Arduino
|
 |
« Reply #1 on: October 26, 2012, 09:37:55 am » |
Just looked at my schematic......the pull up resistors are tied to high but they are connected to GND here....disregard that mistake....
|
|
|
|
|
Logged
|
|
|
|
|
Raleigh, NC.
Offline
Full Member
Karma: 0
Posts: 129
Addicted to Arduino
|
 |
« Reply #2 on: October 26, 2012, 11:31:28 am » |
Please somebody help.....I have been racking my brain over how easy this is suppose to be but everything I try doesn't work
|
|
|
|
|
Logged
|
|
|
|
|
Venezuela
Offline
Full Member
Karma: 8
Posts: 229
Ground.......ground........always ground
|
 |
« Reply #3 on: October 26, 2012, 11:49:31 am » |
If I were you, I would add a Serial.println() for every LCD.print() you have in your code plus the counter and state registers. If after test your volume buttons (dec/inc) everything goes right serially, then you have a hardware issue. These are just some ideas.
|
|
|
|
|
Logged
|
|
|
|
|
Raleigh, NC.
Offline
Full Member
Karma: 0
Posts: 129
Addicted to Arduino
|
 |
« Reply #4 on: October 26, 2012, 11:53:29 am » |
I just can't get over how easy this seems but its kicking my ass......I mean, I have the BUtton state and all that...and with the ++ it works great incrementing...I even took the wire from the other button I'm trying to use as the 'Down counter" and it make the count go up...so somewhere in my code there needs to have some "--" decrementing, but I can't figure it out...hardware is good...its my code I need to fix....somebody out there has got to know..
|
|
|
|
|
Logged
|
|
|
|
|
Venezuela
Offline
Full Member
Karma: 8
Posts: 229
Ground.......ground........always ground
|
 |
« Reply #5 on: October 26, 2012, 12:45:40 pm » |
Try this: #include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
const int buttonPin = 5; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 6; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses int buttonState5 = 0; // current state of the button int buttonState6 = 0; // current state of the button int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input: pinMode(buttonPin, INPUT); pinMode(buttonPin1, INPUT); lcd.begin(16,2); lcd.setCursor(0,1); lcd.print("Volume:"); }
void loop() { // read the pushbutton up input pin: buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state if (buttonState5 != lastButtonState) { // if the state has changed, increment the counter if (buttonState5 == HIGH) { buttonPushCounter++; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState5; // read the pushbutton down input pin: buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state if (buttonState6 != lastButtonState) { // if the state has changed, decrement the counter if (buttonState6 == HIGH) { buttonPushCounter--; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState6; }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 5
Posts: 469
what?
|
 |
« Reply #6 on: October 26, 2012, 01:07:20 pm » |
"--" decrementing, but I can't figure it out instead of -- use -=1 or -=2 or any decrement you like, sam for incrementing +=1
|
|
|
|
|
Logged
|
|
|
|
|
Raleigh, NC.
Offline
Full Member
Karma: 0
Posts: 129
Addicted to Arduino
|
 |
« Reply #7 on: October 26, 2012, 01:32:23 pm » |
Thanks to both of you....I used both of the information given...my code now is shown below...
Its working...I just have to tweak it a little with some delays etc...
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
const int buttonPin = 5; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 6; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses int buttonState5 = 0; // current state of the button int buttonState6 = 0; // current state of the button int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input: pinMode(buttonPin, INPUT); pinMode(buttonPin1, INPUT); lcd.begin(16,2); lcd.setCursor(0,1); lcd.print("Volume:"); }
void loop() { // read the pushbutton up input pin: buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state if (buttonState5 != lastButtonState) { // if the state has changed, increment the counter if (buttonState5 == HIGH) { buttonPushCounter++; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } delay(50); } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState5; // read the pushbutton down input pin: buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state if (buttonState6 != lastButtonState) { // if the state has changed, decrement the counter if (buttonState6 == HIGH) { buttonPushCounter-=1; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } delay(50); if (buttonPushCounter < 10) { lcd.setCursor(8,1); lcd.print(" "); } if (buttonPushCounter <= 0) { lcd.setCursor(7,1); lcd.print("OFF"); } if (buttonPushCounter >= 25) { lcd.setCursor(7,1); lcd.print("Max"); } } // save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6; }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13898
Lua rocks!
|
 |
« Reply #8 on: October 26, 2012, 03:29:13 pm » |
Please edit your post, select the code, and put it between [code] ... [/code] tags.
You can do that by hitting the # button above the posting area.
Please use code tags in future, it makes your code easier to follow.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13898
Lua rocks!
|
 |
« Reply #9 on: October 26, 2012, 03:29:56 pm » |
Also the double-spacing is distracting. This isn't an essay.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #10 on: May 05, 2013, 09:34:52 am » |
Hi .. i'm a newb i tried this code ... on my arduino (gift) and it seems to go beyond the "OFF" & "MAX" how do i make it count from OFF to 5(MAX) ... because this code its prints OFF and MAX on display .. but he goes beyond that with -1 -2 -3 -4 -5 ..and when i press my +button ... i have to go from -2 or how many times i pushed the botton after OFF and for MAX the same ..goes after my if (buttonPushCounter >= 5) AND i want to modify this code to control a blinking led ... something like : if OFF = closed led if 1 = 1/min if 2 = 2/min if 3 = 3/min if MAX= 4/min + to display this PS i don't have any coding knowledge sry  but im triend to learn but i got this 2 days ago ..i i dont want to trow it away because i dont know how this is done I have browsed the forum for something similar ..cant find anything ... Thanks to both of you....I used both of the information given...my code now is shown below...
Its working...I just have to tweak it a little with some delays etc...
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
const int buttonPin = 5; // the pin that the Up pushbutton is attached to
const int buttonPin1 = 6; // the pin that the Down pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses int buttonState5 = 0; // current state of the button int buttonState6 = 0; // current state of the button int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input: pinMode(buttonPin, INPUT); pinMode(buttonPin1, INPUT); lcd.begin(16,2); lcd.setCursor(0,1); lcd.print("Volume:"); }
void loop() { // read the pushbutton up input pin: buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state if (buttonState5 != lastButtonState) { // if the state has changed, increment the counter if (buttonState5 == HIGH) { buttonPushCounter++; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } delay(50); } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState5; // read the pushbutton down input pin: buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state if (buttonState6 != lastButtonState) { // if the state has changed, decrement the counter if (buttonState6 == HIGH) { buttonPushCounter-=1; lcd.setCursor(7,1); lcd.print(buttonPushCounter); } delay(50); if (buttonPushCounter < 10) { lcd.setCursor(8,1); lcd.print(" "); } if (buttonPushCounter <= 0) { lcd.setCursor(7,1); lcd.print("OFF"); } if (buttonPushCounter >= 25) { lcd.setCursor(7,1); lcd.print("Max"); } } // save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState6; }
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Sr. Member
Karma: 9
Posts: 333
|
 |
« Reply #11 on: May 05, 2013, 05:23:31 pm » |
This appears to be a new member picking up an old (> 6 months) thread. The quote was unnecessary however.
@siko: You need to learn some more basics. If you have a range you want to use, you are the one to tell your Arduino how to handle that range. So if off is 0, then do not allow the value to go below 0, and if on = 255 do not allow the value to be more than that. Or if the value is already "out of range", force it back in range before you do anything else with it. There are multiple ways of handling this. Everything you need for that is already in the sketch, use your imagination to extend it to fit your needs. (You just need to add 2 lines).
|
|
|
|
|
Logged
|
|
|
|
|
|