Pages: [1]   Go Down
Author Topic: Need some help!  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;

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 smiley

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

Cheers!
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

« Last Edit: March 18, 2013, 09:17:47 am by Dulimir » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
my question is where to put the lcdWrite in the code so that it will work.
The usual place is in "loop ()".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Get rid of your interrupt
Yes
Quote
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?)).
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: