Menu

Help! Trying to compile into hex but get "missing end sub definition"

2024-01-09
2024-01-09
  • Chase Maillette

    Chase Maillette - 2024-01-09

    Attached is the. GCB file.

    The Error code comes from Ln 55, Col 1

     
  • Anobium

    Anobium - 2024-01-09

    Hello Chase/Nathan

    I am guessing that you are copying you source program from another language.

    You have incorrect syntax for the use of GOSUB. The Gosub command is used to jump to a label as a subroutine, in a similar way to Goto. The difference is that Return can then be used to return to the line of code after the Goto. Gosub should NOT be used if it can be avoided.

    So, in your code.

    'Reads sensor values 
    Sub readSensors
        DigitalFlame = ReadAD(AN2)
        DigitalWallFront = ((6787/(ReadAD(AN1)-3))-4)/5
        DigitalWallLeft = ((6787/(ReadAD(AN0)-3))-4)/5
    return 
    

    You need to change the sub NAME to NAME: which is a label.

    However, use proper subroutines The name of the sub subroutine and end sub. So, in your code.

    'Reads sensor values 
    Sub readSensors
        DigitalFlame = ReadAD(AN2)
        DigitalWallFront = ((6787/(ReadAD(AN1)-3))-4)/5
        DigitalWallLeft = ((6787/(ReadAD(AN0)-3))-4)/5
    End Sub 
    

    Note... changed in the return to end sub, then you need to remove the gosub from the calling lines.

    The difference? Using GoSub cannot be optimised by the compiler. You prevent any optimisation of the generated hex file. So, use Sub - End Sub.


    Also, you ADC calc.... ((6787/(ReadAD(AN1)-3))-4)/5 this is horrid.

    You will get the microcontroller overflows - this is where the microcontroller cannot cope with the maths and during the intermediate maths you will get incorrect results.

    Case where the ADC value is 0x00. Your calc will return 1356...
    Case where the ADC > 0 and ADC < 30. Your calc will overflow with values that are greater than 255 ( a byte value ).
    And, the calc is very slow.

    Consider using SCALE(). DigitalWallFront = Scale( ReadAD( AN1 ), 0, 255, 225, 4 ) // I am guessing the value of 225.....

    You should model this range in EXCEL. Remember to use INT() to simulate the integer nature of the microcontroller. You will see the overflows and the division by zero, negative numbers... So, using Scale() will control the returned range.


    Enjoy,

    Evan

     
  • Anobium

    Anobium - 2024-01-09

    and, figure this out DigitalWallFront =[byte]Scale( ReadAD(AN1), 0, 255, 200, 4)

    The line of code adapts scale(). Try to understand why this adaption works, and, it will be slightly faster.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.