24 hour sequential 08M2 elapsed timer

Desert Dan

New Member
Hi Guys,

I'm an elderly Electronics enthusiast with little experience in programming Picaxe's. I need help with writing a program to operate four relays in turn every 24 hours using four 08M2 chips. I understand that they can't time more than 18 hours in one go, so some looping/pausing is required. The closing of the fist relay would start the next chips program and so on.

Or a more elegant way would be to have one 08M2 and activate one of its four outputs in turn to operate a relay every 24 hours. This is the preferred method. The initial activation would be just a push button switch on one of the inputs.

I would really appreciate help with this.

Regards Desert Dan.
 
Last edited:

Buzby

Senior Member
Hi Dan,

Welcome to the forum !.

The solution to your task is quite easy to do with PICAXE, and I would be happy to helpl.

Unfortuunatly I've I've just got in from a good night out, so not so much use at the moment..

Somebody else will chip in and help, but I'm going to bed.

Cheers,

Buzby
 

AllyCat

Senior Member
Hi Dan,

Yes, welcome to the forum. It should be quite possible to use a single 08M2 to control four relays; at worst you may have to use the Serial Output pin (c.0) to (also) drive one of the relays. Extending the timing to > 24 hours is not at all difficult.

But first I think we need to consider a few hardware issues: Does the timing cycle have to be exactly 24 hours (like a clock) or would (say) +/- 10 minutes a day be acceptable? If not, then we will need to find a way to achieve better timing accuracy.

Secondly, what current and voltage do the relays need? Only quite small relays can normally be driven directly from the PICaxe pins (say a maximum of 20 mA at 5 volts, or a 250 ohm coil). Otherwise you will need to use driver transistors (or a ready-made relay module), but at least this might simplify the controlling of four relays from only the four available output pins on a 08M2.

Cheers, Alan.
 

Desert Dan

New Member
Hi Alan,

Thanks for your reply. The timing is not critical, could be even plus or minus 20 minutes.
I do have some 5 volt relay coils and I do know how to connect up driver transistors for 12v coils.
It's the program itself that I can't work out.

Cheers Dan
 

AllyCat

Senior Member
Hi,

The easiest way to start the timing is simply to (re-)apply the power to the PICaxe chip. Then the "Input Only" pin (c.3) would be available, for example to allow you to manually step from one relay to the next (if required).

The next question is how you are going to "tell" the PICaxe how long you want each relay to be operated? The PICaxe is so easy to (re-)program that you might simply embed the timing values within the program. But that's not so easy to change "in the field" (particularly if literally).

The "raw" timing value (or "tick") is generally 1 second (using the "time" variable) but "operate the relay for 12345 seconds" is not very user-friendly, so you may want to consider creating other time-counts such as minutes and hours. To get you started, you might use something like (not tested) in the PE simulator:

Code:
#picaxe 08m2
#no_data          ; Can speed up the download
symbol minutes = w2
do
  minutes = 0
  do
     time = 0     ; Reset automatic (system) seconds counter
     do
     loop until time > 59    ; Wait for a minute
     minutes = minutes + 1
  loop until minutes = 123
  high c.1        ; Operate relay
;  etc
loop
Now it's time for me to go to bed as well (but no, it isn't really after 2.00 here). :)

Cheers, Alan.
 
Last edited:

Desert Dan

New Member
Hi Alan,

Thanks for replying as such an early hour, oh to be young again!

The relays should stay energised until the system is turned off if that doesnt complicate the programming.

It doesn't need to be manually operated.

So I can test it if you can point out the timing variables so that I can reduce the time down to say 1 minute. Then when all is working Ok I can input the 24 hour bits and bobs.

Cheers Dan
 

AllyCat

Senior Member
Hi,

You might be surprised how many of us are very definitely not "young". ;)

Basically you could just change the "59" to "5" to give a "one minute tick" every 6 seconds. I often "comment out" the 9 by inserting a ; symbol (comment marker) between the 5 and 9 (i.e. 5 ;9) so that it's easy to reinstate the correct value later.

Similarly, change the 123 to 12 and the test relay should operate after 72 seconds.

There is a general dislike of "magic numbers" (like the 59 and 123) so more serious programmers would use commands like symbol SECONDS_IN_A_MINUTE = 60 (near the top of the Program) and at the appropriate point: loop until time => SECONDS_IN_A_MINUTE ; Wait for a minute (note the change from > to => to make the number more sensible).

Cheers, Alan.
 

rq3

Senior Member
Hi Guys,

I'm an elderly Electronics enthusiast with little experience in programming Picaxe's. I need help with writing a program to operate four relays in turn every 24 hours using four 08M2 chips. I understand that they can't time more than 18 hours in one go, so some looping/pausing is required. The closing of the fist relay would start the next chips program and so on.

Or a more elegant way would be to have one 08M2 and activate one of its four outputs in turn to operate a relay every 24 hours. This is the preferred method. The initial activation would be just a push button switch on one of the inputs.

I would really appreciate help with this.

Regards Desert Dan.
I'm probably all wet, but how about:

Code:
#macro timer(minutes)            ;define timer macro
    for w0=0 to minutes
        wait 60                        ;delay 60 seconds
    next w0
#endmacro

main:
do
    stuff
       more stuff
timer(1440)                          ;wait 1440 minutes (or 24 hours)
  close a relay
      more stuff
loop
 

Desert Dan

New Member
Hi Alan,

Would you be able to write the whole program for me to try out. I have one of the evaluation boards with 4 leds so I can soon get an idea if it's going to work. Just on 11.40pm here so off to bed for me.

Cheers Dan
 

lbenson

Senior Member
Use the simulator in the PE6 program (free download, look here: http://www.picaxe.com/software )

You can try out the programs by Allycat and rq3--you don't even need a picaxe chip. You can adjust the timings they have set up so that, for instance, seconds correspond to hours. You can walk step by step through each picaxe instruction to see what is happening.

Give it a try and come back with any questions.
 

AllyCat

Senior Member
Hi,

"Give a man a fish, and you feed him for a day; show him how to catch fish, and you feed him for a lifetime."

Apparently not an Ancient Chinese or Biblical proverb, but 19th century English.

Cheers, Alan.
 

Desert Dan

New Member
24 hour timer

I'm 70 years old and at my age it's near impossible to teach myself programming.

In two weeks time I am to go into Hospital for an operation. The project is to control a chilled fresh food cat feeder that I built using Peltier devices. The relay coils are being used to open the food containers in turn. There is no one that can come to the house to feed her while I'm in Hospital and I can't afford to put her in a Cattery. So it's pretty important that I get this finished before I go in.

So for you guys this is a peace of cake, but for me near impossible so your help with writing the whole program would be greatly appreciated.

Dan.
 

Buzby

Senior Member
Hi Dan,

Sorry to hear of your misfortune, here is some code to get you started.

It's not clear from your description if the relays need to remain continuously energised, or if they just need a pulse to release a catch or similar.
If pulses are needed you will need to change the 'high' instructions to 'pulsout' instructions.

Also, the 'sertxd' instructions, which are just for debug purposes, will need removing, because sertxd uses c.0.

Run this code in the simulator, it takes a few minutes to simulate each day
Code:
' Dan's cat feeder
' ================


#picaxe 08m2
#no_data    

' symbols
symbol minutes = w2
symbol hours   = w3


' Start of code
minutes 	= 0
hours 	= 0
do
	do ' Hours loop
		time = 0     ' Reset in-built seconds counter
		do ' Minutes loop
			pause 1000 
			sertxd (#hours,":",#minutes,":",#time,cr,lf)	      
		loop until time > 5 ' *** Should be 59 for one minute
		minutes = minutes + 1
	loop until minutes > 5	' *** Should be 59 for one hour

	hours = hours + 1
	minutes = 0

	if hours = 24 then 
		sertxd ("Day 1 relay operated",cr,lf) 
		high c.4        ' Operate relay
	endif

	if hours = 48 then 
		sertxd ("Day 2 relay operated",cr,lf)
		high c.2        ' Operate relay
	endif

	if hours = 72 then 
		sertxd ("Day 3 relay operated",cr,lf)
		high c.1        ' Operate relay
	endif

	if hours = 96 then 
		sertxd ("Day 4 relay operated",cr,lf)
		high c.0        ' Operate relay
	endif

loop
Cheers,

Buzby
 

AllyCat

Senior Member
Hi,

Ah, knowing the application helps a lot. A search for "cat feeder" on the forum produces a remarkably large number of hits (too many!), but I didn't see anything that looks immediately relevant or useful. I must admit that my interpretation of the requirement was quite different to Buzby's, but seeing his code, his interpretation is probably more correct than mine.

Normally one would indeed aim to fit everything into a single PICaxe, but this application might be an exception (assuming you have sufficient 08M2s available). For the consecutive opening of "doors" (e.g. one each day) it might be better to construct four very simple, identical modules which have just a very minor software difference, to give delays of (say) 1, 2, 3 and 4 days.

There are several advantages: each hardware and software module can be very simple and can be compared against the other(s), to check for correct operation (or to rectify isolated mistakes). Secondly, if the program "crashes" (perhaps after opening its appropriate "door", for example), then there's a reasonable chance that the other programs/modules will still keep working until they open their door. So there's much less risk of coming home to a very hungry cat.

For many projects the software "requirements specification" is the really hard part to get correct and finished. You might find that it's easier and quicker to try out a few ideas yourself on the simulator than wait for code updates from us over here, 9 hours behind you. ;)

Cheers, Alan.
 

The bear

Senior Member
Hi Dan,
I'm older than you, still a beginner at programming, but enjoying it very much.
People on the forum are very helpful, but they usually prefer to offer more help, once you have made a start and are struggling.
The simulator has been mentioned, its brilliant, give it a go.
The programs suggested, are all well and good, but you need to understand how they work.
Look in the manuals, start simple if you can.
I'm sure someone would help mit the cat.
Good luck, Bear..
 

premelec

Senior Member
Hi Dan - it would be helpful to know more about the hardware you are trying to activate - relays ordinarily do not open things - that's more work for motors, servos, solenoids etc... so just how is your cooler rigged - a picture would be of help see how you are rigged and anticipate transient voltages on your power supply and such. Likely you'd initially want a 'test' mode to run the sequence faster and see that the food doors are opened ok - perhaps every 10 minutes... I guess in a pinch there are commercial mechanical dry food feeders which could work for you - though not please the cat as much. [I'm 81 years old with a bunch of experience and still learning].
 

Desert Dan

New Member
Hi Alan,

I uploaded your program to the 08M2 but it only ran for 4 hours, any ideas.
Wondering why no one is using the 'time' option, I thought that was the big advantage of the M2 chip over the older one.
Also when I try the simulation program it gets stuck on the time line. How do you make it skip over that.

Cheers Dan
 

premelec

Senior Member
Dan - if you shorten the timing variables and run it in simulation mode in the editor it should be evident what happens - sometimes it's something like a variable getting over the top etc...
 

manuka

Senior Member
Desert Dan: I'm a great cat fan & ponder the little darling will crave companionship after a few days. How long are you laid up ? Surely you have a plan B ? Any local kids you can bribe with Easter Eggs to pop by ? Your Peltier cooler may become fair game for the curious & lonely cat to tinker with ... Stan.
 

AllyCat

Senior Member
Hi,

Hi Alan,

1. I uploaded your program to the 08M2 but it only ran for 4 hours, any ideas.
2. Wondering why no one is using the 'time' option, I thought that was the big advantage of the M2 chip over the older one.
3. Also when I try the simulation program it gets stuck on the time line. How do you make it skip over that.

Cheers Dan
Sorry, this reply must be rather after your bedtime.

1. I don't know. Is that the original or "speeded up" version (with modified constants) and in the simulator (PE5 or PE6) or on a "real" chip? But it probably doesn't matter too much now as it seems there are better ways to code your requirement.

2. The real advantage of the time variable is that the PICaxe can do lots of other stuff and just check occasionally to see how time is progressing. Thus it's ideal for programmers like me who want the PICaxe to "do lots of stuff". However, you've found one of its snags; it may not simulate in a "user-friendly" manner:

The problem is that the simulator must run a hundred or even a thousand times slower than "real time". That's partly because the simulator is a complex program, but also because most programs will run much faster than the programmer can follow. Even the "slow" PICaxe can execute 1,000 - 10,000 lines of program each second.

3. The simulator doesn't "know" what what the program is doing (nor who wrote it), so it assumes that there might be many hundreds of instructions to be executed each second (i.e. between the one second "ticks" for the time variable) so it only increments the time variable very slowly. However, it should eventually increment time from 0 to 1, etc..

One way to skip the time delay is to define a constant such as #define SIMULATING (I believe PE6 may do this automatically) and then write the appropriate section of code such as :
Code:
#define SIMULATING     ; Delete or comment-out for real PICaxe hardware  
do
#ifNdef SIMULATING      ; The next lines will be included only if the program is NOT simulating
   do
   loop until time > 59    ; Wait for a minute
#endif
   sertxd ("*")              ; Mark that "one minute" has passed
loop
But for your application the PAUSE or WAIT commands are probably a much better choice. In this case the simulator "knows" that nothing else is happening so it can shorten the execution time to a "user-friendly" value, perhaps the same as the "real" tme.

WAIT 60 is the simplest instruction to introduce a one minute delay, but personally I would recommend PAUSE 60000. WAIT is a "pseudo" command which basically multiplies the numerical value by 1000 (i.e. converts ms to seconds) and executes a PAUSE. That's fine for 60 seconds, but 65535 is the largest value that a PICaxe can handle so WAIT 120 will NOT work correctly. That is explained in the command reference, but if one of us regular programmers is "inspecting" somebody else's program code, then WAIT 120 doesn't "shout as being wrong" in the same way that PAUSE 120000 does.

Cheers, Alan.
 

Desert Dan

New Member
IMG_1793.JPG

Hi Alan,

I've uploaded a photo of the cat feeder, hope you can see it, just shows a line of text with CONFIG.

The way its works is that on power up the normally closed contacts of each relay connected to the O8M2 supply 12 VDC to each of the four Peltier devices and the relay coils on brackets. Those relays have had their contacts removed and are just acting as electromagnets to hold the food container lids closed. So all lids are closed and the fresh food is being kept cool.

The program is supposed to work like this, after 24 hours one output of the chip activates and energies a relay, that changes its normally closed contacts and removes 12VDC from the lid relay coil and the Peltier device, Pussy now eats. The chip output relay remains energised to keep the first feed bowl powered down.and 24 hours later the second 08M2 output activates and repeats the same action on the second feeder bowl, removing power to the Peltier device and the relay coil acting as an electromagnet allowing the lid to open.

This repeats for the remaining two bowls. The 08M2 will maintain voltage to the four relays with contacts it controls until i return and turn off the system.

Cheers Dan
 

Desert Dan

New Member
Hi Stan,

Like your senses of humour!!

Plan B is the cat dies, NO.

I should only be in for only 3 days and she does eat dry food so there will be a feeder provided for that with water.

Many years ago I had a neighbor coming in to feed my Tropical fish while I was on holidays. I didn't know at the time but he snooped through my private papers and recorded my full name, birth date and other things. He turned out to be the neighbor from hell with noisy cars, loud music etc etc and it went to court. Guess what, the papers he served in return had all my personal details on them. No more non family members left alone in my house.

Cheers Dan
 

Desert Dan

New Member
Hi Alan,

thanks for your reply. I've printed out your info and will try it out.
I have uploaded a photo of the Cat feeder project. Not sure if everyone can see it or only the person I replied too.
This is the first forum I've ever used and nearly as much to learn as Picaxe programming !!

Cheers Dan
 

Desert Dan

New Member
Hi Buzby,

I ran the your program and output C.0 pulses all the time. C1.0 is off, C2.0 on and steady, C3.0 off and C4.0 on and steady. After running for about 30 minutes

What lines do I input the numbers in to achieve the final 24 hour timing X 4.

I left the sertxd text in and the simulator wasn't worried by it, or should I take it out.
.
Your program looks like it should do the job.

Thanks in advance, cheers Dan

PS See photo of feeder
 

Buzby

Senior Member
Hi Dan,

c.0 pulses 'all the time' because it is the pin that sertxd uses to talk to the programming laptop.
You need to remove, or comment out, the sertxd lines on the real hardware.

The outputs should switch on in the order c.4, c.2, c.1, c.0. So seeing c.4 and c.2 on after 30 mins when running with '5', ( see below ), sounds about right.

The timing values are in the lines where I said use '59' instead of '5'. These numbers are how many seconds, or minutes, the code uses to time minutes, or hours.
By using '5' instead of '59' makes the PICAXE think a minute is 5 seconds long, and an hour is 5 minutes long.
This saves having to wait 4 days testing the functionality.

Meow,

Buzby
 

hippy

Technical Support
Staff member
This is probably the simplest program which sets outputs C.0, C.1, C.2 then C.4 on consecutive days ...

Code:
#Picaxe 08M2

Main:
  Low C.0, C.1, C.2, C.4
  Gosub WaitOneDay : High C.0
  Gosub WaitOneDay : High C.1
  Gosub WaitOneDay : High C.2
  Gosub WaitOneDay : High C.4
  End

WaitOneDay:
  time = 0 : Do : Loop Until time >= 43200 ; 12 * 60 * 60
  time = 0 : Do : Loop Until time >= 43200 ; 12 * 60 * 60
  Return
And with some resilience against power cuts ...

Code:
#Picaxe 08M2

Data 0,(0)

Main:
  Low C.0, C.1, C.2, C.4
  Read 0,b0
  On b0 GOTO Day0, Day1, Day2, Day3, Day4

Day0:                           Gosub WaitOneDay : Write 0,1
Day1: High C.0                : Gosub WaitOneDay : Write 0,2
Day2: High C.0, C.1           : Gosub WaitOneDay : Write 0,3
Day3: High C.0, C.1, C.2      : Gosub WaitOneDay : Write 0,4
Day4: High C.0, C.1, C.2, C.4 : End

WaitOneDay:
  time = 0 : Do : Loop Until time >= 43200 ; 12 * 60 * 60
  time = 0 : Do : Loop Until time >= 43200 ; 12 * 60 * 60
  Return
 
Last edited:

The bear

Senior Member
Hi Dan,
Nice photo, looks very professional. It also appears you have enough cable for the job.
All we want now is a mug shot of your tiger.
Regards, Bear..
 

Desert Dan

New Member
Thanks Tech Support,

Wonderful that you guys take the time to keep an eye on the forum.

I'll try your programming suggestions out.

Cheers Dan
 

techElder

Well-known member
Dan, what makes you confident that the cat will not hide in terror when this contraption is operating?
 

Desert Dan

New Member
Good question Tex,

I have been using for sometime now a single version of the feeder.
The timing being done with a basic AC mains timer.

She has learned that when the noise of the cooling fan stops the lid is open and it's time to eat.

But what I intend to add with the four feeder bowl version is a recording module that will play a recording of me whistling and calling her name. This will operate when each feeder lid opens, there very cheap and available on ebay.

Cheers Dan
 

Buzby

Senior Member
... She has learned that when the noise of the cooling fan stops the lid is open and it's time to eat.

But what I intend to add ~ is a recording module that will play a recording of me whistling and calling her name.
Dan,

This will be a 'must see' event !.

You've got to add a webcam so we all can admire your clever cat.

What's her name ?.

Meow,

Buzby
 

rq3

Senior Member
Good question Tex,

I have been using for sometime now a single version of the feeder.
The timing being done with a basic AC mains timer.

She has learned that when the noise of the cooling fan stops the lid is open and it's time to eat.

But what I intend to add with the four feeder bowl version is a recording module that will play a recording of me whistling and calling her name. This will operate when each feeder lid opens, there very cheap and available on ebay.

Cheers Dan
I'm thoroughly convinced that people don't train cats. Cats train people, and they do it slowly and subtley. Our cat would do just as yours does. Our dogs would eat the food AND the circuit. Our last dog LOVED aluminum electrolytics, the sulphuric acid inside tastes like lemons. At least he thought so.
 

nigec

New Member
How accurate is the 08M2 timer? Drift over 4 days I guess will not kill a cat with lack of food. Lack of water will. Do you have a cat flap to allow your cat to roam to do pee and poo?
 
Top