I'm trying to do a small test where I am using the ISR on the Arduino Pro Micro it up from sleep mode. I'm using a Force Sense Resistor (FSR, a resistor that changes it's resistance depending on how hard you press it) with a voltage divider circuit with the output of the voltage divider connected to an analog pin and the digital interrupt pin. I'm not able to get the interrupt to trigger no matter what condition I have. I don't believe the Vout on the voltage divider is too low since the ADC registers values between 0 to 1000 (0V to about 4.8V) when the FSR is pressed. Is there anything wrong with my code that is preventing my test from working properly?
uint8_t LED = 9;
uint8_t wakeup = 2;
uint8_t enable = 0;
void Wakeup(void);
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(wakeup, INPUT);
attachInterrupt(digitalPinToInterrupt(1), Wakeup, RISING);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println(analogRead(A2));
if (enable == HIGH)
digitalWrite(LED, HIGH);
else if (enable == LOW)
digitalWrite(LED, LOW);
if (analogRead(A2) < 700)
enable = 0;
}
void Wakeup(void) {
enable = 1;
Serial.println("Interrupt");
}
-
1perhaps you should not be using the serial port pin for the interruptjsotola– jsotola2020年08月23日 05:14:25 +00:00Commented Aug 23, 2020 at 5:14
-
I thought 0 and 1 were serial pins. I'm using digital pin 2Jay– Jay2020年08月23日 05:22:03 +00:00Commented Aug 23, 2020 at 5:22
-
@jsotola it's pro micro with 32U4. it doesn't use pin 2 for serialTirdad the Whitehand– Tirdad the Whitehand2020年08月23日 05:33:32 +00:00Commented Aug 23, 2020 at 5:33
-
please review your codejsotola– jsotola2020年08月23日 07:21:59 +00:00Commented Aug 23, 2020 at 7:21
1 Answer 1
Don't do serial prints inside an ISR.
Your variable
enable
should be declaredvolatile
as it changes inside an ISR. The compiler is entitled to cache the variable value if you don't do that.You are inconsistent in your use of
enable
. You test for LOW or HIGH but set it to 0 or 1. Now they happen to be the same, but someone might wonder. If you are going to test for LOW or HIGH, then assign LOW or HIGH to it.
See my reference question How do interrupts work on the Arduino Uno and similar boards?.
I thought 0 and 1 were serial pins. I'm using digital pin 2
Not in the code you are not:
attachInterrupt(digitalPinToInterrupt(1), Wakeup, RISING);
Do you mean:
attachInterrupt(digitalPinToInterrupt(wakeup), Wakeup, RISING);
It's kind of confusing to have two names which differ only in case.
Explore related questions
See similar questions with these tags.