• 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.

correct code method?

MrDEB

Well-Known Member
Am attempting to put all my leds in a matrix thus freeing up some pin.
In order to allow an led to stay ON or OFF I am using TOGGLE. This code works pretty good. I attempted to use boolean but don't really need it?
The led stays on until the button is pressed a second time.
I inserted the delayms to aid in a debounce. it works better than without.
any suggestions on improvements?

while true
if sw0 = 0 //pushbutton 0
then
delayms(50)
'player1 = true
toggle (led0) //led0
else sw0 = 1
end if

if sw1 = 0
then
delayms(50)
'player2 = true
toggle (led1)
else sw1 = 1
end if

if sw2 = 0
then
delayms(50)
' player3 = true
toggle (led2)
else sw2 = 1
end if

if sw3 = 0
then
delayms(50)
' player4 = true
toggle (led3)
else sw3 = 1
end if

if sw4 = 0
then
delayms(50)
' player5 = true
toggle (led4)
else sw4 = 1
end if

if sw5=0
then
delayms(50)
' player5 = true
toggle (led5)
else sw5 = 1
end if

if sw6 = 0
then
delayms(50)
' player5 = true
toggle (led6)
else sw6 = 1
end if

if sw7=0
then
delayms(50)
toggle(led7)
endif

wend
 
Last edited by a moderator:

Visitor

Active Member
any suggestions on improvements?
Yes. Please learn to use code tags. Type exactly the following:

SmartSelect_20200201-172108_DroidEdit.jpg

Toggling the LEDs means changing the state of the LED. If the state of the LED gets out of sync with the player status, you'll get the wrong indication. Better to have a positive relationship between player status and LED status to ensure the expected results.
 

Pommie

Well-Known Member
Most Helpful Member
There used to be a function called [noparse] that would allow you to post tags without them being interpreted. Has it been completely removed or replaced with something else?

Mike.
 

be80be

Well-Known Member
Code:
while true
if sw0 = 0 //pushbutton 0
  then
     delayms(50)
    'player1 = true
    toggle (led0) //led0
 else sw0 = 1
 end if

if sw1 = 0
  then
     delayms(50)
    'player2 = true
    toggle (led1)
  else sw1 = 1
end if

if sw2 = 0
  then
     delayms(50)
   ' player3 = true
   toggle (led2)
 else sw2 = 1
end if

if sw3 = 0
   then
     delayms(50)
   ' player4 = true
    toggle (led3)
  else sw3 = 1
end if

if sw4 = 0
 then
    delayms(50)
  ' player5 = true
   toggle (led4)
 else sw4 = 1
end if

if sw5=0
   then
     delayms(50)
   ' player5 = true
    toggle (led5)
  else sw5 = 1
end if

if sw6 = 0
   then
     delayms(50)
   ' player5 = true
    toggle (led6)
  else sw6 = 1
end if

if sw7=0
    then
    delayms(50)
    toggle(led7)
end if
 

Pommie

Well-Known Member
Most Helpful Member
How can sw0 etc be both input and output?
I.E.
Code:
if sw0 = 0          //input
else sw0 = 1        //ouput
Mike.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Treid to put the original code in code braces but the indentation was miles out..... so I didn't.

I think the "if else" has been misunderstood.. The else statement isn't necessary maybe this is what was intended
Code:
if sw0 = 0
   toggle (led4)
else if sw0 = 1
  ' do nothing
end if
 

Pommie

Well-Known Member
Most Helpful Member
I'm now guessing sw0 is a variable that has previously been populated.

Mike
 

MrDEB

Well-Known Member
Ian has correct info for the sw0=1 else DO NOTHING.
SW0 TO SW9 are the ten switch inputs. I made them all HIGH and inputs using pull-up resistors.
One addition I am thinking of is eliminate the DELAYMS(XXXX) and use one DEBOUNCE SUB.
I ran the code without the DELAYMS(XXX) and it seems to run better with the DELAYMS(XXX)
I tried using Boolean but is it better using or not?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Ian has correct info for the sw0=1 else DO NOTHING.
SW0 TO SW9 are the ten switch inputs. I made them all HIGH and inputs using pull-up resistors.
One addition I am thinking of is eliminate the DELAYMS(XXXX) and use one DEBOUNCE SUB.
I ran the code without the DELAYMS(XXX) and it seems to run better with the DELAYMS(XXX)
I tried using Boolean but is it better using or not?
If that be the case then al you need is the if... Forget the else....

If switch = 0 then toggle(led) etc... end of..
 

MrDEB

Well-Known Member
cleaned up code, got rid of the fluff and found DELAYMS(500) to work better
Code:
While true
    If sw0 = 0      //pushbutton 0
        Then
        DelayMS(500)  //debounce
        Toggle (led0)  //led0
     End If 
     
        If sw1 = 0
            Then
            DelayMS(500)
            Toggle (led1)
          End If
decided a sub route wouldn't achieve anything. The delay doesn't kickin until the switch is pressed. DUH!
 
Last edited by a moderator:

hexreader

Member
My thoughts - take them or leave them....

1 Please read post # 2 carefully. Visitor gave really good advice and you seem to have ignored or misunderstood it
2 Please post full code for better help. I promise that nobody will steal it
3 Please comment your code much, much, much better, so we can have a clue as to your intentions, and to help you understand your own ideas better

EDIT: I see that "a moderator" fixed issue 1 while I was posting...
code tags - ignore
 
Last edited:

Visitor

Active Member
Congratulations on finally using code tags properly. Everyone who looks at your posts appreciates it.
 

MrDEB

Well-Known Member
I followed visitors advice in post 2 but it didn't display correctly?
here is my complete code to date.
Code:
Device = 18F43k22 ' Tell the compiler what chip we are using
Clock = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
Include "IntOSC.bas"  //don't really need
Include "SetDigitalIO.bas"
Include "Utils.bas"
'INCLUDE "Utils.bas"
'Config MCLRE = OFF
'CONFIG fOSC = INTIO67 ' Internal oscillator, IO on pins 6 and 7 remove?
'Config boren = off

'Dim count As Byte     //for keeping track of start of game after et selection
'Dim x As Byte
'Dim Time As LongWord
'Dim Et_time As LongWord

//blue leds
Dim led0 As PORTC.0
Dim led1 As PORTC.1
Dim led2 As PORTC.2
Dim led3 As PORTC.3
Dim led4 As PORTC.4
Dim led5 As PORTC.5
Dim led6 As PORTC.6
Dim led7 As PORTC.7
Dim led8 As PORTD.2
Dim led9 As PORTD.3

Dim sw0 As PORTB.0
Dim sw1 As PORTB.1
Dim sw2 As PORTB.2
Dim sw3 As PORTB.3
Dim sw4 As PORTB.4
Dim sw5 As PORTB.5
Dim sw6 As PORTB.6
Dim sw7 As PORTB.7
Dim sw8 As PORTD.0
Dim sw9 As PORTD.1

SetAllDigital
Input(sw0)
Input(sw1)
Input(sw2)
Input(sw3)
Input(sw4)
Input(sw5)
Input(sw6)
Input(sw7)
Input(sw8)
Input(sw9)

Output(led0) 
Output(led1)  
Output(led2) 
Output(led3)  
Output(led4) 
Output(led5)  
Output(led6) 
Output(led7)  
Output(led8) 
Output(led9)

led0=0
led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
led7=0
led8=0
led9=0
sw0 = 1
sw1 = 1
sw2 = 1
sw3 = 1
sw4 = 1
sw5 = 1
sw6 = 1
sw7 = 1
sw8 = 1
sw9 = 1


'Trisa=%00000000
'TrisB=%11111111   'make all INputs
'TrisC=%00000000   'make all outputs
'Trisd=%00000000
While true
    If sw0 = 0      //pushbutton 0
        Then
        DelayMS(500)  //debounce
        Toggle (led0)  //led0
     End If  
      
        If sw1 = 0
            Then
            DelayMS(500)
            Toggle (led1)
          End If
   
             If sw2 = 0
                Then
                DelayMS(500)
                 Toggle (led2)
              End If
        
                If sw3 = 0
                    Then
                    DelayMS(500)
                    Toggle (led3)
                 End If
           
                    If sw4 = 0
                        Then
                        DelayMS(500)
                        Toggle (led4)
                    End If
            
                        If sw5=0
                            Then
                            DelayMS(500)
                            Toggle (led5)
                        End If    
                 
                    
                            If sw6 = 0
                                Then
                                DelayMS(500)
                                Toggle (led6)
                              End If
                 
                                If sw7=0
                                     Then
                                     DelayMS(500)
                                     Toggle(led7)
                                  EndIf
   
    Wend
I now need to figure out how to either read a 2 x 5 keypad or output to a 2 x 5 led matrix.
looking at the KEYPAD16 module for inspiration etc.
building my project one section at a time.
hopefully my code posting is correct?
this code is an altered version of similar project but why reinvent the wheel?
 
Last edited by a moderator:

Visitor

Active Member
Me! Sorry... I have to.. Its the law!!!
Thanks for cleaning up! Did you also clean up his previous post? The one I thought by some miracle he had finally done correctly?

Perhaps someone should explain that he can and should read his posts after posting and that he can and should edit them to be correct.
 

Latest threads

EE World Online Articles

Loading

 
Top