int rgb_pins[] = {9, 10, 11}; // red, green, blue
int button_pin = 7;
// attachInterrupt interrupt triggered on a ... input:
//LOW to trigger the interrupt whenever the pin is low
//CHANGE to trigger the interrupt whenever the pin changes value
//RISING to trigger when the pin goes from low to high
//FALLING for when the pin goes from high to low
// https://wokwi.com/projects/388719555383518209
int button_state_saved = 0;
// volatile bool runAllowed = false;
// bool button_state = 0;
void setup() {
Serial.begin(9600);
// Serial.println(rgb_pins.len);
for (int i = 0; i < 3; i++) {
pinMode(rgb_pins[i], OUTPUT);
}
pinMode(button_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(button_pin), interrupt_handler, RISING);
// attachInterrupt(digitalPinToInterrupt(button_pin), );
}
void interrupt_handler()
{
// button_state_saved = -button_state_saved;
button_state_saved = !button_state_saved;
}
void loop() {
// button_state = digitalRead(button_pin);
read_button();
if (button_state_saved) {
changing_colors();
}
// runAllowed = digitalRead(button_pin);
// if(runAllowed){
// for(int i=0; i<1000; i++){
// Serial.print("i = ");
// Serial.print(i);
// Serial.print(" ");
// Serial.println("Motor running");
// if(not runAllowed){
// Serial.println("Interrupt called");
// break;
// }}}
// Serial.println(button_state);
// if ()
// changing_colors();
}
void read_button() {
int current_button_state = digitalRead(button_pin);
if (button_state_saved != current_button_state) {
button_state_saved = current_button_state;
}
}
void changing_colors() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// if (i == j) {
// digitalWrite(rgb_pins[j], i == j);
// }
// else {
// digitalWrite(rgb_pins[j], i == j);
// }
digitalWrite(rgb_pins[j], i == j);
// digitalWrite(rgb_pins[0], LOW);
// digitalWrite(rgb_pins[1], HIGH);
// digitalWrite(rgb_pins[2], LOW);
// Serial.println(i == j);
}
delay(500);
}
for (int i = 0; i < 3; i++) {
digitalWrite(rgb_pins[i], LOW);
}
}
// void funcStartStop() {
// noInterrupts();
// button_state = !button_state;
// interrupts();
// }
// void switchPressed ()
// {
// if (digitalRead(button_pin) == HIGH)
// digitalWrite (13, HIGH);
// else
// digitalWrite(13, LOW);
// } // end of switchPressed
// void turnonled()
// {
// // button_state_saved = -button_state_saved;
// runAllowed = !runAllowed;
// }