Problem with timerinterrupt on Arduino uno

Hello,

i just try to toggle a variable every second (or so) to update a row on a lcd display. The lcd display is working very good, although i can´t update it in the ISR :-( So i have to toogle a boolean value in the ISR and test outside, if it was toogle inside the ISR and, on behalf of the toggle, write something to the lcd.

Here is the timer part:

volatile unsigned long prog_spalte=20;

...

 Timer1.initialize(1000000);
  Timer1.attachInterrupt(callback);
  if (gebeaus == NULL)
  {
     lcd.setCursor(0, 3);
     lcd.print("> >");
  }
       lcd.setCursor(0, 3);
       lcd.print (prog_spalte);
}


void callback()
{
  prog_spalte++;
  gebeaus = !gebeaus;
  
//  lcd.setCursor(0, 3);  // Not working INDSIDE ISR
//  lcd.print("> >");
}

But the ISR is reached exactly one tie and then, it will be never touched, since the value of "prig_spalte" remains "21" (20 for the initialization and one plus because the ISR is exactly reached one time.

Maybe i have an error in reasoning, i searched the net, but all examples are the same as mine. But something must be wrong...

regards

  gebeaus = !gebeaus;

Is gebeaus volatile?

Given that snippet, I can't see how this will ever evaluate to true:

  if (gebeaus == NULL)
  {
     lcd.setCursor(0, 3);
     lcd.print("> >");
  }

gebeaus is not a pointer, so it can't be NULL.

Sorry, sorry, PaulS, i missed the code piece, yes, gebeaus is volatile:

volatile bool gebeaus;

regards

Something i do not understand, if i run the LED 13 example for the arduino, the timer interrupt is working flawless:

#include "TimerOne.h"  
int ledpin=13;
//int alle_x_sekunden=1;

void setup()   {
               pinMode(ledpin, OUTPUT); 
               Timer1.initialize(1000000);
               Timer1.attachInterrupt(blinken);
               }


void blinken() {
               digitalWrite(ledpin, digitalRead(ledpin) ^ 1);
               }


void loop()    {
// ...
               }

Only my code is not running, or not updating or not toggling the bool variable...

p.s.:

PaulS, you are right, i corrected the request for "gebeaus" to "if (gebeaus==false)"

Ok, i let the led 13 blink in the callback ISR and the led is blinking on the arduino board, so i know that the ISR on the arduino is working correct.

But the bool variable "gebeaus" is not toggling and the "prog_spalte" is not updating, so there is an other drawback, it seems, that the global variables are not updated in the ISR of the arduino although they are declared as "volatile".

regards

But the bool variable "gebeaus" is not toggling and the "prog_spalte" is not updating, so there is an other drawback, it seems, that the global variables are not updated in the ISR of the arduino although they are declared as "volatile".

Well, we sure as hell don't know what your code is doing, having seen only an incomplete snippet. Maybe they could help you at http://snippets-r-us.com.

Thank you PaulS,

ok, i will try it also at http://snippets-r-us.com/, seems to be an helpful site.

regards

Why do you need an interrupt of any description to do something once per second? Why not just use millis() and the technique in Blink Without Delay example.

Interrupts are only necessary when timing must be accurate to a few microseconds.

...R

Hello Robin2,

i would like to set variables asynchron and i would have the main loop as clean as possible. The millis example is ok, but i think, i need more interrupt, and besides of that, i owner, how interrupts are handled on the arduino.

regards

and i would have the main loop as clean as possible.

Like so?

void loop()
{
   doAllTheUglyStuff();
}

void doAllTheUglyStuff()
{
   // All the complicated, hairy, ugly, poorly written crap goes here
}

Wondering how interrupts work is good. Bastardizing your code, adding unnecessary complexity, is not.