Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

REPEAT UNTIL loop doesn't work right?

Status
Not open for further replies.
good points Dr G
the switch is a 4 position switch pin 1 = Time1 pin 2 = ground pin 3 = Time2 pin 4 = Time3 pin 5 = ground pin 6 = Time 4
here is the code that I used to verify that I have a hardware issue and not a code issue. Is what is occurring is when the rotary switch is set on Time4, the position 4 (player4) led is enabled, the two green then two yellow leds are enabled in sequence. Then the Mst_led is enabled ,then the code jumps out of the loop and player10 led is enabled to indicate the sub has ended. In the Time1 _ Time 3 sequences the code does not jump out of the sub UNTIL the Master_swt is depressed.
basic code
sub et_select()
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.3
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.4
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.4
THEN Et_time=1000
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
END IF
' MST_LED = 0
'DELAYMS(1500)
UNTIL Master_Swt=0 delayms(500) //exit routine
end sub
WHILE TRUE
et_select()
for x = 0 to 5
player10=1
delayms(1000)
toggle(player10)
delayms(500)
next
wend
[end/QUOTE]
 
I think you are confused. First program listing:

SmartSelectImage_2018-06-25-13-34-26.png


Lastest program listing (impossibleto read -FORMAT!):

SmartSelectImage_2018-06-25-13-36-00.png


Of course, nobody can tell what you're doing because you haven't included your code with the dimension statements, whether you used SetAllDigital to switch Port A to digital, whether you are erroneously writing to input ports, etc.

If you can't even take time to make your code readable (which will help YOU keep track of things) why should anybody else take their time to decipher it? You could start by putting your if/then statements in the proper conventionsl format.
 

Attachments

  • SmartSelectImage_2018-06-25-13-36-22.png
    SmartSelectImage_2018-06-25-13-36-22.png
    13.3 KB · Views: 176
  • SmartSelectImage_2018-06-25-13-36-38.png
    SmartSelectImage_2018-06-25-13-36-38.png
    13.9 KB · Views: 174
  • SmartSelectImage_2018-06-25-13-36-51.png
    SmartSelectImage_2018-06-25-13-36-51.png
    11.6 KB · Views: 176
First of all try this when posting your code

[code=Basic]Some programming statements here...[/code]

This will format your code and make it easier to read.
 
First of all try this when posting your code

[code=Basic]Some programming statements here...[/code]

This will format your code and make it easier to read.


I can not count how many times I've given him this piece of advice.
 
good points Dr G
the switch is a 4 position switch pin 1 = Time1 pin 2 = ground pin 3 = Time2 pin 4 = Time3 pin 5 = ground pin 6 = Time 4
here is the code that I used to verify that I have a hardware issue and not a code issue. Is what is occurring is when the rotary switch is set on Time4, the position 4 (player4) led is enabled, the two green then two yellow leds are enabled in sequence. Then the Mst_led is enabled ,then the code jumps out of the loop and player10 led is enabled to indicate the sub has ended. In the Time1 _ Time 3 sequences the code does not jump out of the sub UNTIL the Master_swt is depressed.

Everybody finds their own way, but I think that in pretty much every instance where I have successfully debugged something, I have used a divide and conquer approach. Further, divide usually means to break out into a very simple portion and progress systematically. When you find yourself thinking that you are exiting a subroutine before a return, you are probably not. It is possible, but it is one of the last things you want to suspect going in. I am not saying that you don't have a hardware problem or a software problem or both, I am saying that you are complicating matters by not looking into small chunks and doing so systematically.

On the switch, to be sure that we are on the same page, refer to the datasheet on page 3-3. Note that the pin numbers on the switch are not in the usual format of 1-6. Instead, they are giving you which pin is ground for each switch position. So pin 3 is called 0 (for switch position 0) meaning that when the switch is in position 0, pin 3 will be connected to the common....and so on. Pins 2 and 5 are common (for GND).

So, I would start with something very simple by running the code below (and forgive me if some of the syntax is wrong but I have never used a BASIC with a PIC)...but only after making sure that you have the ports initialized correctly. Also, I can't tell from the schematic if you are sinking or sourcing current for the 8 player LEDs on Port B. I am acting like you have initialized parts such that player1=1 will turn on the LED marked play1 near the LED and pos0 near the port bit.

Code:
START

IF Time0=0 THEN
  // turn on LED for Switch position 0
  player1=1
  // turn off the other 3 LEDs
  player2=0
  player3=0
  player4=0
END IF

IF Time1=0 THEN
  // turn on LED for Switch position 1
  player2=1
  // turn off the other 3 LEDs
  player1=0
  player3=0
  player4=0
END IF

IF Time4=0 THEN
  // turn on LED for Switch position 2
  player3=1
  // turn off the other 3 LEDs
  player1=0
  player2=0
  player4=0
END IF

IF Time2=0 THEN
  // turn on LED for Switch position 3
  player4=1
  // turn off the other 3 LEDs
  player1=0
  player2=0
  player3=0
END IF

// delay for 1 second
delayms(1000)

GOTO START

Now, you should be able to turn the switch and only one of the 4 LEDs will light indicating the appropriate switch position.

If this doesn't work (assuming I have not done something bone-headed), you have to back up further....can you light the 4 LEDs sequentially?...No...is the port initialized to outputs...and so on and so forth.

If it works, you can add in another chunk. In this regard, I would suggest examining the so-called master switch - is that a momentary switch? Do you have a model number? Are you thinking about debouncing? All that stuff.

I think one of the problems with your approach is that you have way too much testing going on to isolate the problems in a meaningful manner.

That's my 2 cents anyways.

Edited to add:

This might be more conservative, in case, for some reason, more than one LED is lit:
Code:
START

// turn off all four LEDs
 player1=0
 player2=0
 player3=0
 player4=0

IF Time0=0 THEN
  // turn on LED for Switch position 0
  player1=1
END IF

IF Time1=0 THEN
  // turn on LED for Switch position 1
  player2=1
END IF

IF Time4=0 THEN
  // turn on LED for Switch position 2
  player3=1
END IF

IF Time2=0 THEN
  // turn on LED for Switch position 3
  player4=1
END IF

// delay for 1 second
delayms(1000)

GOTO START
 
Last edited by a moderator:
I like to simplify when testing hardware. .. :

while(1)
if sw1 then led1
if sw2 then led2
if sw3 then led3
if sw4 then led4
if sw5 then led 1 & 3
 
That was done several months ago on a perfboard and the PCboard. Each segment of code was tested by itself. Have yet to test a bare PCboard, only viewed the board file and find no issues so DMM. But presently building a gas powered garden cart for neighbor and trying to finish up my recumbent trike.
 
ya but it might be a good idea to try it again for debug purpose since it may be design flaw on these other boards .... if it is not that then the only other thing that comes to mind that it could be is that the Et_time is too large for the DELAYMS() function and overflowing the register
 
... it could be is that the Et_time is too large for the DELAYMS() function and overflowing the register
The DelayMS() function takes a word parameter, so no matter what you throw at it the max delay is 65535ms (65secs).
If you try to pass it values larger than that the higher bits will be ignored (truncates it to a 16-bit word).
 
Status
Not open for further replies.

Latest threads

Back
Top