Page 4 of 6

Re: "sleep" inside a loop

Posted: Sat Nov 16, 2024 4:58 pm
by mnfisher
Sounds interesting..

How is the '4' hours period started - is it a fixed time or 'starts' when user presses a button (for example) Is the time allowed only as a singlr 4h block - or can the user have 2h in the morning and 2h in the evening (or other split)? (which would be more complicated?)

Sounds like the MCU can be 'asleep' for 20hrs per day -- and awake for 4 hours (depending on the pulse rate from the flow meter - could stil use sleep)
I assume the valve is close when no power is applied (so cannot extend supply by cutting power whilst the valve is open)

Martin

Re: "sleep" inside a loop

Posted: Sat Nov 16, 2024 5:22 pm
by ELECTRONICA67
Yes, it is correct that when you connect the batteries, the 4 hours start to count as an open bar. If in those 4 hours you have not consumed the 200 liters, the valve will close and you will not be able to have water for the next 20 hours even if you have not consumed the 200 liters.

When 24 hours have passed, the valve will open again and allows you to consume up to 200 liters for the next 4 hours.

Enrique

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 5:08 am
by chipfryer27
Hi

Yes, real typhoons, but not just any old ones, super-typhoons. Normal ones have the wind speed around 70mph wheras the super version typically have it upwards of 120mph which is around a catagory 4 or 5 hurricane. I'd have thought Martin would have created a gadget that when wind speeds were getting a bit interesting it would automatically call in his chickens and raise the drawbridge etc <s>

Anyway then the above explanation of project puts a slighty different slant on things. I'm assuming that your flow meter gives a pulse every x-amount of liquid so one litre of water equals x-pulses, with 200 Litres giving a fixed count of pulses.

Depending on how fast the water is flowing the rate will differ.

So you really are looking for two things, an elapsed time counter to tell you when four hours and twenty fours hours have elapsed, and a counter to tell you how much water has flowed, all starting from when you push a button.

As your sensor will be interrupting at "random" within the four hours I think we discount the WDT (for the moment). When sleeping any Interrupt will wake the chip so your sensor could be issuing many triggers per second or none at all.

I'm just thinking aloud here.

When you push the button to start it opens your valve and does two other things

Enables your sensor interrupt which increments a counter. When this counter reaches 200L the valve closes until "reset"
Enables TMR1 set for an appropriate period and increments a counter. When this reaches four hours it closes the valve until "reset".

I don't see great problem in having the chip sleep inbetween times.

Once four hours have passed the chip has absolutely nothing to do for the next twenty, so it disables the TMR1 and Sensor interrupts and uses the WDT set for an appropriate time, as this can make the chip sleep for extended periods. When the WDT wakes the chip, say every 256 seconds it increments a counter. When this counter informs that twenty hours have passed it then "resets" allowing the button to be used again.

Accuracy isn't that crucial for timing, if it was twenty hours and two seconds nobody would care (or whatever).

Is the above roughly what you wish?

Regards

PS
Forgot to mention that if you want TMR1 running whilst sleeping you would need an external clock. Also as it will be sleeping for the overwhelming majority of time, you need to make sure anything connected isn't drawing current. Using FETs for switching would be preferred, active-high.

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 8:49 am
by chipfryer27
Hi

Without access to hardware I can only simulate and as Martin mentioned earlier FC is great at that.

I'm not saying the following will work for you or even be what you need but it may give you some ideas.

I assume you do not have the setup for TMR1 to run during sleep so I haven't included.

The attached chart when run, first checks to see if it is being simulated. If so then later in the chart we simulate interrupts and sleeps by including delays. These delays won't appear in your actual hardware and you can just delete if you want.

I have used an LED on A1 as your valve
Button on B1 as your Start
Button on B0 as your Flow Sensor, interrupting and incrementing a count

Note that I don't know anything about your sensor you will need to figure out reading it and how many "counts" equals 200 Litres.

The chart basically sits sleeping from the minute you apply power and briefly:-

As usual we first reset variables then switch your valve off. We then enable WDT for 256 seconds and enable IOC (Start Button) before going into a loop with four branches (0-3).

In Branch 0 we do nothing but loop around sleeping

When the Start button is pressed we wake up and go to Button Macro. In here we set Branch=1

In Branch 1 we disable Start button, disable WDT, Set Branch =2 and enable your Valve. We also enable TMR1 and INT0 (Flow Sensor)

In Branch 2 it basically loops for four hours checking for target values and if reached does something. If you press the button "Flow Sensor" it will increase the value by one count. As we are simulating I have a delay of 13mS in the branch to act as Timer1. Each time the decision is reached it will call the Tick macro which increments a counter.

The first decision in this looping Branch is to check if 200 litres have flowed. If so we disable the Interrupt and disable the Valve.
The next decision is to check if four hours have passed. If so we disable TMR1, Disable INT0, Disable the Valve, Enable WDT and set Branch = 3

In Branch 3 we basically sleep for the next 20 hours (all buttons etc are disabled). Once 20 hours have elapsed we Enable IOC (start button), set Branch = 0 and loop around Branch 0 sleeping until the Start button is pressed.

I have not been able to test but it does compile to C and does simulate.

If you Simulate and have all variables on display, depending on your simulation speed you can see and also alter values. When testing, as I was simulation my four hours, I set "Litres" to 198. This meant I only had to push the "Flow Button" twice to reach 200. When it did the chart did as expected.

I then set "Tick" to 1,000,000 so within a few tens of seconds it had reach "four hours", did what it should then went into Branch 3. Here I checked nothing was active, buttons didn't work etc and then set "Tick" to a value close to 20 hours, which upon hitting the chart did as expected.

It isn't elegant, there are no doubt better ways to do it, but I can't think on a better way to sit in a hotel avoiding typhoons.... :)

Again, it is really just to perhaps give you ideas.

Regards

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 3:37 pm
by mnfisher
Looks good...

Spent my day working on chicken recall device. So far not working well - they maybe need more training.

One slight modification - turn everything off and go over to WDT interrupts if volume >= 200L or Time >= 4hrs. This will require some calculation of the WDT cycles needed to make up the remainder of the 4hours if needed. One idea - using the time remaining - use 256s WDT interrupts and then reduce the time (by changing the pre-scaler) to give an exact delay. But - I'm maybe over complicating things - it might give a little extra battery life?

Martin

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 5:05 pm
by ELECTRONICA67
Hello,

I hope you and your family are okay, I imagine it's about finding a safe place and sitting down to let it pass, and then fixing the mess. :|

Yes... you're right, the circuit checks data for the time of 4 hours with the valve open or if you consume the 200 liters close the valve, whichever occurs first, and the 20 hours with the valve closed, The other data is count the liters used (200 liters) within the 4 hours. The problem started when I tried to add batteries and the energy consumption was too much to use batteries, I even removed the 4-digit display. . .leave it installed the circuit with batteries and it will work automatically when the 24 hours are over and restart the cycle.

I will test with the program you sent me.

I am very grateful for what you are helping me and in the middle of a typhoon :!: ; as you say, distracting yourself while the typhoon passes :shock:

I will keep you informed of the progress made. :)

Regards

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 6:19 pm
by ELECTRONICA67
Sorry Lain, I spent all morning trying to compile and I couldn't, I changed computers and I've already compiled it, I'm starting the tests

Re: "sleep" inside a loop

Posted: Sun Nov 17, 2024 10:54 pm
by mnfisher
Sorry, rediscovered Half Life (2 is free if you are quick)

Some other things to look at for battery power.
An Led could pull 20,mA so possibly use PWM to reduce this..
When sleeping set all pins as input.
Make sure timer is disabled etc

Martin

Re: "sleep" inside a loop

Posted: Mon Nov 18, 2024 2:07 am
by chipfryer27
Hi

My family is definately safe from typhoons (thanks for asking) as they are nowhere near me. This being a business trip they saw little need to keep me company and eagerly packed my bags for me <s>

My knowledge of chickens is limited to humane dispatch and of course consumption, but I do remember a big loud TV chicken on Cartoon Cavalcade <s> That said, thanks to another contributer I am learning more about feeding and deplucking...

As mentioned, my chart was just to give an idea and as Martin said it could easily go into sleep if 200L had passed before four hours had elapsed. A simple calculation would do that

75 "ticks" = one second. 14,400 seconds = four hours, Therefore 75 x 14,400 = 1,080,000 ticks in four hours.
When 200L is reached, in the decision we can cancel TMR1, subtract the value of Tick from 1,080,000 and divide by 75 giving the remaining seconds left to reach four hours.

Say after two hours and forty two minutes we consumed 200L. That would be 75 x 60 x 162 = 729,000 "ticks". Subrtract this from 1,080,000 gives 351,000. Divind by 75 = 4680 seconds (or 78 minutes). A variable called Extra = (1080000-Ticks)/75

We can now exit this Branch and add in the Extra in Branch 3.

A few years back I got a bit obsessed with getting a PIC(LF) to draw as little as possible. You really can get drawn into it and when you get something right the feeling of satisfaction as you need to change scale on a meter to read is something <s> When I started I was hoping for six months from the circuit on two AA's, but using whatever Lithium was available at the supermarket I actually got far, far longer (well over a year if I remember correctly). PIC was still happy to run but the remaining electronics decided it was time to pack in. Monitoring via radio, remote locations and made possible by modules from my goto, RF-Solutions.

I'm not sure of the circuitry Enrique is using. Maybe it just needs a pulse to start / stop or the like but if holding a valve open then via appropriate FETs it should be possible to use PWM. Start off with 100% Duty (to operate) and back off to you find your holding limit. A bit of experimentation to find his limit could really make a difference. Maybe it is possible for Mains derived power to be operating the valve with the "PIC" just controlling? Again huge savings could be made.

Half-Life....... I need to get a life first <s> Now that is going back. Have to admit to enjoying Doom back in the day and playing it on a powerful PC. An HP running 25MHz...... but, and this is a big one, I had comms... 2 x 512Kb/s direct links, one to enemies in Canada, the other to enemies in Faroe..... enjoyable days <s>

Regards

Re: "sleep" inside a loop

Posted: Mon Nov 18, 2024 3:58 am
by ELECTRONICA67
Helloo Lain. . . . :D

Good evening, excellent news; after 2 weeks of sleepless nights and days when I didn't even eat, we have lowered consumption so far by 0.012 mA or what is the same 12 microA, I imagine that it is still possible to do more; for now today I will sleep my 8 hours and have a glass of my Agavero. . . Cheers to you.

Do you think that 3 Duracell AA batteries are enough for a few months? From what I have understood, the lower the voltage, the lower the current consumption, maybe even 2 batteries, I think the Pic works between 1.8-5.5 volts. :?:

With the program I understood 2 or 3 more options. I was able to start testing in the afternoon. . . my laptop failed, but everything is fine.

Thanks and Best Regards
Enrique