Tried to make a counter using the internal arduino interupt function

Hello,
I tried to programm a counter using the internal interupt function with the libary TimerOne. I tried to check my counter int with println in serial monitor but it always just gives out 140.
I am quite new to programming so i lack the expertise to analyse if i produced spagetthi code or the internal interupt function isnt fitting for this kind of task. I am also open for other suggestion how to implement another counter type algorythmen based on the arduino hardware.

As stated in the code i want to use it to transfer it to hours and days displaying on my lcd-display though i need a variable which can reset the count (days and hours).

#include <TimerOne.h> // by Paul Stoffregen
#include <LiquidCrystal.h> //by Arduino

LiquidCrystal lcd(12,11,5,4,3,2);
unsigned const int Threshhold =350;
unsigned const int Baudrate=9600;
unsigned const int Licht=A0;
const unsigned long zeit=1000000;
  int Tage=0;
  int Stunden=0;
  int Knopf=8;
  
int Reset(int Count)
{return Count=0;}

 volatile int zeitcounter( volatile int Zeitcounter) 
{ return Zeitcounter==Zeitcounter+1;}

void setup(){
  lcd.begin(16,2);
  Serial.begin(Baudrate);
  pinMode(Knopf,INPUT);
  Timer1.initialize(zeit);
Timer1.attachInterrupt((volatile)zeitcounter);

}


void loop(){
int SensorValue=analogRead(Licht);
Serial.print("SensorValue:"); Serial.println(SensorValue);
int printzeit=(volatile)zeitcounter;
Serial.print("zeitcounter:"); Serial.println(printzeit);

if(zeitcounter==3600){
  Stunden=Stunden+1;
  Reset(zeitcounter);
}
if(Stunden==24){
  Tage=Tage+1;
  Reset(Stunden);
}

if (digitalRead(Knopf)==1) {
  Reset(Tage);
  Reset(Stunden);
}

if (SensorValue>=Threshhold) {
  lcd.print("NEED WATER PLS!");
   lcd.setCursor(0,1);
   lcd.print ("Days:"); lcd.print(Tage);lcd.print(" Hours:");lcd.print(Stunden);
}

else {
  lcd.print("Plants Happy <3");
  lcd.setCursor(0,1);
  lcd.print ("Days:"); lcd.print(Tage);lcd.print(" Hours:");lcd.print(Stunden);
}

 delay(500);
 lcd.clear(); 
}

The serial Monitor gives out zeitcounter:140
(Right now i have a light sensor built in which i later want to replace with a moisture sensor when i get the hardware)

Thx for the help in advance

You're printing an address.

zeitcounter is a function (should be void)

Hi,
there are some big misunderstandings in your code :wink:

  • You don't need a timer for your purpose. There is already a time counter: millis()

  • This:

int Reset(int Count)
{return Count=0;}

does not what you intend to do. It simply returns 0 without changing any variable. 'Count' is only a local variable that doesn' exist anymore after return from the function

  • An Interrupt:
 volatile int zeitcounter( volatile int Zeitcounter) 
{ return Zeitcounter==Zeitcounter+1;}

cannot return anything and it cannot contain a calling parameter. And Zeitcounter==Zeitcounter+1 is a comparison which always results to 'false'

if(zeitcounter==3600){

'zeitcounter' is ( the address of ) a function and not a variable.

I think you are confusing functions and variables.

Thank you for your leads i will try to rewrite the code ;D

1 Like

But any suggestion to just add a one to a counter using millis().
I need to have it seperate so i can reset it

Have a variable timeBase and subtract it from the millis() value for the current counter value. Reset your counter by setting timeBase to the current millis() so that the difference restarts at zero.

A seconds counter which resets after 60:

unsigned int zeitcounter;
const unsigned long intervall = 1000;
unsigned long lastIntervallTime;

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 9600 );
}

void loop() {
  // put your main code here, to run repeatedly:
  if ( millis() - lastIntervallTime > intervall ) {
    lastIntervallTime += intervall;
    zeitcounter++;
    Serial.println(zeitcounter);
  }

  if (zeitcounter== 60) zeitcounter = 0;
}

thank you, this solves the issue :smiley: