# data[].attributes.body
Slide 21:

from hub import light_matrix, sound

import runloop, time

Slide 25 Driver:

#import our necesarry libraries
import motor
from hub import port, button
import runloop

async def main():
    #make the hand open once to start
    motor.run_for_time(port.F, 1000, 300)
    #begin checking for button presses
    runloop.run(checkButtons())
#A function that calls itself to keep checking the buttons
async def checkButtons():
    #when the button is not pressed, open the hand
    while not button.pressed(button.LEFT):
        motor.run(port.F, 300)
    #when the button is pressed, close the hand.
    while button.pressed(button.LEFT):
        motor.run(port.F, -300)
    #loop the function
    runloop.run(checkButtons())


runloop.run(main())

# data[].attributes.body
Slide 9: (motor command incomplete)A

#import libraries

from hub import light_matrix, port

import motor

import runloop

async def main():

# rotate clockwise for 2 secs at 75 degrees per second

motor XXXX

runloop.run(main())

Slide 12 (Await commands incomplete)

async def main():

# rotate the motor to position 0

await motor.run_to_absolute_position(port.C,XXX

# rotate the motor 360 degrees clockwise

await motor.run_for_degrees(port.C, XXX

# how will this motor rotate?

await motor.run_for_degrees(port.C, XXX

runloop.run(main())

Reuse the above code for slides 13 & 15

Slide 28

# Import libraries

from hub import light_matrix, port

import motor

import runlop

Slide 29

#use a variable to set the driving speed

driving_speed = XXX

#display the speed on the hub

Slide 30

#definte a function for riding

async def ride():

#Display the speed on the hub

await light_matrix.write(str(driving_speed))

motor.run_for_time(port.C, XXXXX

motor.run_for_time(port XXXXXX

runloop.run(ride())

Slide 36

# change the speed of the bike when going uphill

while True:

#get the current angle of the motion sensor

angle = motion_sensor.tilt_angles()

print(angle)

driving_speed = 600

#if the angle is greater than 20, you are going uphill

if angle[1] >= XXX

driving_speed = driving_speed - XXX

runloop.run(ride())

break

else

runloop.run(ride())

# data[].attributes.body
Slide 11

# import libraries

from hub import light_matrix, port

import runloop, force_sensor, motor_pair

#set variables

fs = XXXX

motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)

cir = motor_pair.PAIR_1

Slide 17

# set speed of motors

motor_speed = XXXX

#start moving when force sensor is pressed

async def main():

if force_sensor.pressed(fs):

motor_pair.move(car, XXX, velocity = XXX)

else

motor_pair.XXXXX

while True:

runloop.run(main())

Slide 21

# import libraries

from hub import light_matrix, port

import runloop, color_sensor, motor_pair

#set variables

cs = XXXX

Slide 22

motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)

car = motor_pair.PAIR_1

# set speed of the motors

motorXXXXX

Slide 25

# import libraries

from hub import light_matrix, port

import runloop, color_sensor, motor_pair, color

#set variables

cs = port.B

motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)

car = motor_pair.PAIR_1

# set speed of the motors

motorXXXXX

async def main():

while True:

if color_sensor.color(cs) is not color.BLACK:

motor_pair.move(car, XXX, velocity = XXXX)

else

motor_pairXXXXX

runloop.run(main())

Slide 30 (lines 15-18)

if color_sensor.color(cs) is not color.BLACK:

motor_pair.move_tank(car,0,motor_speed)

else

motor_pair.move_tank(XXXX

Slide 43

# import libraries

from hub import light_matrix, port

import runlook, motor_pair, motor, XXXX

#pair your drive motors

motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)

# initialize variables

driveMotors = motor_pair.PAIR_1

arm = XXXXX

distanceSensor = port.F

Slide 48

#set speed

driveSpeed = XXX

armXXX

# define the function

async def main():

Slide 49

#set arm in place

await motor.run_for_time(arm, 500, armSpeed)

await motor.run_for_degrees( XXXX

#beep to show the arm is ready

await sound.beep(XXXX

await sound.beap(510, 500)

#run the function

runloop.run(main())

Slide 54

distance = 200

async def move():

#loop the movement

while True:

runloop.run(move())

Slide 55

distance = 200

async def move():

if distance_sensor.distance(distanceSensor) > XXX

motor_pair.move(driveMotors, 0, velocity = driveSpeed)

else

motor_pair.stop(driveMotors)

Slide 58

else

motor_pair.stop(driveMotors)

# drop the arm

await motor.run_for_degrees(arm, XX, -armSpeed)

#beep to show the robot has picked up the object

await sound.beep(440, 500)

await sound.beep(XXXX

# move backwards and lift arm

await motor_pair.move_for_time(driveMotors, 1500, 0, veolocity=-driveSpeed)

await motor.run_for_degrees(arm, XX, armSpeed)

# data[].attributes.body
# These lines import the functions of the hub we use to make our code.
from hub import light, light_matrix, motion_sensor, button
import runloop
step = 0
true_step = 0

#This function is defined here with async def
async def main():
    global step
    global true_step
    #Retrieve the yaw angle
    yaw = motion_sensor.tilt_angles()[0]
    if yaw > 100 and true_step == 0:
        step += 1
        true_step = 1
    elif yaw < -100 and true_step == 1:
        step += 1
        true_step = 0
    # Press the left button to vview how many steps you have walked
    if button.pressed(button.RIGHT):
        step = 0
        await light_matrix.write(str(step))
    print("Yaw Angle: " + str(yaw))


while True:
    runloop.run(main())

# data[].attributes.body
from hub import port
import runloop, motor
# initialize motors
motor_left = port.B
motor_right = port.F

async def main():
    # write your code here
    await motor.run_to_absolute_position(motor_left, 0, 440)
    await motor.run_to_absolute_position(motor_right, 0, 440)


    # Run motors in alternating moves
    for count in range (10):
        await motor.run_for_time(motor_right, 200, 600)
        await motor.run_for_time(motor_left, 200, -600)

        await motor.run_for_time(motor_right, 200, -600)
        await motor.run_for_time(motor_left, 200, 600)

runloop.run(main())