// Define pin constants
const int led1Pin = 2;  // LSB of overflow counter
const int led2Pin = 3;  // MSB of overflow counter
const int buttonPin = 4;
const int bargraphPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
const int numBargraphSegments = 10;

// Variables for tracking state
int counter = 0;
int overflowCounter = 0;
bool buttonState = HIGH;      // Initially not pressed (HIGH with INPUT_PULLUP)
bool lastButtonState = HIGH;  // For debouncing
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 150; // 0.15 seconds for debounce

void setup() {
  // Initialize pins
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);  // Using pull-up resistor
  
  // Initialize all bar graph segments as outputs
  for (int i = 0; i < numBargraphSegments; i++) {
    pinMode(bargraphPins[i], OUTPUT);
  }
  
  // Initialize all components to off state
  updateDisplay();
}

void loop() {
  // Read the current button state
  bool reading = digitalRead(buttonPin);
  
  // If button state changed, reset the debounce timer
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  
  // Check if button state has been stable for the debounce period
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If button state has changed since last check
    if (reading != buttonState) {
      buttonState = reading;
      
      // If the button is pressed (LOW with INPUT_PULLUP)
      if (buttonState == LOW) {
        // Increment the counter
        counter++;
        
        // Check for overflow
        if (counter > numBargraphSegments) {
          counter = 0;
          overflowCounter = (overflowCounter + 1) % 4; // Wrap back to 00 after 11
        }
        
        // Update display to reflect new counter values
        updateDisplay();
      }
    }
  }
  
  // Save button state for next loop iteration
  lastButtonState = reading;
}

void updateDisplay() {
  // Update the bar graph segments based on counter value
  for (int i = 0; i < numBargraphSegments; i++) {
    digitalWrite(bargraphPins[i], (i < counter) ? HIGH : LOW);
  }
  
  // Update the LEDs to display overflow counter in binary
  // 00: 0 overflows, 01: 1 overflow, 10: 2 overflows, 11: 3 overflows
  digitalWrite(led1Pin, (overflowCounter & 0x01) ? HIGH : LOW); // Least significant bit
  digitalWrite(led2Pin, (overflowCounter & 0x02) ? HIGH : LOW); // Most significant bit
}
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
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
bargraph1:A1
bargraph1:A2
bargraph1:A3
bargraph1:A4
bargraph1:A5
bargraph1:A6
bargraph1:A7
bargraph1:A8
bargraph1:A9
bargraph1:A10
bargraph1:C1
bargraph1:C2
bargraph1:C3
bargraph1:C4
bargraph1:C5
bargraph1:C6
bargraph1:C7
bargraph1:C8
bargraph1:C9
bargraph1:C10