I am testing the hardware interrupt on Arduino Uno. I am connecting digitalpin2 to a digital switch. The code supposed to print "True" or "False" on serial monitor each time interrupt is triggered.
But when the digital switch is pressed, the board would just go freeze. I didn't know that my digital switch name is Elsa..
daozui:
I am testing the hardware interrupt on Arduino Uno. I am connecting digitalpin2 to a digital switch. The code supposed to print "True" or "False" on serial monitor each time interrupt is triggered.
But when the digital switch is pressed, the board would just go freeze. I didn't know that my digital switch name is Elsa..
Is there any wrong with the code?
#include <Arduino.h>
#define NOT_AN_INTERRUPT -1
const int switchpin =2;
boolean sw=false;
volatile int count=0;
#include <Arduino.h>
#define NOT_AN_INTERRUPT -1
const int switchpin =2;
volatile boolean sw=false; // everything in the interrupt needs to be volatile or the state of the variable is not verified each time it is used
volatile int count=0;
volatile int trigger = 0;
void setup()
{
Serial.begin(9600);
pinMode(switchpin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(switchpin),Response, LOW);
}
void loop()
{
if(trigger){ // We've been interrupted lets see what happened.
Serial.print("We've been interrupted ");
Serial.print(trigger );
Serial.print(" times, sw = ");
Serial.println((sw)?"True":"False");
trigger = 0;
}
}
void Response() // this is all inside the interrupted so everything in here needs to be fast to prevent issues
{
trigger++; // Let the loop know we've been interrupted.
// noInterrupts(); // Not needed the attach interrupts has already blocked other interrupts.
// a simple way to do what you have below is
// sw = !sw; // in english sw = (not) sw and sw toggles
if(sw)
{
// Serial.println("True"); // Serial print takes a lot of time and has interrupts it needs to handle so avoid using this in an interrupt.
sw=false;
}
else
{
// Serial.println("False"); // Serial print takes a lot of time and has interrupts it needs to handle so avoid using this in an interrupt.
sw=true;
}
// interrupts(); // this is ok but it is called next outside this function so why call it twice
}
const int switchpin =2;
volatile boolean sw=false; // everything in the interrupt needs to be volatile or the state of the variable is not verified each time it is used
volatile int count=0;
volatile int trigger = 0;
void loop()
{
if(trigger){ // We've been interrupted lets see what happened.
Serial.print("We've been interrupted ");
Serial.print(trigger );
Serial.print(" times, sw = ");
Serial.println((sw)?"True":"False");
trigger = 0;
}
}
void Response() // this is all inside the interrupted so everything in here needs to be fast to prevent issues
{
trigger++; // Let the loop know we've been interrupted.
// noInterrupts(); // Not needed the attach interrupts has already blocked other interrupts.
// a simple way to do what you have below is
// sw = !sw; // in english sw = (not) sw and sw toggles
if(sw)
{
// Serial.println("True"); // Serial print takes a lot of time and has interrupts it needs to handle so avoid using this in an interrupt.
sw=false;
}
else
{
// Serial.println("False"); // Serial print takes a lot of time and has interrupts it needs to handle so avoid using this in an interrupt.
sw=true;
}
// interrupts(); // this is ok but it is called next outside this function so why call it twice
}