Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:11 10 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Seconds

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:55pm 08 Jan 2018
Copy link to clipboard 
Print this post

I'm trying to get the MM to add 2 values together at a certain time

if time$ = "22:46:00" then rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
end if

rain and rain2 are dim'd as float and display the correct values
and it does work
BUT
it adds up continuously it seems like every microsecond for that one second the time is correct

I just did a test with rain = 0.832 and rain2 = 0 and it ended up like this
[code] 0.8382 rain
128.245 rain2
0.8382 rain
129.083 rain2
0.8382 rain
129.921 rain2
0.8382 rain
130.759 rain2
[/code]
it did start off with rain 2 = 0, then rain2 = 1.664, then 2.496 and so on till 130.759 and the time$ = "22:46:01"

How can I get it to only add once in that second period?
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1646
Posted: 11:25pm 08 Jan 2018
Copy link to clipboard 
Print this post

You could clear a flag during initialisation and, test if before the calculation at 22:46:00, if it's clear then do the calculation and then set it immediately after. Clear it again at 22:46:01.

Bill
Keep safe. Live long and prosper.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 11:36pm 08 Jan 2018
Copy link to clipboard 
Print this post

You are doing the check more than 1 time per second, so it will add multiple times the way you have coded it until the second.

Set another tick interrupt (you can have up to 4 on the MM) every second (1000ms) and in that interrupt you can do the check and update.
 
goc30

Guru

Joined: 12/04/2017
Location: France
Posts: 435
Posted: 11:43pm 08 Jan 2018
Copy link to clipboard 
Print this post

You can write

dim timet1 as string 'time at t-1

if (time$ = "22:46:00" and time$<>timet1) then
rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
end if
timet1=time$
Edited by goc30 2018-01-10
 
Boppa
Guru

Joined: 08/11/2016
Location: Australia
Posts: 816
Posted: 06:24am 09 Jan 2018
Copy link to clipboard 
Print this post

is the old 'wait' command still available? or use a for/next loop to pause for a second

if time$ = "22:46:00" then rain2 = rain + rain2
(wait 1 second)
print rain, " rain"
print rain2, " rain2"
end if
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 06:30am 09 Jan 2018
Copy link to clipboard 
Print this post

I know lew247 has a lot of other code running on his weather station updating every second that he wants to keep running smoothly without glitches when such situations occur. So PAUSE (WAIT) is not ideal in this situation.
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:42am 09 Jan 2018
Copy link to clipboard 
Print this post

Using a Flag is the easiest way.
[code]
Dim RainFlag = 0

...

if RainFlag = 0 and time$ = "22:46:00" then
RainFlag = 1
rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
end if
if RainFlag = 1 and time$ = "22:47:00" then
RainFlag = 0
end if
[/code]
even better and more resilient is to check for a minute, because if somehow other code takes up more than a second you could miss that specific timestamp.
[code]
Dim RainFlag = 0

...

if RainFlag = 0 and left$(time$,5) = "22:46" then
RainFlag = 1
rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
end if
if RainFlag = 1 and left$(time$,5) = "22:47" then
RainFlag = 0
end if
[/code]

You could do it even more resilient even allowing for a very long sleep.
This will do the task anytime only once between 22:00:00 and 07:59:59 the next day.
[code]
Dim RainFlag = 0

...

if RainFlag = 0 and (left$(time$,2) >= "22" or left$(time$,2) < "08" then
RainFlag = 1
rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
end if
if RainFlag = 1 and left$(time$,2) >= "08" then
RainFlag = 0
end if
[/code]




Edited by MicroBlocks 2018-01-10
Microblocks. Build with logic.
 
Boppa
Guru

Joined: 08/11/2016
Location: Australia
Posts: 816
Posted: 06:49am 09 Jan 2018
Copy link to clipboard 
Print this post

OK knew it would be too easy
set a flag like this? (my basic is rusty so may not be correct format)

if time$ = "22:46:00" and flag=0 then rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
flag = 1
end if

if time$ = "22:46:01" then flag=0



Actually that would need cleaning up as if drops back into the rest of the loop but it shows the idea
probably better as

if time$ = "22:46:00" and flag=1 then end if
rain2 = rain + rain2
print rain, " rain"
print rain2, " rain2"
flag = 1
end if


if time$ = "22:46:01" then flag=0


(Personally I hate this new basic with indents, I find line numbers much easier to follow and work with, but then that was what I grew up with, its been years since I programmed anything in Basic)
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:44pm 09 Jan 2018
Copy link to clipboard 
Print this post

Thanks everyone, Jean your a star, nailed it Edited by lew247 2018-01-10
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025