Interrupts

Interrupt, Switch Blinking LED from Red to Yellow

Experimented with some of the values.

Tested the button with pin13 before proceeding- button works.

Did not consistently get it to switch colors every time I pressed the button down.

First interrupt attempt (successful), includes questions:

int rLED=9;
int yLED=10;
int ledOn=5;
volatile int switch_color=2;
//see loop for onoff pattern, did other iterations with more complex onoff patterns, made it more difficult to cue the interrupt
int ledDelay=250;
void setup() {
pinMode(rLED,OUTPUT);
pinMode(yLED,OUTPUT);
pinMode(switch_color,INPUT);
attachInterrupt(digitalPinToInterrupt(switch_color),switchLed,FALLING);
//does rising or falling matter? maybe only needed with more than 2
ledOn=rLED;
Serial.begin(9600);
//does this impact the switch back to red from yellow?
}
void switchLed() {
ledOn++;
if (ledOn > yLED) {
ledOn = rLED;
}
}
void loop() {
//red LED turns on and off based on delay variable, then on for delay*10, and off for delay*5 before repeating
digitalWrite(ledOn,HIGH);
delay(ledDelay);
digitalWrite(ledOn, LOW);
delay(ledDelay);
}
int rLED=9; int yLED=10; int ledOn=5; volatile int switch_color=2; //see loop for onoff pattern, did other iterations with more complex onoff patterns, made it more difficult to cue the interrupt int ledDelay=250; void setup() { pinMode(rLED,OUTPUT); pinMode(yLED,OUTPUT); pinMode(switch_color,INPUT); attachInterrupt(digitalPinToInterrupt(switch_color),switchLed,FALLING); //does rising or falling matter? maybe only needed with more than 2 ledOn=rLED; Serial.begin(9600); //does this impact the switch back to red from yellow? } void switchLed() { ledOn++; if (ledOn > yLED) { ledOn = rLED; } } void loop() { //red LED turns on and off based on delay variable, then on for delay*10, and off for delay*5 before repeating digitalWrite(ledOn,HIGH); delay(ledDelay); digitalWrite(ledOn, LOW); delay(ledDelay); }
int rLED=9;
int yLED=10;

int ledOn=5;

volatile int switch_color=2;

//see loop for onoff pattern, did other iterations with more complex onoff patterns, made it more difficult to cue the interrupt
int ledDelay=250;

void setup() {

  pinMode(rLED,OUTPUT);
  pinMode(yLED,OUTPUT);

  pinMode(switch_color,INPUT);
  attachInterrupt(digitalPinToInterrupt(switch_color),switchLed,FALLING);
//does rising or falling matter? maybe only needed with more than 2

  ledOn=rLED;

  Serial.begin(9600);
//does this impact the switch back to red from yellow?

}


void switchLed() {
    ledOn++;
    if (ledOn > yLED) {
        ledOn = rLED;
    }
}


void loop() {
//red LED turns on and off based on delay variable, then on for delay*10, and off for delay*5 before repeating
digitalWrite(ledOn,HIGH);
delay(ledDelay);
digitalWrite(ledOn, LOW);
delay(ledDelay);


}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.