would anyone like to neaten my code please?

Status
Not open for further replies.

bigal_scorpio

Active Member
Hi to all,

I am not yet very good with Microprocessors and am trying to learn MikroBasic. But being a late starter I am finding it difficult to get my head round.

I have written a program to give 3 pulses when the ignition of the car is turned on, then it must wait until the ignition is turned off and again send 3 pulses.

I have managed to get a working program but a friend looked at it and said it could be done much easier and smaller, but seemed unwilling or unable to explain how, so could anyone who uses basic have a quick look and advise me what I SHOULD be doing.

Thanks........Al

Code:
'Program to send 3 pulses on ignition being turned on
'then waiting for ignition off to send another set of 3 pulses
' using 12F629 with MCLR off, BODEN off, PWRTE on, WDT off, INT OSC No Clockout
'Power to GPIO.0 from ignition through voltage divider



program tripleflash3

dim t as byte

main:
     TRISIO = %00000001
     CMCON = 7
    if Button(GPIO,0,1,0) then
    goto main
    end if
     
    if Button(GPIO,0,1,1) then
      For t = 1 to 3
      GPIO.1 = 1
      Delay_ms(100)
      GPIO.1 = 0
      Delay_ms(100)
      next t
    end if
waiter:
    if Button(GPIO,0,1,1) then
    goto waiter
   end if
         For t = 1 to 3
      GPIO.1 = 1
      Delay_ms(100)
      GPIO.1 = 0
      Delay_ms(100)
      next t
      goto main

end.
 
Don't use MikroBasic, but using an interrupt on change might look nicer. But being in an auto or motorcycle circuit could make it vulnerable to noise. Try a nice fat RC filter like 1k/10uf on GPIO input. So using some generic ideas, try and implement however you do in MikroBasic:

Code:
IOC5 = 1  ;Set interrupt on change for GPIO.0

On Interrupt GPIOChange Call Ignition  ;Call sub when GPIO.0 changes
count = 0

Main:
If count = 1 Then call FlashLed
goto Main

Sub Ignition
     count = 1  ;Or boolean True
end sub

Sub FlashLed
     count = 0  ;reset interrupt to False
     ...            ;blinky stuff
     ...
end sub
 
This is how I would tidy your code,
Code:
'Program to send 3 pulses on ignition being turned on
'then waiting for ignition off to send another set of 3 pulses
' using 12F629 with MCLR off, BODEN off, PWRTE on, WDT off, INT OSC No Clockout
'Power to GPIO.0 from ignition through voltage divider



program tripleflash3

dim t as byte

main:
    TRISIO = %00000001
    CMCON = 7
    while TRUE				
        while Button(GPIO,0,1,0)	
        wend
     
        if Button(GPIO,0,1,1) then
            For t = 1 to 3
                GPIO.1 = 1
                Delay_ms(100)
                GPIO.1 = 0
                Delay_ms(100)
            next t
        end if

        while Button(GPIO,0,1,1)
        wend
        For t = 1 to 3
            GPIO.1 = 1
            Delay_ms(100)
            GPIO.1 = 0
            Delay_ms(100)
        next t
    wend

end.

All I have done is remove any gotos and replaced them with while..wend and indented to show where while..wend, for...next and if...endif start and end.

Mike.
 
You could try somethink like this:
Code:
program tripleflash3

Dim t As Byte

    TRISIO = %00000001
    CMCON = 7

    Do While 1
        Do While Button(GPIO, 0, 1, 0)      ' wait for some state...
        Loop
         
        If Button(GPIO, 0, 1, 1) Then
           TripleFlash()
    
            Do While Button(GPIO, 0, 1, 1)      ' wait for some state...
            Loop
        End If
        
        TripleFlash()
    Loop

sub procedure TripleFlash()
    Dim t As Byte
    
    For t = 1 To 3
        GPIO 0.1 = 1
        Delay_ms (100)
        GPIO 0.1 = 0
        Delay_ms (100)
    Next t

End Sub

end.
I don't program in MikroBasic, so the above may need some tweaking to compile.

You'll notice that
* a subroutine is used to replace duplicated code,
* the busy loops e.g.
Code:
label:
   if something then goto label
are replaced with
Code:
do while something : Loop
* no more gotos - potentially more readable, and you'll be less likely to write spaghetti code
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…