Hi
I have written a mini arduino program that does the following
(1) Reads a switch button and increments/decrements a value
(2) display the value on a 4 digit 7 segment controlled by MAX7219
void loop ()
{
// see if switch is open or closed
byte switchState = digitalRead (switchPin); // pin 1
byte switchState1 = digitalRead (switchPin1); // pin 2
if(switchState != oldSwitchState) // code below only works when pin is high
{
// debounce
if ( millis() - switchPressTime >= debounceTime) // debounce time 10 ms
{
switchPressTime = millis();
oldSwitchState = switchState;
if (switchState == HIGH )
{
Serial.println ("Switch opened.");
a++;
printNumber(a);
}
} // end if switchState is HIGH
} // end if debounce time up
if (switchState1 != oldSwitchState1)
{
// debounce
if (millis () - switchPressTime1 >= debounceTime)
{
switchPressTime1 = millis ();
oldSwitchState1 = switchState1;
if (switchState1 == LOW)
{
Serial.println ("Switch1 closed.");
} // end if switchState is LOW
else
{
Serial.println ("Switch1 opened.");
a--;
if (a<0)
a=0;
printNumber(a);
} // end if switchState is HIGH
} // end if debounce time up
} // end of state change
// other code here ...
} // end of loop
// Print decimal numbers
void printNumber(int v) {
int ones;
int tens;
int hundreds;
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v;
//Now print the number digit by digit
lc.setDigit(0,4,(byte)ones,false);
lc.setDigit(0,3,(byte)tens,true);
lc.setDigit(0,2,(byte)hundreds,false);
}
Credit goes to Nick Gammon for the debounced switch code.
So it all works well except i need a long press where when the button is held down the value on the 7 segment needs to count up fast. Can anyone help me on this. I tried using while loop and using a counter and checking the counter for long/short presses, but it doesn't seem to work
Can this code be improved and i was also wondering how libraries are implemented on standalone arduino. The led control library for max7219 is 13 kb itself!.
Thank you.