Go Down

Topic: LCD Value does not getting updatet after 5 seconds ?? (Read 508 times) previous topic - next topic

cola1988

Hey everyone,

got a littler problem with my project. I want to display the rpm on a LCD.
I have a Hall Sensor with works fine. The whole programm works and the RPM gets displayed very well.

But: After some time running (around 5 seconds) the value on the Display does not get updatet anymore. i don't find a solution
Seems that the arduino fails or something..

Please help

Regards Phil



Code: [Select]

// read RPM
#include <LiquidCrystal.h>
int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int led = 13;
LiquidCrystal lcd(12, 11, 5, 4, 3, 8);
 
void setup(){
Serial.begin(9600);
attachInterrupt(0, rpm_fan, FALLING);
pinMode(led, OUTPUT);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
}
void loop(){
if (millis() - lastmillis == 333){ //Uptade every 1/3 second, this will be equal to reading  1/3 frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 6; // Convert frecuency to RPM, note: 6 because 1/3 frequency and multiplied by 2 because of half recolutions 6 = 2 * 3.

if(rpm < 10){
  lcd.clear();
  lcd.setCursor(0, 2);
  lcd.print(rpm);
}
if(rpm > 10){
  lcd.setCursor(0, 2);
  lcd.print(rpm);
}
if(rpm > 100){
  lcd.clear();
  lcd.setCursor(0, 2);
  lcd.print(rpm);
  }
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
   } } // this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan(){
  half_revolutions++;
   }


groundfungus

#1
Feb 22, 2014, 05:29 pm Last Edit: Feb 22, 2014, 05:31 pm by groundfungus Reason: 1
Code: [Select]
int half_revolutions = 0;

variables used in interrupt service routines need to be declared volatile.  And an int might be overflowing, try it with long instead.

volatile int half_revolutions = 0;

cola1988

Hey,

i changed to volatile and long... But the problem is still aviable. hmm

Regards Phil

arloG

It's probably not a good idea to check that the time interval is exactly 333 milliseconds.  Try replacing
if (millis() - lastmillis == 333)
   with
if (millis() - lastmillis >= 333)

I don't know your hardware set-up so I tried using your IF loop condition in this simple code
Code: [Select]

unsigned long lastmillis = 0;
void setup()
{  Serial.begin(9600);
}
void loop()
{  if (millis() - lastmillis >= 333)
  {   Serial.println(lastmillis);
      lastmillis = millis(); // Uptade lasmillis
  }
}


When I used "==" instead of ">=", it latched up at just under 13 seconds.  I'm not sure why I didn't duplicate your 5 seconds.  Maybe it's due to differences in the test configuration or maybe your estimate wasn't precise.  I know there was no need for precision here.

Grumpy_Mike

While
Code: [Select]

detachInterrupt(0);//Disable interrupt when calculating

there is no need to keep attaching and detaching interrupts.
See:-
http://arduino.cc/en/Reference/Interrupts#.Uwl_rl6prdU

cola1988

Hey guys,

problem solved! it was the =>

:) Thanks to you all!

Go Up