#include <Arduino.h>
#define colorBtn 6
#define durationBtn 7
// RGB LED pins
#define redPin 13
#define greenPin 12
#define bluePin 11
bool colors[][3] = {
{1,1,1} , {1,0,0},{0,1,0},{0,0,1}, // wrgb
{0,1,1} , {1,1,0},{1,0,1},{0,0,0} // cymk
};
char* colorNames[] = {
"white","red","green","blue",
"cyan","yellow","megenta","black" // black is just off so do not need to toggle to black
};
int delays[] = {
1000, 800, 600, 400, 200
};
// Variables to store the current color and duration indices
int currentColorIndex = 0;
int currentDurationIndex = 0;
// Variables to store the last debounce time for buttons
unsigned long lastDebounceTimeColor = 0;
unsigned long lastDebounceTimeDuration = 0;
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
// Variables to store the LED state and last blink time
bool ledState = LOW;
unsigned long previousMillis = 0;
void setup() {
// Initialize RGB LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Initialize button pins as inputs with internal pull-up resistors
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
// Initialize serial communication for debugging
Serial.begin(9600);
// Print the initial color and duration to the serial monitor
Serial.print("Color: ");
Serial.println(colorNames[currentColorIndex]);
Serial.print("Duration: ");
Serial.print(delays[currentDurationIndex]);
Serial.println(" ms");
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to toggle the LED
if (currentMillis - previousMillis >= delays[currentDurationIndex]) {
previousMillis = currentMillis;
ledState = !ledState;
setColor(ledState);
}
// Check the color button state with debouncing
if (digitalRead(colorBtn) == LOW) {
if (currentMillis - lastDebounceTimeColor >= debounceDelay) {
lastDebounceTimeColor = currentMillis;
currentColorIndex = (currentColorIndex + 1) % 7; // Cycle through colors
Serial.print("Color: ");
Serial.println(colorNames[currentColorIndex]);
}
}
// Check the duration button state with debouncing
if (digitalRead(durationBtn) == LOW) {
if (currentMillis - lastDebounceTimeDuration >= debounceDelay) {
lastDebounceTimeDuration = currentMillis;
currentDurationIndex = (currentDurationIndex + 1) % 5; // Cycle through durations
Serial.print("Duration: ");
Serial.print(delays[currentDurationIndex]);
Serial.println(" ms");
}
}
}
void setColor(bool state) {
digitalWrite(redPin, colors[currentColorIndex][0] && state);
digitalWrite(greenPin, colors[currentColorIndex][1] && state);
digitalWrite(bluePin, colors[currentColorIndex][2] && state);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6