# REPEAT UNTIL loop doesn't work right?

Status
Not open for further replies.

#### MrDEB

##### Well-Known Member
Have tried using ELSE ETC. but the REPEAT UNTIL loop stops when I input time4. The time 1 to time3 work. WHY?? I thought REPEAT should keep looping UNTIL master_swt = 0.
Code:
SUB ET () // rotary switch selects the desired delay elapsed time between 1st green and red led.
//player selects the desired elapsed time of play then presses the red Led switch (Master_swt) to indicate
//selection then code continues

repeat' master_swt=1
mst_led=1
IF Time1=0   //port A.2
THEN Et_time=1000
end if
IF Time2=0  // portA.3
THEN Et_time=1500
End if
IF Time3=0  //portA.4
THEN Et_time=2000
End if
IF Time4=0  //portA.5
THEN Et_time=2500
end if

Green1=0        //500ms time1
DELAYMS(Et_time)       //5000ms time3
TOGGLE(Green1)
Green2=0        //500ms time1
DELAYMS(Et_time)       //5000ms time3
TOGGLE(Green2)
Yellow1=0        //3500ms time1
DELAYMS(Et_time)       //5000ms time3
TOGGLE(Yellow1)
Yellow2=0        //500ms time1
DELAYMS(Et_time)       //5000ms time3
TOGGLE(Yellow2)

MST_LED=1
DELAYMS(1000)

MST_LED = 0
DELAYMS(1500)
UNTIL Master_Swt=0  //exit routine
'wend
END SUB

[/QUOTE]

#### tumbleweed

##### Active Member
I thought REPEAT should keep looping UNTIL master_swt = 0
It does.

In that code you may have to press master_swt for as long as 12.5 secs for the loop to exit.

#### MrDEB

##### Well-Known Member
I was wrongly thinking that since time4 was at the end it doesn't have any more code to process.
Any ideas to remedy the long depress?

#### JonSea

##### Well-Known Member
We tried to cover this in one of your other epic sagas, but you decided it was too complicated. You can't have long delays if you expect push buttons to work. A delay ststement stops everything.

One way around this is to use short delays and a counter. Say you delay for 100 mS. When you press the switch, ot may not be noticed for a tenth of a second, but that short of a delay won't even be noticed.

So a short delay: delayms(10)

And a counter: inc(ElapsedTime)

And a test using a while/wend or repeat/until ElaspedTimr equals 10×the time you want in seconds.

Easy peasy. I'm out.

#### MrDEB

##### Well-Known Member
the code posted works as planned until I rotate the rotary switch to the fourth position regardless where it started. When it is set for the fourth position the code goes south. It doesn't follow the code. I tested to see if connections are good and yes all works as it is supposed to.
It seems like the code just runs out of steam.

#### tumbleweed

##### Active Member
It seems like the code just runs out of steam
Well, that's it then. You've used up all the bits.

#### JonSea

##### Well-Known Member
Well, that's it then. You've used up all the bits.

His bit bucket is empty?!?

Maybe the bits are just rebelling over the torture they've been through.

#### MrDEB

##### Well-Known Member
I am contemplating using a CONST ARRAY seeing how the rotary switch is a coded switch thus us only 4 bits but trying to access what is actually happening. I ran a short code to determine if in fact the desired pin is going LOW. that works.
When I rotate the switch 4 times then the code gets weird.

#### JonSea

##### Well-Known Member
....the rotary switch is a coded switch....
If anybody is going to be able to help you, you better post exactly what the switch is and a schematic of your connections. Your posts have been all over the place, so it's impossible to know what you've got.

#### MrDEB

##### Well-Known Member
After I determined what is actually occurring by turning LEDs for each Time(x) I found Time4 on portA,5 is not playing right. Yes I looked at the data sheet and it is a digital I/O pin. I have two completed boards and both have same issue.
need to investigate further as LOML wants to go camping for my B day.
basic code
SUB ET () // rotary switch selects the desired delay elapsed time between 1st green and red led.
//player selects the desired elapsed time of play then presses the red Led switch (Master_swt) to indicate
//selection then code continues

REPEAT' master_swt=1

IF Time1=0 //port A.2
THEN Et_time=500
DELAYMS(500)
pLAYER1 = 1 //indicates switch position
Green1=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green1)
Green2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green2)
Yellow1=0 //3500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow1)
Yellow2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow2)
MST_LED = 1
DELAYMS(500)
TOGGLE(MST_LED)
DELAYMS(500)
player1 = 0
END IF
IF Time2=0 //port A.2
THEN Et_time=700
pLAYER2=1 //indicates switch position
Green1=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green1)
Green2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green2)
Yellow1=0 //3500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow1)
Yellow2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow2)
MST_LED = 1
DELAYMS(500)
TOGGLE(MST_LED)
DELAYMS(500)
player2 = 0 //turn off position led
END IF
IF Time3=0 //port A.2
THEN Et_time=1000
pLAYER3=1 //indicates switch position
Green1=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green1)
Green2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green2)
Yellow1=0 //3500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow1)
Yellow2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow2)
MST_LED = 1
DELAYMS(500)
TOGGLE(MST_LED)
DELAYMS(500)
player3 = 0
END IF

IF Time4=0 //port A.2
THEN Et_time=1500
pLAYER4=1 //indicates switch position

Green1=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green1)
Green2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Green2)
Yellow1=0 //3500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow1)
Yellow2=0 //500ms time1
DELAYMS(Et_time) //5000ms time3
TOGGLE(Yellow2)
MST_LED = 1
DELAYMS(500)
TOGGLE(MST_LED)
DELAYMS(500)
player4 = 0
'if master_swt=0 then exit sub

END IF
MST_LED=1
DELAYMS(1000)

' MST_LED = 0
'DELAYMS(1500)
UNTIL Master_Swt=0 //exit routine
'wend
END SUB

[end/QUOTE]

ml

#### JonSea

##### Well-Known Member
You said it's a coded switch. As I asked before, please provide a part number, data sheet or even a picture

I applaud you for producing a ledgable schematic on the first try - that's astonishing. But it's useless as you've shown 5 push buttons where the rotary switch is (from my understanding of your problem). If the switch truly is a coded switch, your schematic is totally wrong.

 I guess the push buttons aren't the rotary switch. If there is a rotary switch on this "schematic", I can't find it. The way you use bus connections is wrong and impossible to follow.

Last edited:

#### MrDEB

##### Well-Known Member
short routine to test rotary switch and ports. ALL work as planned
Code:
WHILE TRUE
if time1 = 0 then  //rotary switch position
player1 =1        //led at position1
delayms(1000)
toggle(player1)
delayms(1000)
end if
if time2 = 0 then
player2 =1        //led at position2
delayms(1000)
toggle(player2)
delayms(1000)
end if

if time3 = 0 then
player3 =1
delayms(1000)
toggle(player3)
delayms(1000)
end if

if time4 = 0 then
player4 =1
delayms(1000)
toggle(player4)
delayms(1000)
end if
wend

Last edited by a moderator:

#### MrDEB

##### Well-Known Member
I copy and pasted the sub route from the main program code and ran it by itself. ALL works as planned so now search for why it screws up with entire code.
I confirmed that the rotary switch and ports all work as planned.

#### JonSea

##### Well-Known Member
The data sheet is for a bunch of different switches, so so it's still a mystery what switch you have.

Buses should be used to collect a group of like signals together, like the connection between micro and LCD in this illustration. You can't have a meaningful schematic by collecting every non-relates signal in bus. You can't tell when anything goes. You might as well just eliminate the wires and write notes by each pin - that would make just as much sense and be easier to folllow.

#### Dr_Doggy

##### Well-Known Member
mayb e a short circuit on Master_Swt an #4 ?

are you monitoring Master_Swt to see if/when it is clearing ?

#### MrDEB

##### Well-Known Member
Just got back from Mt Rainier and was contemplating this most of the weekend. I will double check but yes when the master_swt is depressed, the sub is exited. Will confirm about a short in the morning. Am really bushed.

#### MrDEB

##### Well-Known Member
added a short led routine and yes there is something wrong as both boards do the same thing. I any position 1 - 3, it works just as planned but position 4 it jumps out of the sub before depressing the master swt. It cycles through the leds then ends the sub.
I was hoping a code issue.

#### DrG

##### Active Member
This looks like a lot of self-inflicted injury compounded with a strange unresponsiveness and a wandering focus reminiscent of those old drunkard walk programming problems.

Others have repeatedly asked for the TS to identify the rotary switch (even in a previous thread) but it does not happen. Yes, a link to a data sheet is provided that contains 4/10/16 position switches all of which are 6-pin in both SMT and through-hole packages.

If we assume that it is a 4-position variety, then what I see (and I could have made a mistake) from the schematic is the following (SP=switch position 0-3), SG (switch pin grounded for that position), L=line as named in the schematic and code, PB=connection to port bit:

SP SG L PB

0 3 Time2 RA3

1 1 Time1 RA2

2 6 Time4 RA5

3 4 Time3 RA4

Notice that the sequence that is followed in the schematic (RA2-RA5) and the code (Time1-Time4) is not followed by the switch position.

But, what is even more interesting is what happens when you consider the case if U1 is either the 10 or 16 position variety. Pin 6 of U1 is connected to the 10K pull up and the Time4 line will only go low when the switch is in positions 8-9 (or 8-15) and never when in position 4.

Of course, it is only a guessing game, but I am thinking it may be the case because he refers to positions 1-4 and not (0-3) suggesting he is using a 10 (or 16) position switch. If this is the case, then the problem is further compounded because position 3 will ground two pins on U1 (1, 4).

In these cases, the subroutine would never see Time4=0 and never set the delay interval. It would still not exit the subroutine, except if the master switch is low or the processor resets for some other reason. Then again, there could be many other problems and I could simply be wrong.