So far I made this code.
Am I going in the right direction?
I dont know how should I modify function showNumber, could you show me how to do it?
const byte latchPin = 5; // Pin connected to Pin 5 of 74HC595 (Latch)
const byte dataPin = 6; // Pin connected to Pin 6 of 74HC595 (Data)
const byte clockPin = 7; // Pin connected to Pin 7 of 74HC595 (Clock)
const byte UpPinFirst = 12; // pushbutton connected to pin 12 for increment by 1 (first counter)
const byte DownPinFirst = 13; // pushbutton connected to pin 13 for decrement by 1 (first counter)
const byte UpPinSecond = A1; // pushbutton connected to pin A1 for increment by 1 (second
counter)
const byte DownPinSecond = A2; // pushbutton connected to pin A2 for decrement by 1 (second counter)
const byte DigitFirst1 = 8; // transistor for ones digit of connected to pin 8(first counter)
const byte DigitFirst2 = 9; // transistor for tens digit connected to pin 9 (first counter)
const byte DigitSecond1 = 2; // transistor for ones digit of connected to pin 2(second counter)
const byte DigitSecond2 = 3; // transistor for tens digit connected to pin 3 (second counter)
const byte reset = A0;
int res = 0;
int prevUpStateFirst = 0;
int prevDownStateFirst = 0;
int currUpStateFirst = 1;
int currDownStateFirst = 1;
int prevUpStateSecond = 0;
int prevDownStateSecond = 0;
int currUpStateSecond = 1;
int currDownStateSecond = 1;
int counterFirst = 0; // initialise first counter as zero
int counterSecond = 0; // initialise second counter as zero
byte numberOfDigits = 2;
const byte numbers[10] = // Describe each digit in terms of display segments 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
{
B11111100, //0
B01100000, //1
B11011010, //2
B11110010, //3
B01100110, //4
B10110110, //5
B10111110, //6
B11100000, //7
B11111110, //8
B11110110, //9
};
void setup()
{
pinMode(latchPin, OUTPUT); // set shift registers pins to output
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(UpPinFirst, INPUT);
pinMode(DownPinFirst, INPUT);
pinMode(DigitFirst1, OUTPUT);
pinMode(DigitFirst2, OUTPUT);
pinMode(UpPinSecond, INPUT);
pinMode(DownPinSecond, INPUT);
pinMode(DigitSecond1, OUTPUT);
pinMode(DigitSecond2, OUTPUT);
pinMode(reset, INPUT);
}
void loop()
{
//counter part
//first counter
currUpStateFirst = digitalRead(UpPinFirst);
if (prevUpStateFirst != currUpStateFirst) // has the state changed from HIGH to LOW or vice versa
{
prevUpStateFirst = !prevUpStateFirst; //invert
if (currUpStateFirst == HIGH && counterFirst <= 99) // If the button was pressed AND not reached 99
counterFirst++; // increment the counter by one
if(counterFirst > 99) counterFirst = 0; //if counter is greater than 99, set it to 0
}
currDownStateFirst = digitalRead(DownPinFirst);
if (prevDownStateFirst != currDownStateFirst) // has the state changed from HIGH to LOW or vice versa
{
prevDownStateFirst = currDownStateFirst;
if (currDownStateFirst == HIGH && counterFirst >= 0) // If the button was pressed AND not reached 0
counterFirst--; // decrement the counter by one
if(counterFirst < 0) counterFirst = 99; // counting backward from 99 to 0
}
//second counter
currUpStateSecond = digitalRead(UpPinSecond);
if (prevUpStateSecond != currUpStateSecond) // has the state changed from HIGH to LOW or vice versa
{
prevUpStateSecond = !prevUpStateSecond; //invert
if (currUpStateSecond == HIGH && counterSecond <= 99) // If the button was pressed AND not reached 99
counterSecond++; // increment the counter by one
if(counterSecond > 99) counterSecond = 0; //if counter is greater than 99, set it to 0
}
currDownStateSecond = digitalRead(DownPinSecond);
if (prevDownStateSecond != currDownStateSecond) // has the state changed from HIGH to LOW or vice versa
{
prevDownStateSecond = currDownStateSecond;
if (currDownStateSecond == HIGH && counterSecond >= 0) // If the button was pressed AND not reached 0
counterSecond--; // decrement the counter by one
if(counterSecond < 0) counterSecond = 99; // counting backward from 99 to 0
}
res = digitalRead(reset); //if the reset button was pressed, set the counters to 0
if(res == HIGH)
counterFirst = 0;
counterSecond = 0;
showNumber(counterFirst); // display the current digit
}
void showNumber(int number)
{
// digits are : (Digit1 - pin 8) and (Digit2 - pin 9)
for(byte i = 0; i < numberOfDigits; i++)
{
digitalWrite(DigitFirst1, LOW ) ; // switch off Digit1(ones)
digitalWrite(DigitFirst2, LOW ) ; // switch off Digit2(tens)
digitalWrite(latchPin, LOW); // Set latchPin LOW while clocking bits in to the register
shiftOut(dataPin, clockPin, LSBFIRST, numbers[number % 10]);
number /= 10;
digitalWrite(latchPin, HIGH); //set latchPin to high to lock and send data
digitalWrite(DigitFirst1, ( i == 0 ) ) ; // switch on/off Digit1
digitalWrite(DigitFirst2, ( i == 1 ) ) ; // switch on/off Digit2
delay(5);
}
}