const int buttonPin = A5;
const int ledPin1 = 5;
const int ledPin2 = 6;
int mode = 0;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long previousMillis = 0;
unsigned long interval = 500;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read the button with debouncing
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
mode = (mode + 1) % 4; // Cycle between 0, 1, 2
Serial.print("Switched to mode: ");
Serial.println(mode);
}
}
}
lastButtonState = reading;
// Choose which pattern to run
switch (mode) {
case 0:
patternLeftRight();
break;
case 1:
patternSlowFade();
break;
case 2:
patternCaliLight();
break;
case 3:
pattern4();
break;
}
}
// A non-blocking version of leftRight()
void patternLeftRight() {
static bool state = false;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
state = !state;
if (state) {
analogWrite(ledPin1, 255);
analogWrite(ledPin2, 0);
} else {
analogWrite(ledPin1, 0);
analogWrite(ledPin2, 255);
}
}
}
void patternCaliLight() {
static int step = 0; // Current step in the pattern
static unsigned long previousMillis = 0; // Time of last step
const unsigned long interval = 80; // Delay between steps (replace delay(80))
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Example caliLight logic:
if (step < 18) {
if (step % 2 == 0) {
if (step < 10) {
analogWrite(6, 255); // first 9 blinks
} else {
analogWrite(5, 255); // next 9 blinks
}
} else {
analogWrite(5, 0);
analogWrite(6, 0);
}
step++;
} else {
step = 0; // Reset pattern
}
}
}
void patternSlowFade() {
static unsigned long previousMillis = 0;
const unsigned long interval = 2; // Replace delay(2)
static int brightness = 0;
static int direction = 1; // 1 = increasing, -1 = decreasing
static int currentLed = 5; // Start with LED on pin 5
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Write the brightness to the current LED
analogWrite(currentLed, brightness);
// Update brightness
brightness += direction;
// Reverse at limits
if (brightness >= 50 || brightness <= 0) {
direction = -direction;
// If we've completed fade in + out, move to next LED
if (brightness <= 0) {
if (currentLed == 5) {
currentLed = 6;
} else {
currentLed = 5;
}
}
}
}
}
void pattern4() {
static bool state = false;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
state = !state;
if (state) {
analogWrite(ledPin1, 255);
analogWrite(ledPin2, 255);
} else {
analogWrite(ledPin1, 0);
analogWrite(ledPin2, 0);
}
}
}