Electronic Circuits and Projects Forum


would anyone like to neaten my code please?

Results 1 to 4 of 4
Reply to Thread
  1. #1
    bigal_scorpio
    bigal_scorpio is offline

    Default would anyone like to neaten my code please?

    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.
    The Doctor just told me I have short term memory problems and he told me I have short term memory problems!

  2. #2
    nickelflippr
    nickelflippr is offline
    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

  3. #3
    Pommie
    Pommie is offline
    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.

  4. #4
    dougy83
    dougy83 is offline
    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

Reply to Thread

Similar Threads

  1. 16F84a code to 16F628a code
    By JKF1000 in Microcontrollers
    Replies: 18
    Latest: 17th October 2009, 05:06 PM
  2. 16F84 Code>16F628 Code
    By Carole Wilosn in General Electronics Chat
    Replies: 3
    Latest: 30th September 2009, 01:37 PM
  3. in need of code
    By butters in Microcontrollers
    Replies: 1
    Latest: 6th June 2009, 07:55 AM
  4. Replies: 1
    Latest: 13th August 2007, 05:45 PM
  5. some testing code.... of code of 89C51 for LCD
    By kumar_3k in Electronic Projects Design/Ideas/Reviews
    Replies: 0
    Latest: 5th November 2003, 05:23 PM