Loop algo problem

1. Loop algo problem

I am trying to make a complex loop and I suppose I am in a wrong mind loop...

The project need a time base. Done in a ISR that counts seconds and minutes OK.

The request is to have a var value change according to data read from an EEPROM. That data include the time and setpoint.

Example:

Phase 1: For 1 minute
change value from setpoint1 to setpoint2 and reverse every 20 seconds

Phase 2: For 2 minutes
change value from setpoint1 to setpoint2 and reverse every 30 seconds

My ISR is this:
Code:
```timer:                    'Timer 1 tick every 1us x 50000 = 50ms
t1con.0=0             'time variable inc every 50ms
if time<19 then       'x 20 = 1sec
time=time+1
else
time=0
if seconds>0 then 'dec seconds
seconds=seconds-1
else
seconds=59
if minutes>0 then  'dec minutes
minutes=minutes-1
else
endif
endif
IF p_seconds>0 then p_seconds=p_seconds-1
endif
tmr1l=\$b7   'reload timer 1  with 16474
tmr1h=\$3c   '65536-49062
t1con.0=1   'start timer 1

@ INT_RETURN```
The data are read from the EEPROM using a For-Next loop in 4 byte group:

Code:
```for addr2=start_addr to end_addr step 4
Then there is this wrong loop that does not work correctly. It goes to the next phase when change_seconds_period expires.

Code:
```temp_change_seconds_period=change_seconds_period
value=setpoint1

loop1:
while minutes>0
if change_seconds_period=0 then loop2
if temp_change_seconds_period=0 then 'change over setpoints
value=setpoint2
endif
loop2:
if !but_stop then stop_prg  'test for user interaction
if !but_pause then pause_prg  'test for user interaction
lcdout \$fe,line3,"Phase ",#phase,\$fe,line4,dec3 value," ",dec3 minutes," ",dec3 temp_change_seconds_period
wend

phase=phase+1
Any help appreciated.
Ioannis
Last edited by Ioannis; - 30th December 2018 at 21:44.  Reply With Quote

2. Re: Loop algo problem

i think your isr is over complex

my take
Code:
```isr:

stop timer
inc counter
is counter>19  then{
counter =0
seconds_changed = 1
}
timer = timer + preset
start timer
exit is

start:
phase=0 ;two phases 0 an 1
phasetime=60
phasereverstime=20
sp=0; two setpoints 0 and 1
phrt=20

loop:

if seconds_changed then
seconds_changed =0
if phasetime then
phasetime=phasetime-1
if  phasereverstime then
phasereverstime=phasereverstime-1
else
phasereverstime=phrt
gosub swapsetpoints
endif
else
if phase then
phase=0
phasetime=60
phrt=20
else
phase=1
phasetime=120
phrt=30
endif
endif
endif

goto loop```  Reply With Quote

3. Re: Loop algo problem

Here, you set these two variables to be equal to each other.
Code:
```temp_change_seconds_period=change_seconds_period
....```
Then, here, you are checking two of them if they are = 0 or not.
If the first one is 0, then second one must also be 0 ; you had set them to be equal beforehand.
So if change_seconds_period=0, then next line temp_change_seconds_period=0 will never work.

Code:
```        while minutes>0
if change_seconds_period=0 then loop2
if temp_change_seconds_period=0 then 'change over setpoints
....```
Am I wrong?  Reply With Quote

4. Re: Loop algo problem

Thanks both for the suggestions.

Give time to look at your recommendations Richard.

Sayzer, the change_seconds_period variable may or may not be zero, depending on the I2C EEPROM read. A check I think is needed there to jump over the next lines.

Ioannis  Reply With Quote Members who have read this thread : 17

You do not have permission to view the list of names. Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts