#include <Arduino.h>
#include <Servo.h> // included as allowed even though not used in this project
// Define Arduino pins for sr1 (controls sevseg1)
const int sr1DS = 2; // data
const int sr1SHCP = 3; // shift clock
const int sr1STCP = 4; // storage clock (latch)
// Define Arduino pins for sr2 (controls sevseg2)
const int sr2DS = 5; // data
const int sr2SHCP = 6; // shift clock
const int sr2STCP = 7; // storage clock (latch)
// Define LED PWM pins
const int led1Pin = 10;
const int led2Pin = 11;
// Define potentiometer analog input pins
const int pot1Pin = A0; // controls brightness for led1 (when active)
const int pot2Pin = A1; // controls brightness for led2 (when active)
const int pot3Pin = A2; // controls the number to be displayed (0-99)
// Segment patterns for digits 0-9 for common-anode 7-seg
// (Bit order: DP, G, F, E, D, C, B, A; but we will send in order A, B, C, D, E, F, G, DP)
// In common-anode: a LOW (0) turns ON the segment.
const byte digitPatterns[10] = {
0b11000000, // 0: A,B,C,D,E,F on, G off, DP off
0b11111001, // 1: B,C on
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000 // 9
};
void setup() {
// Initialize shift register pins for sr1
pinMode(sr1DS, OUTPUT);
pinMode(sr1SHCP, OUTPUT);
pinMode(sr1STCP, OUTPUT);
// Initialize shift register pins for sr2
pinMode(sr2DS, OUTPUT);
pinMode(sr2SHCP, OUTPUT);
pinMode(sr2STCP, OUTPUT);
// Initialize LED pins
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// Initialize potentiometer pins (handled by analogRead, no pinMode needed)
// Initialize both 7-segment displays to show "00"
// tens digit = 0 sent via sr1; units digit = 0 via sr2.
updateShiftRegister(sr1DS, sr1SHCP, sr1STCP, digitPatterns[0]);
updateShiftRegister(sr2DS, sr2SHCP, sr2STCP, digitPatterns[0]);
// Turn off both LEDs initially
analogWrite(led1Pin, 0);
analogWrite(led2Pin, 0);
}
void loop() {
// Read potentiometer values.
int pot1Value = analogRead(pot1Pin); // 0...1023 for LED1 brightness
int pot2Value = analogRead(pot2Pin); // 0...1023 for LED2 brightness
int pot3Value = analogRead(pot3Pin); // 0...1023 for display number
// Map pot1 and pot2 values to PWM range (0-255)
int brightness1 = map(pot1Value, 0, 1023, 0, 255);
int brightness2 = map(pot2Value, 0, 1023, 0, 255);
// Map pot3 value to a number from 0 to 99
int number = map(pot3Value, 0, 1023, 0, 99);
int tens = number / 10;
int units = number % 10;
// Even/Odd LED control:
// If even, led1 is active (set to brightness1) and led2 off.
// If odd, led1 off and led2 active (set to brightness2).
if (number % 2 == 0) {
analogWrite(led1Pin, brightness1);
analogWrite(led2Pin, 0);
} else {
analogWrite(led1Pin, 0);
analogWrite(led2Pin, brightness2);
}
// Update the 7-segment displays via the shift registers.
// For sevseg1, send the tens digit pattern using sr1.
updateShiftRegister(sr1DS, sr1SHCP, sr1STCP, digitPatterns[tens]);
// For sevseg2, send the units digit pattern using sr2.
updateShiftRegister(sr2DS, sr2SHCP, sr2STCP, digitPatterns[units]);
delay(50); // small delay to stabilize the readings and updates.
}
// A helper function to update a shift register.
// dataPin: DS pin; clockPin: SHCP; latchPin: STCP; data: byte to shift out.
void updateShiftRegister(int dataPin, int clockPin, int latchPin, byte data) {
// Begin the latch so that outputs do not change until we finish shifting out data.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
// Latch the data to the output pins.
digitalWrite(latchPin, HIGH);
}