#define colorBtn 6
#define durationBtn 7
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
};
int currentColor = 0;
int currentDuration = 0;
int lastChange = 0;
bool glow = 1;
int debounceDelay = 300;
int lastBtnPressed = 0;
void setup() {
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
attachInterrupt(digitalPinToInterrupt(colorBtn), colorChange, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), durationChange, FALLING);
digitalWrite(13, colors[currentColor][0]);
digitalWrite(12, colors[currentColor][1]);
digitalWrite(11, colors[currentColor][2]);
Serial.begin(9600);
}
void colorChange() {
if (millis()-lastBtnPressed > debounceDelay){
currentColor = (currentColor+1)%7;
Serial.print("Color: ");
Serial.println(colorNames[currentColor]);
lastBtnPressed = millis();
}
}
void durationChange(){
if (millis()-lastBtnPressed > debounceDelay){
currentDuration = (currentDuration+1)%5;
Serial.print("Duration: ");
Serial.print(delays[currentDuration]);
Serial.println(" ms");
lastBtnPressed = millis();
}
}
void loop() {
if (millis()-lastChange > delays[currentDuration]) {
if (glow == 1) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
glow=0;
}
else {
digitalWrite(13, colors[currentColor][0]);
digitalWrite(12, colors[currentColor][1]);
digitalWrite(11, colors[currentColor][2]);
glow=1;
}
lastChange = millis();
}
delay(10);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6