General Electronics

Automatic Watering System help

Got a general electronics question or something to share? Discuss it here.

Automatic Watering System help

Postby Gav » Mon Jan 17, 2011 4:03 am

Hi Everyone.
I have been a member for a while now and have been quietly programming for about 6 months.
I have been experimenting with all your excellent ideas like Large LCD Font, Rotary switches, Linear light dimmers etc and have recently decided to make an automatic watering system for my new house. Only 5 months old. How hard could it be I said,.....fatal last words.
I know there is "24Timers" by Mitchy but
A: I like learning,
B It’s actually pretty complicated to follow.

My trouble has been that my unorthodox style has led my program to run slower and slower each time I use one of my 4 override timer. (The actual daily timer has not been attempted yet). I am running one 4x20 LCD screen that has 4 separate menu screens. Each menu selectable via a variable resistor.

One of my timers is as follows, there are four of them
Syntax: (code.bas) [ Download ] [ Hide ]
Using swordfish Syntax Highlighting
Sub OverideTimer1()
  If ClearButton = 1
    Sec1 = 0
    Sec11 = 0
    Min1 = 0 Else  
  If IncrementButton = 1
    Sec1 = Sec1 + 60               //Increment 1 minute at a time
    Min1 = Sec1 / 60
    DelayMS(600) Else
  If Sec1 > 0 Then
    Sec1 = Sec1 - 1               // Count down
    Min1 = Sec1 / 60              // Used to display minutes
    Sec11 = Sec1 - (Min1 * 60)    // Used to display seconds
    DelayMS(1000)                 // Count down one second at a time
  EndIf
    EndIf
      EndIf
 
End Sub  
 
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


Example of one of the four selectable menu screens. menu
Syntax: (code.bas) [ Download ] [ Hide ]
Using swordfish Syntax Highlighting
If ADVal0 >=0 And ADVal0 <=2 Then
LCD.WriteAt(1,1,"  Front Lawn Timer  ")
LCD.WriteAt(2,1," Overide",DecToStr(Min1,2),":",DecToStr(Sec11,2),"Sec    ")        
End If    
 
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

The program does have a real time clock running in the back ground (Zero-Sum/Zero Cumulative Error Method by Warren Schroeder alias xor) - To be used later. Does not seem to affect speed of opperation.

If anyone can give a simple example of multiple countdown timers (other than "24Timers") that runs smoothly I would be grateful. - GAV .... :D
User avatar
Gav
New Member
New Member
 
Posts: 4
Joined: July 2009
Location: New South Wales - Australia
Has thanked: 3 times
Have thanks: 0 time

Re: Automatic Watering System help

Postby Graham Mitchell » Tue Jan 18, 2011 6:22 am

Thanks for the code Gav. Below is a module that can be bolted onto your original program - it controls four timers with definable intervals etc (the code is untested, should work).

The timers will only count until the set interval is reached, and then stop. It's more of a how-to that should give you some ideas on timer control.

You might notice the timers work in seconds (for example, a value of 3601 is 1 hour and 1 second). There is a sub routine that shows how you can convert the seconds into normal time values (hours/mins/secs). Search for the term [newcode] to find my additions in the original program.

Syntax: (code.bas) [ Download ] [ Hide ]
Using swordfish Syntax Highlighting
{
****************************************************************
*  Name    : Soft_RTC_FreeRunTimer.BAS                         *
*  Author  : Warren Schroeder alias "xor"                      *
*  Notice  : Copyright (c) 2007 Warren Schroeder               *
*          : All Rights Reserved                               *
*  Date    : 5/23/2007                                         *
*  Version : 1.0                                               *
*  Notes   : Real Time Clock - Free Running Timer              *
*            Precision 24H Time Clock Example on LCD           *
*            18F452 @ 8MHz                                     *
*            Timer0 and No Prescaler                           *
*  Program implements Zero-Sum/Zero Cumulative Error Method    *
*  by Roman Black .. http://www.romanblack.com/one_sec.htm     *:                                                  
****************************************************************
}


Device = 18F452
Clock = 8

#option LCD_RS = PORTB.2
#option LCD_EN = PORTB.3
#option LCD_DATA = PORTB.4

Include "lcd.bas"
Include "convert.bas"
Include "GavsTimers"                      ' [newcode]

{
   For One Second Update:
   
   8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
   Use 8-bit Timer0; No Prescaler
   2000000 counts = 1 Second
   Timer0 reload requires 2 cycle compensation = 2000000-2
}

   
Const OneSecond As LongWord = 2000000-2   ' 1 Second total clock cycles - 2 for timer relaod
Dim Cyc_Counter As LongWord Absolute $30  ' Timer0 cycle counter
Dim T0Load As Byte Absolute $30           ' Timer0 reload value
Dim Countdown As Word Absolute $31        ' # of 8-bit cycles
Dim update As Boolean
Dim secs,mins,hrs As Byte

Interrupt RTC()
   Dec(Countdown)
   If Countdown = 0 Then
      Cyc_Counter = OneSecond             ' reload timer
      TMR0L = TMR0L + T0Load              ' include additional TMR0 counts
      If STATUS.0 = 0 Then
         Inc(Countdown)                   ' if no carry increment "countdown" counter once
      End If
      update = true                       ' flag to update clock output
   End If
   INTCON.2 = 0                           ' clear interrupt flag  
End Interrupt

Sub Clock24()
 Dim clk As String
    Inc(secs)
    If secs = 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
       If mins = 60 Then
          mins = 0
          Inc(hrs)
          If hrs = 24 Then
             hrs = 0
          End If
       End If
    End If
    clk = DecToStr(hrs,2)                 ' output to LCD
    LCD.WriteAt(2,5,clk)
    clk = DecToStr(mins,2)
    LCD.WriteAt(2,8,clk)
    clk = DecToStr(secs,2)
    LCD.WriteAt(2,11,clk)
    update = false
End Sub

Sub Initialize()
   ADCON1 = 15
   secs = 0
   mins = 0
   hrs = 0
   Cyc_Counter = OneSecond
   update = false
   LCD.Command(130)
   LCD.Write("24-HOUR CLOCK")
   LCD.Command(196)
   LCD.Write("00:00:00")      
   INTCON = 192                           ' enable GIE & PEIE
   T0CON = 72                             ' 8-bit; no prescaler; Timer0 off
   TMR0L = 0                              ' clear timer0
   INTCON.5 = 1                           ' interrupt enabled
   INTCON.2 = 0                           ' interrupt flag cleared
   T0CON.7 = 1                            ' start Timer0
   Enable(RTC)                            ' enable jump to RTC ISR
End Sub

// main program start
Initialize

// configure custom timers [newcode]
Timer(0).Enabled = 1
Timer(1).Enabled = 1
Timer(2).Enabled = 1
Timer(3).Enabled = 1
Timer(0).IntervalSeconds = 30
Timer(1).IntervalSeconds = 45
Timer(2).IntervalSeconds = 60
Timer(3).IntervalSeconds = 75

While 1=1
  If update = true Then
     Clock24                              ' update 24H Clock output
     UpdateTimers                         ' update the custom timers [newcode]    
  End If
Wend
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4

And the module (save it in the same folder as your main program)
Syntax: (GavsTimers.bas) [ Download ] [ Hide ]
Using swordfish Syntax Highlighting
Module GavsTimers

Const NumberOfTimers = 4

// structure of timer variables
Structure TTimer
    ElapsedSeconds As LongWord
    IntervalSeconds As LongWord
    Flags As Byte
    Enabled As Flags.0
    Complete As Flags.1
End Structure
Public Dim Timer(NumberOfTimers) As TTimer

// reset each timer
Public Sub ClearTimers()
    Dim i As Byte
   
    For i = 0 To Bound(Timer)
        Timer(i).ElapsedSeconds = 0
        Timer(i).IntervalSeconds = 0
        Timer(i).Enabled = 0
        Timer(i).Complete = 0
    Next
End Sub

// check each timer to see if interval period has elapsed
Sub CheckTimers()
    Dim i As Byte
   
    For i = 0 To Bound(Timer)
        If Timer(i).Enabled = 1 Then
            If Timer(i).ElapsedSeconds = Timer(i).IntervalSeconds Then
                Timer(i).Complete = 1
            EndIf  
        EndIf
    Next
End Sub

// increment each timer, if it's enabled
Sub IncrementTimers()
    Dim i As Byte
   
    For i = 0 To Bound(Timer)
        If Timer(i).Enabled = 1 Then
            If Not Timer(i).Complete = 1 Then
                Timer(i).ElapsedSeconds = Timer(i).ElapsedSeconds + 1            
            EndIf
        EndIf
    Next
End Sub

// update and check timer intervals
Public Sub UpdateTimers()
    IncrementTimers
    CheckTimers
End Sub

// convert the number of seconds into hours, minutes and seconds.
// ByVal pRawSeconds as LongWord - total number of seconds (0 - 921600)
// ByRef pHours as Byte as Byte - target variable to store number of hours (0-255)
// ByRef pMinutes as Byte as Byte - target variable to store number of minutes (0-59)
// ByRef pSeconds as Byte as Byte - target variable to store number of seconds (0-59)
Public Sub ConvertSecondsToTime(ByVal pRawSeconds As LongWord, ByRef pHours As Byte, ByRef pMinutes As Byte, ByRef pSeconds As Byte)
    // reset counters
    pSeconds = 0
    pMinutes = 0
    pHours = 0
    // calculate hours
    While pRawSeconds >= 3600
        pRawSeconds = pRawSeconds - 3600
        pHours = pHours + 1
    Wend
    // calculate minutes
    While pRawSeconds >= 60
        pRawSeconds = pRawSeconds - 60
        pMinutes = pMinutes + 1
    Wend
    // calculate seconds (whatever was left-over )
    pSeconds = pRawSeconds    
End Sub

// reset all timer variables upon powerup
ClearTimers
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
- the admin guy

Follow Digital DIY via Facebook | Twitter | Newsletter | RSS Articles | RSS Forum

For site related queries/issues, please contact me via (if you're logged in, PM is fine)

For this message the author Graham Mitchell has received thanks:
Gav
User avatar
Graham Mitchell
Site Admin
Site Admin
 
Posts: 1471
Joined: April 2009
Has thanked: 214 times
Have thanks: 78 times

Re: Automatic Watering System help

Postby MrDEB » Tue Jan 18, 2011 6:56 am

kinda confussed about Grahams CONST array
in the code he has
"CONST numberoftimers=4"
does he not have to define what each numberoftimers in the array is?
I looked but failed to see??
just curious as studying other peoples codes may help me to learn proper code writing.
also in the main program loop the 4 timers are enabled all the time?
User avatar
MrDEB
Valued Member
Valued Member
 
Posts: 1361
Joined: April 2009
Has thanked: 6 times
Have thanks: 17 times

Re: Automatic Watering System help

Postby Graham Mitchell » Tue Jan 18, 2011 7:09 am

Hi MrDEB,

The module is by no means complete, rather it touches on a few expandable concepts. "Const NumberOfTimers = 4" defines the number of timers that the module will service. If Gav had 6 timers for example, the definition could be changed and the module will accommodate them (reseting/incrementing/checking status etc).

MrDEB wrote:also in the main program loop the 4 timers are enabled all the time?

In the main program all four timers are configured, which includes enabling them. They should start incrementing and then the Timer(i).Complete flag is set when Timer(i).IntervalSeconds has elapsed.

After 75 seconds (the longest defined interval), all the Timer(i).Complete flags will be set. How Gav expands on this is really up to him.. As with software RTCs, there's any number of ways to work with timers - I created this one on-the-fly in about 10 minutes.
- the admin guy

Follow Digital DIY via Facebook | Twitter | Newsletter | RSS Articles | RSS Forum

For site related queries/issues, please contact me via (if you're logged in, PM is fine)
User avatar
Graham Mitchell
Site Admin
Site Admin
 
Posts: 1471
Joined: April 2009
Has thanked: 214 times
Have thanks: 78 times

Re: Automatic Watering System help

Postby MrDEB » Tue Jan 18, 2011 7:34 am

My posts keep going nowhere??
second time??
User avatar
MrDEB
Valued Member
Valued Member
 
Posts: 1361
Joined: April 2009
Has thanked: 6 times
Have thanks: 17 times

Re: Automatic Watering System help

Postby MrDEB » Tue Jan 18, 2011 9:55 am

Well lets try again
This automatic irrigation controler is pretty simplistic.
Perhaps take it to a higher level by building a fully AUTOMATIC IRRIGATION CONTROLLER
Taking EVAPORTRANSPIATION data combining soil type, weather history data, present weather data, irrigation efficienty etc then let the controller apply the exact amout of water needed by the desired crop/plant material.
There are controllers on the market that do all of this already but way out of the ballpark for home use.
Data can be downloaded from state weather service and ag dept (farmers as well as golf coarses use this to some extent)
This could be a challenging project which if broken down just involves combining several sensors inputs along with soil percolation rate.
then you have a "plug n water" controller.
User avatar
MrDEB
Valued Member
Valued Member
 
Posts: 1361
Joined: April 2009
Has thanked: 6 times
Have thanks: 17 times

Re: Automatic Watering System help

Postby Graham Mitchell » Tue Jan 18, 2011 2:36 pm

MrDEB wrote:My posts keep going nowhere??

Sorry MrDEB, we had a blow-out on the sites backend.
- the admin guy

Follow Digital DIY via Facebook | Twitter | Newsletter | RSS Articles | RSS Forum

For site related queries/issues, please contact me via (if you're logged in, PM is fine)
User avatar
Graham Mitchell
Site Admin
Site Admin
 
Posts: 1471
Joined: April 2009
Has thanked: 214 times
Have thanks: 78 times


  • Similar topics
    Replies
    Views
    Author

Return to General Electronics