So I'm making an electronic scoreboard, and I have a Uno hooked up to 3 shift registers to give a 1-999 count,
it's working fine except for a minor issue - when pressing the increase count button it increases then pressing the decrease count button increases the count by 1 and then the next press lowers it.
So if the count was 0 it would go as follows
up button pressed = 1
up = 2
down = 3
down = 2
down = 1
up = 0
up = 1
It is a minor issue I could live with, but ideally I'd like to solve it if it is a simple fix. Any idea's?
Thanks.
// Pin connected to SH_CP of 74HC595 = Pin 11
int clockPin = 4;
// Pin connected to ST_CP of 74HC595 = Pin 12
int latchPin = 5;
// Pin connected to DS of 74HC595 = Pin 14
int dataPin = 6;
byte dataArray[10];
const int buttonpin1 = 2; // +1 button
const int buttonpin2 = 3; // -1 button
// variable
int button1state = 0;
int button2state = 0;
int button3state = 0;
int count = 0;
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonpin1, INPUT);
pinMode(buttonpin2, INPUT);
dataArray[0] = 0x03; // 0
dataArray[1] = 0x9F; // 1
dataArray[2] = 0x25; // 2
dataArray[3] = 0x0D; // 3
dataArray[4] = 0x99; // 4
dataArray[5] = 0x49; // 5
dataArray[6] = 0xC1; // 6
dataArray[7] = 0x1F; // 7
dataArray[8] = 0x01; // 8
dataArray[9] = 0x19; // 9
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[0]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[0]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[0]);
digitalWrite(latchPin, 1);
}
void loop() {
int val = digitalRead(buttonpin1); // plus button
int val2 = digitalRead(buttonpin2); // minus button
int num1 = count / 100; // hundreds
int num2 = (count / 10) - (num1*10); //tens
int num3 = count - num1*100 - num2*10; // units
// main loop ###################
if (val == HIGH && val2 == LOW)
{
count++;
if (count >= 1000) count = 0;
delay(100);
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num3]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num2]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num1]);
digitalWrite(latchPin, 1);
}
else if (val2 == HIGH && val == LOW)
{
count--;
if (count <= -1) count = 999;
delay(100);
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num3]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num2]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num1]);
digitalWrite(latchPin, 1);
}
else if (val == HIGH && val2 == HIGH)
{
count = 0;
delay(100);
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num3]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num2]);
shiftOut(dataPin, clockPin, LSBFIRST, dataArray[num1]);
digitalWrite(latchPin, 1);
}
}