Go Down

Topic: Where am I going wrong? (Read 733 times) previous topic - next topic

Seán


You can't use delay() in an ISR because it relies on interrupts for timing and the interrupts are disabled during an ISR.

If you declare 'count' as volatile and just set it to 0 in the ISR then loop() will put the processor back to sleep in 10 seconds.

Code: [Select]
#include <avr/sleep.h>
#include <avr/power.h>

int fsr = 0;
volatile int count = 0;
int analogPin = 0;

[...]

ISR(ANALOG_COMP_vect)
{
  if(analogRead(analogPin) > 100)
  {
    count = 0;
    fsr = analogRead(analogPin);
    Serial.println("The reading is ");
    Serial.println(fsr);
  }
}



Hey, just wanted to let you know this worked a treat. Many thanks!!

Seán
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

Seán

Ah ok, that's handy! Thanks a lot! I'll report back on how I get on.

Thanks again!

Seán
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

wildbill

Ordinarily, the compiler tries to optimize your code for you. That may mean some fairly trivial thing like not bothering to read a variable from RAM because it already has it in a register from previous activity. Alternatively, it may move code out of loops if the calculation doesn't change for each iteration, or ignore if tests because it can calculate the result at compile time.

Volatile warns the compiler that it cannot make assumptions about the variable so labelled because you touch it in an interrupt routine or because it references a memory location that may be changed by other hardware. It tells the compiler: "I don't care what you think you know about what that memory location contains, go and get it again anyway every time you want to use it".

Seán

Hey,

Thanks a lot! I'll give that a go when I get back home.

Can I ask, what does volatile mean in this context?

Thanks.

Seán
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

johnwasser

You can't use delay() in an ISR because it relies on interrupts for timing and the interrupts are disabled during an ISR.

If you declare 'count' as volatile and just set it to 0 in the ISR then loop() will put the processor back to sleep in 10 seconds.

Code: [Select]
#include <avr/sleep.h>
#include <avr/power.h>

int fsr = 0;
volatile int count = 0;
int analogPin = 0;

[...]

ISR(ANALOG_COMP_vect)
{
  if(analogRead(analogPin) > 100)
  {
    count = 0;
    fsr = analogRead(analogPin);
    Serial.println("The reading is ");
    Serial.println(fsr);
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up