#define colorBtn 6
#define durationBtn 7
#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", "magenta", "black" // black is just off
};
int delays[] = { 1000, 800, 600, 400, 200 };
int currentColorIndex = 0; // Initially white
int currentDelayIndex = 0; // Initially 1000 ms
// Debounce variables
unsigned long lastDebounceTimeColor = 0;
unsigned long lastDebounceTimeDuration = 0;
unsigned long debounceDelay = 50; // 50ms debounce delay
bool colorBtnState = HIGH;
bool durationBtnState = HIGH;
void setup() {
// Set LED pins as output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Set button pins as input with pull-up resistors
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
// Initialize serial communication
Serial.begin(9600);
// Initial LED state
updateLED();
Serial.print("Color: ");
Serial.println(colorNames[currentColorIndex]);
Serial.print("Duration: ");
Serial.print(delays[currentDelayIndex]);
Serial.println(" ms");
}
void loop() {
// Blink the LED with the current color and duration
static unsigned long lastMillis = 0;
if (millis() - lastMillis >= delays[currentDelayIndex]) {
lastMillis = millis();
toggleLED();
}
// Read button states
bool colorBtnReading = digitalRead(colorBtn) == LOW; // LOW when pressed
bool durationBtnReading = digitalRead(durationBtn) == LOW; // LOW when pressed
// Handle color button press with debounce
if (colorBtnReading != colorBtnState) {
// If button state changed, check debounce time
if (millis() - lastDebounceTimeColor > debounceDelay) {
if (colorBtnReading == LOW) {
// Button is pressed, change color
changeColor();
}
lastDebounceTimeColor = millis(); // Update debounce time
}
colorBtnState = colorBtnReading; // Store the current button state
}
// Handle duration button press with debounce
if (durationBtnReading != durationBtnState) {
// If button state changed, check debounce time
if (millis() - lastDebounceTimeDuration > debounceDelay) {
if (durationBtnReading == LOW) {
// Button is pressed, change duration
changeDuration();
}
lastDebounceTimeDuration = millis(); // Update debounce time
}
durationBtnState = durationBtnReading; // Store the current button state
}
}
void updateLED() {
// Update the RGB LED based on the current color
digitalWrite(redPin, colors[currentColorIndex][0]);
digitalWrite(greenPin, colors[currentColorIndex][1]);
digitalWrite(bluePin, colors[currentColorIndex][2]);
}
void toggleLED() {
// Toggle the RGB LED on and off
static bool ledState = false;
ledState = !ledState;
if (ledState) {
updateLED();
} else {
// Turn off the LED (black)
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
void changeColor() {
currentColorIndex = (currentColorIndex + 1) % 8; // Cycle through 8 colors (including black)
updateLED();
Serial.print("Color: ");
Serial.println(colorNames[currentColorIndex]);
}
void changeDuration() {
currentDelayIndex = (currentDelayIndex + 1) % 5; // Cycle through 5 durations
Serial.print("Duration: ");
Serial.print(delays[currentDelayIndex]);
Serial.println(" ms");
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6