Need some help!

hey guys, i need some help with my code! its the first time im using interupts and im not sure how to write something to the LCD!

Here is my code;

//grow box controller with solenoid, light relay and lcd display

#include <avr/io.h>
#include <avr/interrupt.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(5,7,8,9,10,11);

int valve = 3;

float tempC;

int tempPin = 2;

int reading;

int relay = 2;

int buzzer = 4;

volatile byte seconds;
volatile byte minutes;
volatile byte hours;

void setup()
{
  Serial.begin(9600);
  pinMode(valve, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(buzzer, OUTPUT);
  lcd.begin(16, 2);
  // initialize Timer1
    cli();          // disable global interrupts
    TCCR1A = 0;     // set entire TCCR1A register to 0
    TCCR1B = 0;     // same for TCCR1B
 
    // set compare match register to desired timer count:
    OCR1A = 15624;
    // turn on CTC mode:
    TCCR1B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1024 prescaler:
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
    // enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);
    // enable global interrupts:
    sei();
  
}

void loop()
{
  reading = analogRead(tempPin);
  tempC = (5.0*reading*100)/1023;
  int tempCC round(tempC);
  //LCD DISPLAY CODE
  
  
}

//Timer 1

ISR(TIMER1_COMPA_vect)
{
    seconds++;
    if (seconds == 60)
    {
        seconds = 0;
        minutes++;
        
    }
    else
    {
      
    }
    if (minutes == 60)
    {
      minutes = 0;
      hours++;
      
    }
    else
    {
      
    }
    if (hours >= 6 && hours <= 18)
  {
    digitalWrite(relay, HIGH); 
  }
    else
    {
      digitalWrite(relay, LOW);
    }
    if (hours == 24)
    {
      hours = 0;
    } 
}

What i would like to do is to display the temperature, hours and minutes (no seconds), and the state of the relay and valve - all this one 16x2 LCD :slight_smile:

how do i do this, if its possible at all!

Cheers!

You don't need the interrupt (timer1) for your project, the Arduino system is already having that infrastructure for you (millis() function).

There is absolutely no code in your sketch that would produce output on your LCD. Try with the examples of the LiquidCrystal library and get familiar with it's API.

I know there is no code that would write to the lcd, my question is where to put the lcdWrite in the code so that it will work.
And i do need the interupt timer, as i want the light to come on at certain times of the day. Thats why i have the hours, minutes and seconds.

i know that writing to lcd takes more time than other processes, so puting it in the interupt makes it go nuts after a while - it starts to display wierd and wonderful things, but thats not what i want :slight_smile:

so in short, the timer interupt controls the light, the void loop() will control everything else that is not on a "schedule"

And i do need the interupt timer, as i want the light to come on at certain times of the day.

You're going to have to convince me that an interrupt is necessary, or even desirable.

my question is where to put the lcdWrite in the code so that it will work.

The usual place is in "loop ()".

well the thing is a did have it without the timer interupt, but using delays of 12 hours didnt work out and proved to be unreliable + it was holding up other things that i wanted the program to do.

So the solution was to have the timer interupt control the light (12h on and 12h off) and the loop do everything else.

My problems started when i tried to write to the LCD with the interupt timer.

i just want to know, how can i write to the LCD the hours, minutes, temp and if the light (relay) is on or off :slight_smile:

how can i write to the LCD the hours, minutes, temp and if the light (relay) is on or off

Try here

well the thing is a did have it without the timer interupt, but using delays of 12 hours didnt work out and proved to be unreliable

I don't think that was likely to be the fault of using the "millis" function.

so as i understand it, all the lcdWrite have to be in loop() ?

can i use booleans in the interupt to determine the state and use those with an if statement to print out to the lcd?

Get rid of your interrupt and put everything into the loop(). Take a look at the BlinkWithoutDelay example to get an impression of how to do timed things without using the delay() function.

Get rid of your interrupt

Yes

and put everything into the loop()

No.
You can keep most of the code of your interrupt function, just add the "blink without delay" functionality at the very top, and call the function each time through "loop()".
The hh:mm:ss variables don't need to volatile now, but keep them global for convenience.
"loop()" will get called a lot, and it probably isn't necessary to calculate the temperature every time through (each time you do an "analogRead", it costs you 100us - temperature probably ins't going to change all that much. Maybe update once every five or ten seconds?)).