// Forum:
// https://forum.arduino.cc/t/more-than-one-shift-is-not-executed-arduino-uno/1085899
#include <LiquidCrystal.h>
#define dataPin 4 // Pin for data input to 74HC595 shifter
#define latchPin 3 // Pin for latch signal to 74HC595 shifter
#define clockPin 2 // Pin for clock signal to 74HC595 shifter
#define buttonLeft 5 // Pin for the left button
#define buttonRight 6 // Pin for the right button
#define rs 12 // RS pin of the LCD
#define en 11 // Enable pin of the LCD
#define d4 10 // D4 pin of the LCD
#define d5 9 // D5 pin of the LCD
#define d6 8
#define d7 7 // D7 pin of the LCD
// Initialize the LCD with the defined pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte currentValue = 0; // Current value of LEDs
int buttonLeftValue = 0;
int buttonRightValue = 0;
int buttonLeftOldValue = 0;
int buttonRightOldValue = 0;
int stateLeft = 0;
int stateRight = 0;
void setup() {
// Set the input/output modes for the pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
lcd.begin(16, 2);
lcd.print("000");
}
void loop() {
int analogValue = analogRead(A0); // Read the analog value from the potentiometer
int digitalValue = map(analogValue, 0, 1023, 0, 256); // Map the analog value to a digital value between 1 and 255
buttonLeftValue = digitalRead(buttonLeft); // Read the input value of the left button and store it
buttonRightValue = digitalRead(buttonRight); // Read the input value of the right button and store it
// If the digital value has changed
if (digitalValue != currentValue) {
currentValue = digitalValue; // Update the current value
digitalWrite(latchPin, LOW); // Set latchPin to LOW
shiftOut(dataPin, clockPin, LSBFIRST, currentValue); // Send the data
digitalWrite(latchPin, HIGH); // Set latchPin to HIGH to transfer the data to the output pins
lcd.setCursor(7, 0);
char buffer[5];
sprintf(buffer, "%03d", currentValue);
lcd.print(buffer); // Print the value on the LCD
}
// If the left button is pressed and its old value was LOW
if ((buttonLeftValue == HIGH) && (buttonLeftOldValue == LOW)) {
stateLeft = abs(1 - stateLeft); // Change the state of the left button
delay(50); // Add a delay to avoid accidental multiple presses
}
buttonLeftOldValue = buttonLeftValue;
// If the state of the left button is 1 (pressed)
if (stateLeft == 1) {
currentValue = currentValue << 1; // Perform left shift
if (currentValue > 255) currentValue = 0; // Set the value to 0 if it exceeds 255
digitalWrite(latchPin, LOW); // Set latchPin to LOW
shiftOut(dataPin, clockPin, LSBFIRST, currentValue); // Send the data
digitalWrite(latchPin, HIGH); // Set latchPin to HIGH to transfer the data to the output pins
lcd.setCursor(7, 0);
char buffer[5];
sprintf(buffer, "%03d", currentValue);
lcd.print(buffer);
delay(200); // Add a delay to avoid accidental multiple presses
}
// If the left button is pressed and its old value was LOW
if ((buttonRightValue == HIGH) && (buttonRightOldValue == LOW)) {
stateRight = abs(1 - stateRight); // Change the state of the left button
delay(50); // Add a delay to avoid accidental multiple presses
}
buttonRightOldValue = buttonRightValue;
// If the state of the right button is pressed
if (stateRight == 1) {
currentValue = currentValue >> 1; // Perform left shift
digitalWrite(latchPin, LOW); // Set latchPin to LOW
shiftOut(dataPin, clockPin, LSBFIRST, currentValue); // Send the data
digitalWrite(latchPin, HIGH); // Set latchPin to HIGH to transfer the data to the output pins
lcd.setCursor(7, 0);
char buffer[5];
sprintf(buffer, "%03d", currentValue);
lcd.print(buffer);
delay(200); // add delay
}
}