// Pin definitions
const int led1Pin = 2;
const int led2Pin = 3;
const int led3Pin = 4;
const int potPin = A0;
// Shift register pins for 7-segment display 1 (tens digit)
const int sr1DataPin = 8;
const int sr1ClockPin = 9;
const int sr1LatchPin = 10;
// Shift register pins for 7-segment display 2 (units digit)
const int sr2DataPin = 11;
const int sr2ClockPin = 12;
const int sr2LatchPin = 13;
// Segment patterns for digits 0-9 (for common anode, 0 = ON, 1 = OFF)
// Segments: A, B, C, D, E, F, G, DP (in that order)
const byte digitPatterns[10] = {
B00000011, // 0: All segments except G and DP
B10011111, // 1: B, C only
B00100101, // 2: A, B, D, E, G
B00001101, // 3: A, B, C, D, G
B10011001, // 4: B, C, F, G
B01001001, // 5: A, C, D, F, G
B01000001, // 6: A, C, D, E, F, G
B00011111, // 7: A, B, C
B00000001, // 8: All segments except DP
B00001001 // 9: A, B, C, D, F, G
};
int lastMappedValue = -1;
unsigned long lastChangeTime = 0;
const unsigned long minStateTime = 2000; // 2 seconds in milliseconds
void setup() {
// Initialize LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
// Initialize shift register pins as outputs
pinMode(sr1DataPin, OUTPUT);
pinMode(sr1ClockPin, OUTPUT);
pinMode(sr1LatchPin, OUTPUT);
pinMode(sr2DataPin, OUTPUT);
pinMode(sr2ClockPin, OUTPUT);
pinMode(sr2LatchPin, OUTPUT);
// Initialize all LEDs to OFF
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
// Display "00" on the 7-segment displays
displayDigit(0, sr1DataPin, sr1ClockPin, sr1LatchPin); // Tens digit
displayDigit(0, sr2DataPin, sr2ClockPin, sr2LatchPin); // Units digit
}
void loop() {
// Read the potentiometer value
int potValue = analogRead(potPin);
// Map the potentiometer value (0-1023) to a range of 0-99
int mappedValue = map(potValue, 0, 1023, 0, 99);
// Check if the value has changed and if enough time has passed since the last change
if (mappedValue != lastMappedValue && (millis() - lastChangeTime >= minStateTime || lastMappedValue == -1)) {
lastMappedValue = mappedValue;
lastChangeTime = millis();
// Update the 7-segment displays
int tensDigit = mappedValue / 10;
int unitsDigit = mappedValue % 10;
displayDigit(tensDigit, sr1DataPin, sr1ClockPin, sr1LatchPin); // Tens digit
displayDigit(unitsDigit, sr2DataPin, sr2ClockPin, sr2LatchPin); // Units digit
// Update the LEDs based on the mapped value
if (mappedValue >= 0 && mappedValue <= 33) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
} else if (mappedValue >= 34 && mappedValue <= 66) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
} else if (mappedValue >= 67 && mappedValue <= 99) {
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
}
}
}
void displayDigit(int digit, int dataPin, int clockPin, int latchPin) {
// Ensure digit is between 0 and 9
if (digit < 0) digit = 0;
if (digit > 9) digit = 9;
// Get the pattern for the digit
byte pattern = digitPatterns[digit];
// Send the pattern to the shift register
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, pattern);
digitalWrite(latchPin, HIGH);
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
sevseg1:COM.1
sevseg1:COM.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
sr1:Q1
sr1:Q2
sr1:Q3
sr1:Q4
sr1:Q5
sr1:Q6
sr1:Q7
sr1:GND
sr1:Q7S
sr1:MR
sr1:SHCP
sr1:STCP
sr1:OE
sr1:DS
sr1:Q0
sr1:VCC
sevseg2:COM.1
sevseg2:COM.2
sevseg2:A
sevseg2:B
sevseg2:C
sevseg2:D
sevseg2:E
sevseg2:F
sevseg2:G
sevseg2:DP
sr2:Q1
sr2:Q2
sr2:Q3
sr2:Q4
sr2:Q5
sr2:Q6
sr2:Q7
sr2:GND
sr2:Q7S
sr2:MR
sr2:SHCP
sr2:STCP
sr2:OE
sr2:DS
sr2:Q0
sr2:VCC
pot1:VCC
pot1:SIG
pot1:GND