#define colorBtn 6
#define durationBtn 7
#define RED_PIN 13
#define GREEN_PIN 12
#define BLUE_PIN 11
bool colors[7][3] = {
{1, 1, 1}, // white
{1, 0, 0}, // red
{0, 1, 0}, // green
{0, 0, 1}, // blue
{0, 1, 1}, // cyan
{1, 1, 0}, // yellow
{1, 0, 1} // magenta
};
char* colorNames[] = {
"white", "red", "green", "blue",
"cyan", "yellow", "magenta"
};
int delays[] = {
1000, 800, 600, 400, 200
};
volatile int colorIndex = 0;
volatile int durationIndex = 0;
volatile unsigned long lastColorPress = 0;
volatile unsigned long lastDurationPress = 0;
const unsigned long debounceDelay = 200;
volatile bool printColorFlag = false;
volatile bool printDurationFlag = false;
unsigned long previousMillis = 0;
bool ledState = false;
void handleColorISR() {
unsigned long currentMillis = millis();
if (currentMillis - lastColorPress > debounceDelay) {
colorIndex = (colorIndex + 1) % 7;
lastColorPress = currentMillis;
printColorFlag = true;
}
}
void handleDurationISR() {
unsigned long currentMillis = millis();
if (currentMillis - lastDurationPress > debounceDelay) {
durationIndex = (durationIndex + 1) % 5;
lastDurationPress = currentMillis;
printDurationFlag = true;
}
}
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), handleColorISR, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), handleDurationISR, FALLING);
}
void loop() {
unsigned long currentMillis = millis();
if (printColorFlag) {
printColorFlag = false;
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
}
if (printDurationFlag) {
printDurationFlag = false;
Serial.print("Duration: ");
Serial.print(delays[durationIndex]);
Serial.println(" ms");
}
if (currentMillis - previousMillis >= delays[durationIndex]) {
previousMillis = currentMillis;
ledState = !ledState;
if (ledState) {
digitalWrite(RED_PIN, colors[colorIndex][0] ? HIGH : LOW);
digitalWrite(GREEN_PIN, colors[colorIndex][1] ? HIGH : LOW);
digitalWrite(BLUE_PIN, colors[colorIndex][2] ? HIGH : LOW);
} else {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
}Loading
st-nucleo-c031c6
st-nucleo-c031c6