Making a motor work without using class structure


I have a piece of code that works, but am wondering if there is way to get it to work without instantiating a BrickPi3 object at the start.

At present my code looks like this:

import brickpi3
import time

BP = brickpi3.BrickPi3() 
speed = 30
stop = 0

inkey = raw_input()
if inkey == " ":
   print ("running forward")
   BP.set_motor_power(BP.PORT_A, speed)
   print ("stop")
   BP.set_motor_power(BP.PORT_A, stop) 

It will run a motor for 2 seconds when the space bar is pressed followed by enter.
I want to make it simpler for students so they don’t need to worry about classes. Any ideas. Also is there a simple way to get the motor to work by pressing the spacebar without having to then press the enter key?

Any ideas most appreciated.


Instantiation creates the object to work with, and it also does a little hardware configuration, confirms that the BrickPi3 is connected, and ensures it is running a version of firmware compatible with the Python drivers.

If you don’t want to have to explain what BP = brickpi3.BrickPi3() does, perhaps just group it with import brickpi3 and explain it as “a necessary line of code to use the BP3”.


Hi @shabysheik1,

In order to use the keyboard for controlling the robot I’d use curtsies package.
Please take a look at how the following project is implemented:

Thank you!