include( filename )

include mpy file


Syntax: include( filename )
Description: Used to include another file into a mpy program
Parameters: filename Name of file to be included
Returns: Nothing
Related:


Example:

include('servo_control.mpy')
servo_setup(0, P1_0)

while 1:
    servo( 0, 30 )
    wait(1000)
    servo( 0, 70 )
    wait(1000)

Then in file servo_control.mpy you would define functions

def servo_setup( servo_motor_number, portpin ):
    pindir(portpin,PULSEOUT)
    pulse_enable(servo_number)
def servo( servo_number, position ):
    period_min = 6200
    period_max = 25000
    period_scale = (period_max - period_min) / 100
    if position < 0:
        position = - position
    a = period_min + position * (period_scale)
    adiv = a/10
    pulse_period(servo_number, a)
    pulse_width(servo_number, adiv)

This example shows how the include() command can be used to place commonly used functions into a separate include file. By doing this the main program can be kept simple.  The include file can be re-used in your other mpy programs. It saves you having to rewrite your functions definitions everytime you need to use it in a mpy program.

This example uses the pulse functions to control a servo motor,  see here for details on the pulse commands