/*
3-bit Binary Input with Pushbuttons, LEDs and LED Bar Graph
- LED1 (digital pin 22) represents the least-significant bit.
- LED2 (digital pin 23) represents the middle bit.
- LED3 (digital pin 24) represents the most-significant bit.
- Pushbuttons on digital pins 2, 3, and 4 toggle the corresponding bits.
- The binary value is displayed on an 8-segment LED bar graph connected to digital pins 30..37.
- Button presses shorter than 0.15 sec (150ms) are ignored.
*/
const int btn1Pin = 2; // btn1 toggles bit0 & led1
const int btn2Pin = 3; // btn2 toggles bit1 & led2
const int btn3Pin = 4; // btn3 toggles bit2 & led3
const int led1Pin = 22; // LSB
const int led2Pin = 23; // Middle bit
const int led3Pin = 24; // MSB
// Bar graph: we'll use 8 segments (0 to 7) mapped to digital pins 30-37 (segment N maps to binary value N)
const int numSegments = 8;
const int barGraphPins[numSegments] = {30, 31, 32, 33, 34, 35, 36, 37};
unsigned long debounceDelay = 150; // 150ms debounce threshold
// For debouncing each button
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
bool lastBtn1State = HIGH;
bool lastBtn2State = HIGH;
bool lastBtn3State = HIGH;
// Current state of each LED (and corresponding bit)
bool led1State = false; // LSB
bool led2State = false; // Middle bit
bool led3State = false; // MSB
// Function to update the bar graph according to current binary value
void updateBarGraph() {
// Calculate binary value from bits: bit2 bit1 bit0
int binaryValue = (led3State << 2) | (led2State << 1) | led1State;
// Turn off all segments first
for (int i = 0; i < numSegments; i++) {
digitalWrite(barGraphPins[i], LOW);
}
// Light the segment corresponding to binaryValue
// (Assuming that the correct segment is connected to the pin mapped as:
// binary value 0 --> barGraphPins[0] (i.e., segment A1),
// binary value 1 --> barGraphPins[1] (i.e., segment A2), etc.)
if (binaryValue >= 0 && binaryValue < numSegments) {
digitalWrite(barGraphPins[binaryValue], HIGH);
}
}
void setup() {
// Initialize LED pins as outputs; start with off.
pinMode(led1Pin, OUTPUT);
digitalWrite(led1Pin, LOW);
pinMode(led2Pin, OUTPUT);
digitalWrite(led2Pin, LOW);
pinMode(led3Pin, OUTPUT);
digitalWrite(led3Pin, LOW);
// Initialize pushbutton pins as inputs with internal pullup resistor.
pinMode(btn1Pin, INPUT_PULLUP);
pinMode(btn2Pin, INPUT_PULLUP);
pinMode(btn3Pin, INPUT_PULLUP);
// Initialize bar graph segment pins as outputs; ensure all off.
for (int i = 0; i < numSegments; i++) {
pinMode(barGraphPins[i], OUTPUT);
digitalWrite(barGraphPins[i], LOW);
}
}
void loop() {
unsigned long currentTime = millis();
// Read the current state of each button (LOW means pressed)
bool currentBtn1State = digitalRead(btn1Pin);
bool currentBtn2State = digitalRead(btn2Pin);
bool currentBtn3State = digitalRead(btn3Pin);
// Check btn1: toggle bit0 & led1 if button pressed and debounce time passed.
if (lastBtn1State == HIGH && currentBtn1State == LOW) { // Button press detected (transition HIGH->LOW)
if (currentTime - lastDebounceTime1 > debounceDelay) {
led1State = !led1State; // toggle the state
digitalWrite(led1Pin, led1State ? HIGH : LOW);
lastDebounceTime1 = currentTime;
updateBarGraph(); // update bar graph display after a state change
}
}
lastBtn1State = currentBtn1State;
// Check btn2: toggle bit1 & led2
if (lastBtn2State == HIGH && currentBtn2State == LOW) {
if (currentTime - lastDebounceTime2 > debounceDelay) {
led2State = !led2State;
digitalWrite(led2Pin, led2State ? HIGH : LOW);
lastDebounceTime2 = currentTime;
updateBarGraph();
}
}
lastBtn2State = currentBtn2State;
// Check btn3: toggle bit2 & led3
if (lastBtn3State == HIGH && currentBtn3State == LOW) {
if (currentTime - lastDebounceTime3 > debounceDelay) {
led3State = !led3State;
digitalWrite(led3Pin, led3State ? HIGH : LOW);
lastDebounceTime3 = currentTime;
updateBarGraph();
}
}
lastBtn3State = currentBtn3State;
// (Optional small delay so the loop does not run too fast)
delay(10);
}