ok, here's the excerpt of my code, with the initial settings, and then the execution, but the buttons just don't make the count move.
now i know the code is mostly correct, but as i was tinkering yesterday, i shorted out the usb and the working saved code was lost.
the simple jist is;
2 buttons, 1 for up, 1 for down, and when you press the up/down button, it checks to see if the button has been pressed in the past 50ms, and if it hasn't it increases the integer (count) by 1.
this count figure is then ported to the seven segment display.
if i put a number in the variable for the count, it is displayed as it should
if i put a for (int j =0; j < 255; j++) then the seven segment(j); counts as it should...
the buttons are cheap and basic, and work
i just can't see where this is failing.
const int upbuttonPin = 2; // the number of the up button pin
const int downbuttonPin = 3; // the number of the down button pin
// debounce buttons
long uplastDebounceTime = 0; // the last time the output pin was toggled
long updebounceDelay = 50; // the debounce time; increase if the output flickers
long downlastDebounceTime = 0; // the last time the output pin was toggled
long downdebounceDelay = 50; // the debounce time; increase if the output flickers
// initialise the button states
int upbuttonState; // the current reading from the up input pin
int uplastbuttonState = LOW; // the previous reading from the up input pin
int downbuttonState; // the current reading from the down input pin
int downlastbuttonState = LOW; // the previous reading from the down input pin
void loop()
{
// UP UP UP UP
int upreading = digitalRead(upbuttonPin); // read the state of the up button
if (upreading !=uplastbuttonState) { // put the button state into the buttonstate int
uplastDebounceTime = millis(); // check how long the debounce time is
}
if ((millis() - uplastDebounceTime) > updebounceDelay) { // if the time is ok then continue
if (upreading !=upbuttonState) { // check the reading to the state, if different
upbuttonState = upreading; // set the state
if (upbuttonState == HIGH) { // if the button state is high
int count = count++; // also increase the count by 1
}
}
uplastbuttonState = upreading; // save the button state to the reading
}
// DOWN DOWN DOWN DOWN
int downreading = digitalRead(downbuttonPin); // read the state of the down button
if (downreading !=downlastbuttonState) { // put the button state into the buttonstate int
downlastDebounceTime = millis(); // check how long the debounce time is
}
if ((millis() - downlastDebounceTime) > downdebounceDelay) { // if the time is ok then continue
if (downreading != downbuttonState) { // check the reading to the state, if different
downbuttonState = downreading; // set the state
if (downbuttonState == HIGH) { // if the button state is high
int count = count--; // also drop the count by 1
}
}
downlastbuttonState = downreading; // save the button state to the reading
}
{
SevenSegDisplay(count); // Run the display routine
}
}
You've declared count inside the innermost of statement, so it will only be available inside that if statement. Your curly braces also need some work to make it easier to read
i've copied the instructions from the arduino reference section, and the code is laid out exactly as mine is;
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// 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 (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
integer, inside an if, inside an if, inside an if being set, closed and then used.
that cplusplus site is great! thanks for the link!
i have solved the issue! and it was an issue created by myself...
on trying to set limits, i had a void called that had what to do if the number went over 999 (set to 999) and what to do if the number went less than 0 (set to 0).
i inadvertently set the count to 0 everytime it looped... it didn't matter if the count changed, as soon as it called that subroutine, it set count back to 0.
...you live and learn!
code is now working, with all debouncing put into place, and limits set!