I cannot for the life of me figure out why mode 2 of my code is starting on pin 3 instead of pin 2. I'm sure its something simple that I'm missing. Cany anyone hep?
If I set Line 130 to currentLed = 8;
it will start at pin 2 like I need to, but that doesn't seem appropriate.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
const int buttonPin1 = 12; // Button 1 (Mode Switch)
const int buttonPin2 = 13; // Button 2 (Action Trigger)
const int buttonPin3 = 11; // Button 3 (Increase Delay)
const int buttonPin4 = 10; // Button 4 (Decrease Delay)
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Pin numbers for the 8 LEDs
const int numLeds = 8; // Number of LEDs
// Mode Definitions
enum Mode { MODE1, MODE2, MODE3 };
Mode currentMode = MODE1; // Default to MODE1
// LCD Setup (16x2 with I2C address x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Button State Variables
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int buttonState3 = 0;
int lastButtonState3 = 0;
int buttonState4 = 0;
int lastButtonState4 = 0;
// Timing variables for button debounce
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastDebounceTime4 = 0;
unsigned long debounceDelay = 50; // Debounce time (ms)
// For Mode 1: Blinking delay
int blinkDelay = 200; // Initial delay for blinking in milliseconds
// For Mode 2: Blink Press
int currentLed = 0; // Start blinking from pin 2
bool buttonPressed = false; // Flag for button press
int mode2Delay = 100; // Initial delay for Mode 2 in milliseconds
// For Mode 3: On/Off Toggle
bool ledsOn = false; // Flag to track LED state for Mode 3
void setup() {
// Initialize pin modes
pinMode(buttonPin1, INPUT_PULLUP); // Set button 1 as input with pull-up
pinMode(buttonPin2, INPUT_PULLUP); // Set button 2 as input with pull-up
pinMode(buttonPin3, INPUT_PULLUP); // Set button 3 as input with pull-up
pinMode(buttonPin4, INPUT_PULLUP); // Set button 4 as input with pull-up
// Initialize the LED pins as OUTPUT
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize the LCD
lcd.init();
lcd.clear();
lcd.backlight();
displayMode(); // Show initial mode and delay
// Start the serial monitor for debugging
Serial.begin(960);
}
void loop() {
// Read the state of the buttons
int reading1 = digitalRead(buttonPin1);
int reading2 = digitalRead(buttonPin2);
int reading3 = digitalRead(buttonPin3);
int reading4 = digitalRead(buttonPin4);
// Handle Mode Switching (Button 1)
if (reading1 == LOW && (millis() - lastDebounceTime1) > debounceDelay) {
if (lastButtonState1 == HIGH) {
// Change mode
currentMode = static_cast<Mode>((currentMode + 1) % 3); // Cycle through 3 modes
displayMode(); // Update the LCD display with the current mode
}
lastDebounceTime1 = millis(); // Reset debounce timer
}
// Handle Action Trigger (Button 2)
if (reading2 == LOW && (millis() - lastDebounceTime2) > debounceDelay) {
if (lastButtonState2 == HIGH) {
triggerAction(); // Trigger the action for the current mode
}
lastDebounceTime2 = millis(); // Reset debounce timer
}
// Handle Increase Delay (Button 3) for Mode 2
if (reading3 == LOW && (millis() - lastDebounceTime3) > debounceDelay) {
if (lastButtonState3 == HIGH) {
mode2Delay += 50; // Increase mode 2 delay by 50ms
displayMode(); // Update the display
}
lastDebounceTime3 = millis(); // Reset debounce timer
}
// Handle Decrease Delay (Button 4) for Mode 2
if (reading4 == LOW && (millis() - lastDebounceTime4) > debounceDelay) {
if (lastButtonState4 == HIGH && mode2Delay > 50) {
mode2Delay -= 50; // Decrease mode 2 delay by 50ms
displayMode(); // Update the display
}
lastDebounceTime4 = millis(); // Reset debounce timer
}
// Update last button states
lastButtonState1 = reading1;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
lastButtonState4 = reading4;
}
void displayMode() {
// Clear the LCD and show the current mode and delay
lcd.clear();
lcd.print("Mode: ");
switch (currentMode) {
case MODE1:
lcd.print("Blink All");
break;
case MODE2:
lcd.print("Blink Press");
break;
case MODE3:
lcd.print("On/Off");
// Reset LEDs when switching to Mode 3
ledsOn = false; // Reset the state
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW); // Turn off all LEDs
}
break;
default:
lcd.print("Unknown");
Serial.println("Error: Unknown mode encountered.");
break;
}
lcd.setCursor(0, 1);
if (currentMode == MODE1) {
lcd.print("Delay: ");
lcd.print(blinkDelay);
lcd.print(" ms");
} else if (currentMode == MODE2) {
lcd.print("Delay: ");
lcd.print(mode2Delay);
lcd.print(" ms");
} else {
lcd.print("Press Btn2");
}
}
void triggerAction() {
// Perform the action for the selected mode
switch (currentMode) {
case MODE1:
mode1(); // Blinking mode
break;
case MODE2:
mode2(); // Blink on button press mode
break;
case MODE3:
mode3(); // On/Off toggle mode
break;
default:
Serial.println("Error: Unknown mode encountered.");
break;
}
}
// Mode 1: Blinking All LEDs
void mode1() {
Serial.println("Blinking LEDs...");
// Blink LEDs in series
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH); // Turn the current LED ON
delay(blinkDelay); // Wait for the set delay
digitalWrite(ledPins[i], LOW); // Turn the current LED OFF
delay(blinkDelay); // Wait for the set delay before moving to the next LED
}
}
// Mode 2: Blink one LED on each button press
void mode2() {
// Read the current button state
buttonState2 = digitalRead(buttonPin2);
// Check if the button was just pressed (falling edge detection)
if (buttonState2 == LOW && lastButtonState2 == HIGH) {
// Wait for debounce time before processing further
delay(debounceDelay);
// Turn off the current LED
digitalWrite(ledPins[currentLed], LOW);
// Move to the next LED (loop back to after the last LED)
currentLed++;
if (currentLed >= numLeds) {
currentLed = 0; // Reset to the first LED
}
// Turn on the next LED and blink it
digitalWrite(ledPins[currentLed], HIGH);
delay(mode2Delay); // Use mode2Delay for LED blink duration
digitalWrite(ledPins[currentLed], LOW); // Turn it off
buttonPressed = true; // Mark button as pressed to avoid double triggers
} else if (buttonState2 == HIGH) {
// Reset button press flag when button is released
buttonPressed = false;
}
// Update the last button state for button2
lastButtonState2 = buttonState2;
}
// Mode 3: Toggle all LEDs on or off
void mode3() {
// Read the current button state
buttonState2 = digitalRead(buttonPin2);
// Check if the button was just pressed (falling edge detection)
if (buttonState2 == LOW) {
// Toggle the LEDs on or off
ledsOn = !ledsOn;
// Set LEDs based on the toggle state
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], ledsOn ? HIGH : LOW);
}
// Debugging output
Serial.print("Button pressed. LEDs are now: ");
Serial.println(ledsOn ? "ON" : "OFF");
// Wait a bit to allow for the button to be released
delay(200);
}
}