Hi
I need following modifications in the code below which I came across while googling:
a) Counting to be done with a Push-Button connected to pin A0.
b) The trailing zeros to be removed (e.g. if the number is 567, the display should be 567 instead of 0567).
I shall be grateful for the help.
/*
* Author: Rafael Zaleski
*
* The selector pin (common anode for each digit) changes every 5ms to display
* different numbers using the common pins 'a' to 'g' in the display.
*
* Don't forget that you have to change the code if your display is common cathode
*/
int a = 6, b = 7, c = 8, d = 9, e = 10, f = 11, g = 12, dp = 13; //Display pins
int d1 = 5, d2 = 4, d3 = 3, d4 = 2; //Common pins
int time = 500; //speed of the counter in ms
int buttonPin = 14;
void setup() {
int i = 1;
for(; i<13; i++)
pinMode(i, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
offd(d1);
offd(d2);
offd(d3);
offd(d4);
int i, j;
//The counter
for(i = 0; i<10000; i++)
{
for(j = 0; j <= (time/20); j++)
{
ond(d1);
num(i%10);
delay(5);
offd(d1);
ond(d2);
num(i/10 - 10*(i/100));
delay(5);
offd(d2);
ond(d3);
num(i/100 - 10*(i/1000));
delay(5);
offd(d3);
ond(d4);
num(i/1000);
delay(5);
offd(d4);
}
}
}
void on(int i) {
digitalWrite(i, LOW);
}
void off(int i){
digitalWrite(i, HIGH);
}
void ond(int i) {
digitalWrite(i, HIGH);
}
void offd(int i){
digitalWrite(i, LOW);
}
void num(int n) {
if(n < 0)
n = 0;
switch(n) {
case 0:
on(a);
on(b);
on(c);
on(d);
on(e);
on(f);
off(g);
break;
case 1:
off(a);
on(b);
on(c);
off(d);
off(e);
off(f);
off(g);
break;
case 2:
on(a);
on(b);
off(c);
on(d);
on(e);
off(f);
on(g);
break;
case 3:
on(a);
on(b);
on(c);
on(d);
off(e);
off(f);
on(g);
break;
case 4:
off(a);
on(b);
on(c);
off(d);
off(e);
on(f);
on(g);
break;
case 5:
on(a);
off(b);
on(c);
on(d);
off(e);
on(f);
on(g);
break;
case 6:
on(a);
off(b);
on(c);
on(d);
on(e);
on(f);
on(g);
break;
case 7:
on(a);
on(b);
on(c);
off(d);
off(e);
off(f);
off(g);
break;
case 8:
on(a);
on(b);
on(c);
on(d);
on(e);
on(f);
on(g);
break;
case 9:
on(a);
on(b);
on(c);
on(d);
off(e);
on(f);
on(g);
break;
}
}