Electronics - PICAXE code

I think I promised I would only write three posts with code.

I think this has to be counted as one of them.

I've been studying hard and also working hard at writing the code for the automatic demand feeder.

I haven't soldered the electronics yet, but I've built each part of it to test it on my temporary breadboard.

This is the bare bones minimum required to make the feeder as lots of the features are not yet complete, and no doubt there are still some errors.

The numbers involved, like the number of feeds per day etc, are very large because I don't feel like waiting for fifteen minutes between tests.

So all the variables are crazy, but from the tests I've done, I think it works.

Even if you have no interest in programming, its worth having a look at it, just to see how your computer actually works.

It looks like this (I'll talk a (very) little about how it works later)...


'-----------About------------------------------------------------------
          'Demand fish feeder ver 02-2011-10-11-1130
'-------------------------------------------------------------------------
'This is an open source project that lives here - http://www.backyardaquaponics.com/forum/viewtopic.php?f=50&t=10587
'and build descriptions that require a lot of photo's live here  -120thingin20years..blogspot.com
'Blame code errors and pecularities on
'BullwinkleII  at backyardaquaponics.com , 120thingsin20years on the PICAXE forum and 'http://ozelecforum.com/, and me at 120thingin20years.blogspot.com ,  
'Special thanks to...
'SABorn at the picaxe forum, and http://ozelecforum.com/
'Steve S at backyardaquaponics.com
'everybody at backyardaquaponics.com, http://ozelecforum.com/, and the picaxe forum 
'Actual PVC feeder device (work in progress (and really only proposed)) can be seen here - 120thingin20years..blogspot.com 
'-------------------------------------------------------------------------
'Uses PICAXE 14M2
'-------------------------------------------------------------------------
'Multi-itask
' Main: (start0) Checks for changed states and calculates  things as required
' Start1:       Checks for lever presses
' Start2:       Reports feeds so far
'-------------------------------------------------------------------------
'This code is provided free to the world without reservation.
'NO RIGHTS RESERVED
'read that bit again before you contribute, but please chip in if you can contribute
'=========================================================

'Variables
{
Symbol w0_DayLengthInMinutes = w0
Symbol w1_RemainderOfTheDayInMinutes = w1
symbol w2_MinimumMinutesBetweenFeeds = w2
symbol w3_ReportFeedsFrequency=w3
Symbol w4_MaxAllowedFeedsForTheDay = w4
Symbol b26_RemainingFeedsForTheDay = b26
Symbol b25_FeedsCurrentlyAllowed = b25
'b24
symbol b23_MinutesSinceLastFeed = b23
symbol b22_NumberOfFeedsNowAllowed = b22
Symbol  b21_LeverLEDState=b21
'-------------Start2 variables ----------------------------
symbol b20_Start2Count = b20
symbol  b19_Start2Count2= b19
symbol b18_FeedsSoFarToday = b18
}
'---------- Here lies everything adjustable ---------
let w4_MaxAllowedFeedsForTheDay =2880    'large number for testing only as I cant wait all day
let w3_ReportFeedsFrequency = 3000       'recomend 15000 = 15 seconds between number of feeds so far today reports
'TODO: feed ammount in fractions of seconds of motor turns
'===========================================
Main:
#simtask all   'for debug only change all to 0,1,or 2 to simulate different multitask threads (not on linux though)
{
'debug
GoSub Init: 'initialize everything required when the device is turned on
GoSub NewDayInit      'initialize everything required to reset for a new day
GoSub OngoingChecks      'main program flow that checks everything continously
end
}
'------------ Main Subs --------------------------------
OngoingChecks: 'main program loop that checks everything continously
{
'debug
GoSub IsFeedingOK    'check if its ok for them to eat
'check for dawn
'check for button press - give them an extra feed override
'check for button press - silulate a days feed
'check for HSM no water flow
'check for HSM water temp is within range
goto OngoingChecks
Return 'never seen
}
'------------------------------------------------------------------
Init: 'used once at system reboot
{
w0_DayLengthInMinutes = 1440    'should be 1440 - populates the variable with the length of a day in minutes
return}
'------------------------------------------------------------------
NewDayInit: 'used at the begining of each new day
{
'debug 'Hi mum let b25_FeedsCurrentlyAllowed = 1
let b21_LeverLEDState = 1
if b21_LeverLEDstate = 1 then High c.4 :endif ' TODO:not sure why this is needed but I was seeing some strange results
let w1_RemainderOfTheDayInMinutes = w0_DayLengthInMinutes
let b26_RemainingFeedsForTheDay = w4_MaxAllowedFeedsForTheDay
let w2_MinimumMinutesBetweenFeeds = w1_RemainderOfTheDayInMinutes / b26_RemainingFeedsForTheDay
let b18_FeedsSoFarToday =  w4_MaxAllowedFeedsForTheDay - b26_RemainingFeedsForTheDay  ' for Start2 report feeds
let time = 0 'reset time to reset minutes since last feed

return}
'------------------------------------------------------------------
NewFeedPeriodInit: 'Used after each feed
{
'debug
let b26_RemainingFeedsForTheDay = b26_RemainingFeedsForTheDay -1
      let b18_FeedsSoFarToday =  b18_FeedsSoFarToday + 1  ' for Start2 report feeds
let w2_MinimumMinutesBetweenFeeds = w1_RemainderOfTheDayInMinutes/b26_RemainingFeedsForTheDay
let b22_NumberOfFeedsNowAllowed = 0 ' dont feed the greedy things just yet
let b21_LeverLEDState = 0 '0 switch off the lever LED
let time =0 'reset time to reset minutes since last feed
return}
'------------------------------------------------------------------
 IsFeedingOK:   'check if its ok for them to eat
{
let b23_MinutesSinceLastFeed = time/60 'calculate  and store the number of minutes since last feed
if b26_RemainingFeedsForTheDay = 0 then return ' bail out of the loop if they cant have any more feeds
endif
if b23_MinutesSinceLastFeed  > w2_MinimumMinutesBetweenFeeds then return ' bail out of the loop if time isnt up
endif
let b22_NumberOfFeedsNowAllowed = 1 'if the program gets this far it means its ok to let them feed
let b21_LeverLEDState = 1 ' turn on the lever LED
'debug
return

'============= End Main ================================
Start1:   'check for lever presses
{
'debug
if b22_NumberOfFeedsNowAllowed = 0 then goto Start1 'no point going further

if pinc.3 = 1 then Gosub Feed
goto Start1}

'--------------Start1 Subs ------------------------------
Feed:
{
'debug
'deliver feed
gosub NewFeedPeriodInit
return}
'============= End Start1 ===============================
start2: 'multitask 2 - loop to  report feeds so far ---------------------------------
'debug
pause w3_ReportFeedsFrequency 'pause for the desired time between each report

if b18_FeedsSoFarToday = 0 then goto FlashZero
b20_Start2Count=b18_FeedsSoFarToday/10                 'isolate tens
for b19_Start2Count2 =1 to b20_Start2Count           'flash tens
if b20_Start2Count=0 then GoTo Remainder
                high c.1   'LED on
pause 800
low c.1                    'LED off
pause 40
high c.1
pause 40
low c.1
pause 780 next b19_Start2Count2


Remainder:
b20_Start2Count=b18_FeedsSoFarToday//10               'isolate remainder
 
for b19_Start2Count2 =1 to b20_Start2Count           'flash units
       
if b20_Start2Count=0 then goto FlashZero
high c.1
pause 200
low c.1
pause 200
 
next b19_Start2Count2
'debug
goto start2
FlashZero:
high c.1
pause 40
low c.1
'debug
goto start2
'==============End Start2 ================================

'TODO: work something out to counter switch bounce if required 

No comments:

Post a Comment

Popular Posts