Good morning,
I am working on my first project and one of the things I am learning is interrupts. Specifically an interrupt from a button. I have connected up four LEDs on a breadboard to an Arduino Mega 2560. The sketch is simple enough. There are two patterns to light up the LEDs. One goes through each LED individually and the second pattern does both red LEDs at once and then switches to both green LEDs at once. Just something simple. I connected a button as an interrupt and this is meant to switch between patterns.
The basic premise works. The sketch starts out with one pattern and on the button push switches to the other pattern. However, the issue that I am running into is that after a short period one of the patterns gets "stuck" for some reason. You can view the issue in the video and the sketch is below as well as the serial output.
Another issue I am having is the initial serial output. For some reason the output is garbled and I have no idea why.
So is something wrong with the sketch or is there something possibly wrong with my Arduino? Thanks in advance for your time and advice.
#define RED1 9
#define GREEN1 7
#define RED2 5
#define GREEN2 3
volatile int sequence = 0;
long last = 0;
volatile long last_used_millis = 0;
int delay_seconds = 1;
int sequence_number = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(GREEN1, OUTPUT);
pinMode(GREEN2, OUTPUT);
attachInterrupt(2, button, FALLING);
Serial.println("Starting Program.............");
}
void loop() {
int current_millis = millis();
if(sequence == 1){
if(current_millis - last_used_millis > (delay_seconds * 500)){
switch(sequence_number){
case 0:
digitalWrite(GREEN2, LOW);
digitalWrite(RED1, HIGH);
sequence_number = 1;
break;
case 1:
digitalWrite(RED1, LOW);
digitalWrite(GREEN1, HIGH);
sequence_number = 2;
break;
case 2:
digitalWrite(GREEN1, LOW);
digitalWrite(RED2, HIGH);
sequence_number = 3;
break;
case 3:
digitalWrite(RED2, LOW);
digitalWrite(GREEN2, HIGH);
sequence_number = 0;
break;
}
last_used_millis = current_millis;
}
}else{
if(current_millis - last_used_millis > (delay_seconds * 1000)){
switch(sequence_number){
case 0:
digitalWrite(GREEN1, LOW);
digitalWrite(GREEN2, LOW);
digitalWrite(RED1, HIGH);
digitalWrite(RED2, HIGH);
sequence_number = 1;
break;
case 1:
digitalWrite(RED1, LOW);
digitalWrite(RED2, LOW);
digitalWrite(GREEN1, HIGH);
digitalWrite(GREEN2, HIGH);
sequence_number = 0;
break;
default:
digitalWrite(GREEN1, LOW);
digitalWrite(GREEN2, LOW);
digitalWrite(RED1, HIGH);
digitalWrite(RED2, HIGH);
sequence_number = 1;
break;
}
last_used_millis = current_millis;
}
}
}
void button(){
long current_millis = millis();
if(current_millis - last > 1000){
Serial.println("Button pressed");
Serial.println("Current Millis is: "+String(current_millis));
Serial.println("Last Millis is: "+String(last));
Serial.println("Difference is: "+String(current_millis-last));
if(sequence == 1){
sequence = 0;
Serial.println("Sequence 0");
}else{
sequence = 1;
Serial.println("Sequence 1");
}
last = current_millis;
}
}
Serial Output: