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 12int 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 secondsw = mins / 10; //
x = mins % 10;
y = secs / 10;
z = secs % 10; // % sign means modulo which does the divison and gives remainderlc.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;
}