repeating procedure using inc Var

sanok

New Member
Hello,
The program runs the procedure twice and goes to wait 10 and then start again, but this time it doesn't repeat the procedure anymore.
It goes through the "no" line and back to start. It keeps going continuously disregarding the compare cell.
I thought that new loop should repeat everything exactly as first time.
Flowchart is attached
23131
 

BESQUEUT

Senior Member
Maybe you have to reset varA somewhere ?

Also note that if varA=2 and then you inc varA
now varA is 3 wich is different to 2...
so the correct answer for (varA=2) is NO
 

hippy

Senior Member
The program runs the procedure twice and goes to wait 10 and then start again, but this time it doesn't repeat the procedure anymore.
It is hard to see why the flowchart would not repeat the procedure given it goes back to the start whatever the result of the comparison, with a delay or not, and the first thing it then does is call the procedure.

It would be worth posting your .plf flowchart file as what is specified for the commands shown do not always match the visual descriptions of what they show. That will allow members to check and assess what may be going wrong.
 

sanok

New Member
when I have this flowchart open I click file and save as and this is what I have attached here.
Here is a BASIC conversion:

main:
Cell_7_5:
; No setup required

gosub prc_motor_runs_f_and_back
inc varA
if varA = 2 then
goto Cell_7_9 (wait10)
end if
goto Cell_7_5 (start)

Cell_7_9:
pause 10000
goto Cell_7_5

prc_motor_runs_f_and_back:
low B.0,B.1
low B.2: high B.3
low B.4,B.5
low B.6: high B.7
pause 1000
low B.2,B.3
low B.6,B.7
pause 2000
low B.3: high B.2
low B.7: high B.6
pause 1000
low B.2,B.3
low B.6,B.7
pause 2000
return

#no_data 'reduce download time
 

AllyCat

Senior Member
Hi,

You are changing 4 pins each time, is that to drive two separate motors or are the pins commoned to increase the current drive (or for some other reason)? Also, the specific PICaxe chip you're using might be relevant. Or are you just using the simulator so far?

Cheers, Alan.
 

sanok

New Member
Hi,

You are changing 4 pins each time, is that to drive two separate motors or are the pins commoned to increase the current drive (or for some other reason)? Also, the specific PICaxe chip you're using might be relevant.

Cheers, Alan.
yes I drive two motors at the same time, the chip is pic16f886
 

hippy

Senior Member
The code appears to be for a PICAXE-28X1. I see a slight difference to the converted code in #6 because the GOSUB cell repeats twice using varH.

But I cannot see anything wrong with the code, and it simulates fine for me, keeps running the motor control routine. I don't however have suitable hardware to physically test it on.

The only thing I can think is that it runs a couple of times and then the batteries go flat, don't have enough oomph to turn the motors.
 

sanok

New Member
The code appears to be for a PICAXE-28X1. I see a slight difference to the converted code in #6 because the GOSUB cell repeats twice using varH.

But I cannot see anything wrong with the code, and it simulates fine for me, keeps running the motor control routine. I don't however have suitable hardware to physically test it on.

The only thing I can think is that it runs a couple of times and then the batteries go flat, don't have enough oomph to turn the motors.
so when you simulate does it repeat procedure twice after each start or only on first start? and on second and each consecutive start does it go to "no" at compare?
 

hippy

Senior Member
so when you simulate does it repeat procedure twice after each start or only on first start? and on second and each consecutive start does it go to "no" at compare?
Yes. I added a SERTXD cell at the start of the motor procedure with the data to show set as -
Code:
"Motor varA=", [A], CR, LF
When I simulate that the Terminal shows -
Code:
Motor varA=000
Motor varA=000
Motor varA=001
Motor varA=001
Motor varA=002
Motor varA=002
Motor varA=003
Motor varA=003
etc
That shows the subroutine is called twice for every main loop, regardless of value of varA.

The DELAY 10 is executed when varA becomes equal to 2, otherwise the 'no' branch is taken.
 

hippy

Senior Member
varA have to be resetted to zero somewhere..
You are probably right.

I think the problem here is terminology, how what it is doing and not doing is being described. The procedure (subroutine) is repeating, the overall desired sequence is perhaps not.
 

inglewoodpete

Senior Member
While we have been discussing software issues, there is a possibility that the reactive currents and voltages from the motor/s are causing random resets of the PIC.
 

sanok

New Member
While we have been discussing software issues, there is a possibility that the reactive currents and voltages from the motor/s are causing random resets of the PIC.
no because this is happening in simulation too with circuit off. Could be a software glitch or a permanent conflict with this particular code/flowchart?
 

BESQUEUT

Senior Member
no because this is happening in simulation too with circuit off. Could be a software glitch or a permanent conflict with this particular code/flowchart?
As said, the program does exactly what it is writed to do.
If you want a 10s pause every two iteration, you have to reset varA to zero when the test is YES...
 

sanok

New Member
As said, the program does exactly what it is writed to do.
If you want a 10s pause every two iteration, you have to reset varA to zero when the test is YES...
Oh I understand now, adding expression box VarA=0 after wait10
thank you
 

sanok

New Member
my understanding was that a new loop is a new lop regardles of what happened in previous loop, but looks like that is not the case.
 

AllyCat

Senior Member
Hi,
my understanding was that a new loop is a new lop regardles of what happened in previous loop, but looks like that is not the case.
The PICaxe system resets all (normal) variables to zero BEFORE the program starts to run. But if this is an important part of how the program is required to work, then it's much better practice to actually specify the Var = 0 (or whatever). Maybe sometime later, the program might be converted to a different computer language or the Variable obtained from a different source (e.g. Reading from EPROM) where the value might not start at zero.

Also, the program "Loop"s (actually there are two) are something that YOU (the Programmer) have created, the individual instructions cannot "know" that they are part of a loop. But even if the "Start" module did "know" that it was part of a loop, how could it know that it should behave differently (i.e. Reset the Var.) if the previous instruction was in the "Yes" path but not in the "No" path ?

There are "formal" Loop instructions (e.g. DO .. : LOOP UNTIL var =2) but they still can't "guess" that a particular (or any) Variable needs to be Reset before (or after) they execute. The only command (that I can think of) which specifically does initialise the Loop is the FOR variable = .. TO .. : NEXT structure, but this will still only reset/load the named variable, any others will be unchanged (or manipulated by the enclosed instructions as required).

Cheers, Alan.
 

Aries

New Member
Double-clicking on your flowchart for the call to the motor subroutine, it says "repeat 2" - hence the reason each print line appears twice - it's the equivalent of
gosub prc_motor_runs_f_and_back
gosub prc_motor_runs_f_and_back

The pause happens only when VarA = 2 - you should be able to see this if you print the time when you print the value of the variable.
 
Top