I'm trying to create 3 digit counter but something is not right.
This is my code:
#include <SPI.h>
#include <EEPROM.h>
// Defining pins and their name for better usage
// Buttons
#define increaseButtonPin 4
#define decreaseButtonPin 3
#define limitButtonPin 2
// Feedback
#define buzzer 7
#define ledGreen 0
#define ledRed 1
// DATA
#define PIN_DATA 11
#define PIN_CLOCK 9
#define PIN_LATCH 10
// Prototypes
void update_shift_reg(byte output); // Byte num to displays
void display(int *num); // Displays number on the 7-segment displays
void setup()
{
// Shift register
pinMode(PIN_DATA, OUTPUT);
pinMode(PIN_CLOCK, OUTPUT);
pinMode(PIN_LATCH, OUTPUT);
// Feedback
pinMode(increaseButtonPin, INPUT);
pinMode(decreaseButtonPin, INPUT);
pinMode(limitButtonPin, INPUT);
// Initialize LEDs and buzzer
pinMode(buzzer, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
// Set LEDs and buzzer to low initially
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
}
void loop()
{
// Reads value from the memory
int num = EEPROM.read(1);
int limit = EEPROM.read(2);
// Read button states
int increaseButtonState = digitalRead(increaseButtonPin);
int decreaseButtonState = digitalRead(decreaseButtonPin);
int limitButtonState = digitalRead(limitButtonPin);
if (increaseButtonState == HIGH)
{
num ++;
display(&num);// Increment the number and display it
digitalWrite(ledGreen, HIGH); // Perform feedback actions (LED, buzzer)
tone(buzzer, 100);
delay(50);
noTone(buzzer);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
int count = 0;//this part will be used for increasing number on dispaly by 10
while(digitalRead(increaseButtonPin) == HIGH){ //Code for increasing the number with special conditions
count++;
display(&num);
if( count >= 1000 ){
num += 9;
count = 0;
display(&num);
break;
}
}
}
if (decreaseButtonState == HIGH)
{
num --;// Decrease the number and display it
display(&num);
tone(buzzer, 100);
delay(50);
noTone(buzzer);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
delay(50);
display(&num);
int count = 0; // Code for decreasing the number with special conditions
while(decreaseButtonState == HIGH){
count++;
display(&num);
if( count >= 1000 ){
num -= 9;
count = 0;
display(&num);
break;
}
}
}
if (limitButtonState == HIGH)
{
limit = num;
}
display(&num);// Display the number on the 7-segment displays
digitalWrite(ledGreen, LOW); // Perform feedback actions (LEDs)
digitalWrite(ledRed, LOW);
if (num > limit)
{
num = limit;
digitalWrite(ledRed, HIGH);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(ledRed, LOW);
digitalWrite(buzzer, LOW);
}
// Store the updated values in EEPROM memory
EEPROM.update(1, num);
EEPROM.update(2, limit);
}
void update_shift_reg(byte output)
{
digitalWrite(PIN_LATCH, LOW);
shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, output);
digitalWrite(PIN_LATCH, HIGH);
}
void display(int *num)
{
byte ones = *num % 10;
byte tens = (*num / 10) % 10;
byte hundreds = (*num / 100) % 10;
byte _data = B00000000;
update_shift_reg(_data);
_data = _data | B00000001;
_data = _data | (ones << 3);
update_shift_reg(_data);
_data = B00000000;
update_shift_reg(_data);
_data = _data | B00000010;
_data = _data | (tens << 3);
update_shift_reg(_data);
_data = B00000000;
update_shift_reg(_data);
_data = _data | B00000100;
_data = _data | (hundreds << 3);
update_shift_reg(_data);
}
Can you find the problem?