// Define pins for shift register
#define DATA_PIN 8
#define CLOCK_PIN 9
#define LATCH_PIN 10
// Define pins for potentiometer and switch
#define POTENTIOMETER_PIN A0
#define SWITCH_PIN 11
// Define the number of shift registers
#define NUM_SHIFT_REGISTERS 2
// Global variable to store the LED pattern
uint16_t ledPattern = 0;
// Function to write data to shift registers
void writeData(uint16_t data) {
digitalWrite(LATCH_PIN, LOW);
for (int i = NUM_SHIFT_REGISTERS * 8 - 1; i >= 0; i--) {
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(DATA_PIN, (data >> i) & 0x01);
digitalWrite(CLOCK_PIN, HIGH);
}
digitalWrite(LATCH_PIN, HIGH);
}
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(POTENTIOMETER_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP); // Enable internal pull-up resistor for the switch pin
}
void loop() {
// Check if the switch is pressed
bool switchState = digitalRead(SWITCH_PIN);
if (switchState == LOW) {
// If switch is pressed, turn on the LED bar graph
// Read the value from the potentiometer
int potValue = analogRead(POTENTIOMETER_PIN);
// Map the potentiometer value to the range of LED brightness (0-1023 to 0-1023)
int brightness = map(potValue, 0, 1023, 0, 1023);
// Calculate the number of LEDs to light up based on the brightness
int numLEDs = map(brightness, 0, 1023, 0, 10);
// Create a bit pattern to represent the LED states
ledPattern = 0;
for (int i = 0; i < numLEDs; i++) {
ledPattern |= (1 << i);
}
} else {
// If switch is not pressed, turn off the LED bar graph
ledPattern = 0;
}
// Write the data to the shift registers
writeData(ledPattern);
}