controlling a 7 segment display with two buttons

Hi there

I'm trying to control a 7 segment display with two buttons, one to reset it and another to start/stop it. I'm having trouble with my code and was wondering if anyone could point out where i'm going wrong with it?

#include <LedControl.h>

LedControl lc=LedControl(12,11,10,1); //* Pin 12 is connected to the DATA IN-pin of the first MAX7221 pin 1
//* Pin 11 is connected to the CLK-pin of the first MAX7221 PIN 13
//* Pin 10 is connected to the LOAD(/CS)-pin of the first MAX7221 PIN 12

int w,x,y,z;
int button = 2;
int BUTTON = 3;
int buttonvalue=0;
int BUTTONVALUE=0;
int buttonPin=0;
int state=0;
int p=0;
void setup(){

pinMode(button, INPUT);
pinMode(BUTTON, INPUT);
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
Serial.begin(9600);
}

void loop(){

// handle button presses

int BUTTON = digitalRead(BUTTON);
if(BUTTON==LOW){
Serial.println(" BUTTON LOW");

if (state==0){
state=0;
}

int but = digitalRead(button);
if(but==LOW){
Serial.println("button LOW");

if(state==0){
state=1;
}
else if(state==1){
state=2;
}
else if(state==2){
state=1;
}
}

// now act according to state

Serial.println(state);

if(state==0){

lc.setDigit(0,0,0,false); //first driver, digit 0, w is minutes
lc.setDigit(0,1,0,true);
lc.setDigit(0,2,0,true);
lc.setDigit(0,3,0,false);
}

if(state==1){

unsigned long t=millis();

int mins = (t / (1000UL * 60)) % 60; // calculate minutes
int secs = (t / 1000) % 60; // seconds
int hths = t % 100; // hundredths of seconds

w = mins / 10; //
x = mins % 10;
y = secs / 10;
z = secs % 10; // % sign means modulo which does the divison and gives remainder

lc.setDigit(0,0,w,false); //first driver, digit 0, w is minutes
lc.setDigit(0,1,x,true);
lc.setDigit(0,2,y,true);
lc.setDigit(0,3,z,false);
}

if (state==2){
// do nothing
}
// ------- debounce code ---------

delay(100);
}

// a function-based version of http://www.arduino.cc/en/Tutorial/Debounce
// by Dave Mellis.

int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // 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

// debounceRead() -- debounced version of digitalRead()

int debouncedRead(int p); {
// read the state of the switch into a local variable:
int reading = digitalRead(p);
// 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:
buttonState = reading;
}

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
return buttonState;
}

You could do yourself a favour and decide what is a button state and what is a button pin, and name your variables unambiguously accordingly.

This line:

int BUTTON = digitalRead(BUTTON);

merits some attention.

BTW Saying "I'm having trouble" doesn't mean very much to anyone else if we don't know what the expected behaviour and the observed behaviour are.

the button state is telling the button which state to run so if button is state 1 and then is pressed run state 2, button pin is the pin going into the arduino

Did you mean "button", or "BUTTON" (either one) ? :wink:

Seriously, look at the scope rules for 'C' and then look at the line I highlighted.

sorry, I'm a bit of a beginner with arduino so i'm not sure what you mean. as far as "button" and "BUTTON" go i thought i had to give them two different names when i declare them at the start of the project.

OK, yes calling them different names is a good idea BUT:

int BUTTON = digitalRead(BUTTON);

Creates an "int" called "BUTTON" and then calls "digitalRead" with the value of the newly-created "int" as its parameter.
But BUTTON hasn't got a valid value, because you haven't initialised it yet.

thanks that seems to have helped only problem i'm having now is with this part of the code

In function 'void loop()':
error: return-statement with a value, in function returning 'void

}

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
return buttonState;
}

do you have any suggestions as i'm stuck with this as well

thank you for your help so far

This int debouncedRead(int p); { will (and have) cause you pain.
Try int debouncedRead(int p) {

:slight_smile:

[edit]Internal Server Error HTTP 500[/edit]