Go Down

Topic: Arduino Analog Interrupt (Read 2477 times) previous topic - next topic


I am trying to implement an interrupt on digital pin 2 that will record values from analog Pins, however when value on digital pin 2 changes from "1" to "0" the Serial output stops and nothing seems to get recorded (Penny, Dime, Quarter, Nickel) so I cant tell if anything is actually happening. Is there something wrong with my approach?

Code: [Select]
int dime = 0;
int penny = 0;
int nickel = 0;
int quarter = 0;

int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;

void setup()
 pinMode(2, INPUT);

void loop()
 attachInterrupt(0, analog, CHANGE);

void analog()
 while(digitalRead(2) == LOW)
   dime = analogRead(analogPin0);
   penny = analogRead(analogPin1);
   nickel = analogRead(analogPin2);
   quarter = analogRead(analogPin3);
   if(dime > 20)
   if(penny > 20)
   if(nickel > 20)
   if(quarter > 20)


Using delay() inside an ISR is very much bad.  Specifically it relies on interrupts, which IIRC are disabled while inside an ISR.  ISRs need to be fast fast fast, in and out, to keep from causing problems elsewhere.

Use the ISR to set a global flag, then check that flag's presence inside loop().



Thanks, kg4wsv I think i got it working now  :)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131