Build cool stuff with littleBits, a Pi, and some Lego Bricks
Arduino Programs
Once the Arduino IDE is installed on the PC, you can start writing littleBits Arduino applications. In the Arduino desktop application, you need to set the board type by clicking Tools | Board; then, select Arduino Leonardo. To choose the serial port on the PC that talks to the Arduino, click Tools | Serial Port and select the appropriate COM port from the list. For our PC setup it was COM33.
As a first test, we used the littleBits Wires modules to connect the motors to the d1 and d9 connectors on the Arduino module, and then we wrote a program to start and stop both motors (Listing 1) [4]. Lines 4 and 5 set up the connectors as outputs, and the loop in lines 7-15 starts and stops the motors. To upload this sketch to the Arduino, click the Upload arrow in the IDE toolbar.
Listing 1
motortest.ino
01 // Simple Motor Test 02 void setup() { 03 //define the pins 1 and 9 for the motors 04 pinMode(1, OUTPUT); 05 pinMode(9, OUTPUT); 06 } 07 void loop() { 08 // run the motors for 2 sec on stop for 2 sec 09 digitalWrite(1, 1); 10 digitalWrite(9, 1); 11 delay(2000); 12 digitalWrite(1, 0); 13 digitalWrite(9, 0); 14 delay(2000); 15 }
The next step was to get serial communications working so we could send commands from the keyboard (Listing 2). The statement Serial.begin(9600);
in line 9 defines serial communication at 9600 baud, and the statement Serial.read()
(line 14) reads the input from the USB port. The commands that control the motors (lines 16-35) are:
- s – stop both motors
- g – go, run both motors
- l – turn left
- r – turn right
Listing 2
littlebits1.ino
01 // Serial Command Test 02 03 char thekey; 04 05 void setup() { 06 //define the pins 1 and 9 for the motors 07 pinMode(1, OUTPUT); 08 pinMode(9, OUTPUT); 09 Serial.begin(9600); 10 Serial.println("littleBits Arduino Rover Control"); 11 Serial.println("Enter a command : s - stop , g - go \ , l - left, r - right"); 12 } 13 void loop() { 14 if (Serial.available() > 0) { 15 thekey = Serial.read(); // get the key from the phone 16 17 // "s" stop both motors 18 if (thekey == 's') { 19 digitalWrite(1,0); 20 digitalWrite(9,0); 21 } 22 // "g" run both motors 23 if (thekey == 'g') { 24 digitalWrite(1,1); 25 digitalWrite(9,1); 26 } 27 // "l" only run right motor, turn left 28 if (thekey == 'l') { 29 digitalWrite(1,1); 30 digitalWrite(9,0); 31 } 32 // "r" only run left motor, turn right 33 if (thekey == 'r') { 34 digitalWrite(1,0); 35 digitalWrite(9,1); 36 } 37 } 38 }
The complete code for the Arduino module is fairly simple, but it could be expanded to support commands to the d5 connector to control lights or play music, for example.
To test serial communications, open up the Serial Monitor with the toolbar icon on the far right (Figure 5), type one of the control commands, and click Send.
Raspberry Pi Code
After we proved that we could control the littleBits motors from the Arduino monitor, we wanted to send the same commands to the Arduino using the Raspberry Pi (Figure 6).
The first step was to ensure that the Rasp Pi could see the littleBits Arduino module on the USB cable. The Linux command lsusb
should find the module. On the Raspberry Pi, the USB port is named /dev/ttyACM0.
The small Python program in Listing 3 sends commands to the rover. Text editors like Nano can be used to create the Python files.
Listing 3
lb_test0.ino
01 import serial 02 03 port = serial.Serial("/dev/ttyACM0", 9600) 04 05 keycode = " " 06 print "Drive the Rover from Pi" 07 print "Commands: g=go, s=stop, l=left, r=right, or x=exit" 08 while keycode <> "x": 09 keycode = raw_input("Enter a command: ") 10 port.write(keycode)
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 6
(incl. VAT)