Coding for Arduino 2
Space Rover
Module 5, Project 2: Space Rover
Use the following guide to build the CTC-101 Space Rover project.
You will be able to control the rover to explore alien planets!
Getting around on alien planets can be difficult. You are going to need a rover to investigate these new alien worlds. Now you will be able to control the rover to explore alien planets.
Materials
- 1 x control board
- 1 x education shield
- 2 x continuous rotation servo
- 7 x jumper wire
- 1 x 9V battery
- 4 x AA battery
- 1 x AA battery holder
- 2 x power plug (One without the plug, that has two loose wires)
- 1 x Space rover kit
- 1 x metal ball
- 2 x wheel
Other materials:
- 1 x Android phone with Bluetooth
Step 1
Gather all the construction materials for the Space rover, it uses many of the parts form the Line follower!
Step 2
Make sure to screw the wheels to the continuous servos.
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Step 14
Step 15
Step 16
Step 17
Step 18
Step 19
Step 20
Step 21
Step 22
Attach the shield onto the top of the board.
Step 23
Connect one jumper wire from the GND, to the breadboard.
Step 24
Step 25
Using 3 jumper wires, connect the right servo. The black wire to GND, the white wire to digital pin 6, and the red wire to the breadboard.
Step 26
Using 3 jumper wires, connect the left servo. The black wire to GND, the white wire to digital pin 9, and the red wire to the breadboard.
Step 27
Prepare a 6 V battery with loose wires connector.
Step 28
Using the power plug with loose wires, connect the battery pack to the shield breadboard, black wire to GND and the red wire to the two servos’ power wires.
Step 29
Step 30
Check that your wiring is ready and connect the board to the computer.
Step 31
Find the Spacerover program and open it.
/*
* SpaceRover
*
* Getting around on alien planets can be difficult.
* You are going to need a rover to investigate these new alien worlds.
* Now you will be able to control the rover to explore alien planets.
*
* (c) 2013-2016 Arduino LLC.
*/
#include <EducationShield.h>
BLEuart ble = BLEuart(TYPE_LOGOROBOT);
Wheels wheels = Wheels(6, 9);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ble.setName("Rover");
ble.begin();
wheels.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if (ble.searchCentral()) {
Serial.println("Connected to central ");
while (ble.connected()) {
if (ble.dataReceived()) {
ble.fetchData();
Serial.print("Received number of commands: ");
Serial.println(ble.getReceivedLength());
for (int i = 0; i < ble.getReceivedLength(); i++) {
unsigned char n = ble.receivedString()[i];
Serial.print(n);
logoMove(n);
}
Serial.println();
}
}
Serial.println(F("Disconnected from central "));
}
}
void logoMove(unsigned char direction) {
switch (direction) {
case 1: //forward
wheels.goForward();
delay(750);
break;
case 2: //backwards
wheels.goBackwards();
delay(750);
break;
case 3: //left
wheels.turnLeft();
delay(250);
break;
case 4: //right
wheels.turnRight();
delay(250);
break;
}
wheels.standStill();
delay(300);
}
Step 32
Then upload the program to the board. Remove the USB cable, you will not run this project with it connected.
Step 33
Step 34
Step 35
Prepare a 9 V battery with a power plug and put it inside the robot.
Step 36
Step 37
Step 38
Step 39
Step 40
Step 41
Connect the 9 V battery to the board’s power socket.
Step 42
Launch the CTC app and connect it to the Space rover, to control it!
How it works
- The EducationShield library is included.
- Create an object of BLEuart class, and set the sketch type as TYPE_LOGOROBOT.
- Create the wheels object, which is used for controlling 2 servo motors as wheels of a vehicle. The left wheel is connected to digital pin 6, and right one is to digital pin 9.
- In setup, set name of the BLE device. Any name less than 8 characters is fine. Make sure it’s different from your classmates projects.
- Start BLE communication.
- In loop, search for a central device to connect to. If a connection is established, execute the following code.
- While the board is connected to a central device, execute the following code.
- When the board receives data from the central:
- Fetch the data.
- Display the length of the data, in number of characters.
- Loop through every character of the received string, the string should be composed entirely of commands for running the rover.
- Execute the command and move the wheels.
- In logoMove:
- It accepts one parameter, a character represents the direction. It should be either ‘1’,’2’,’3’ or ‘4’, meaning forward, left, backward, right respectively. Remember that they are characters instead of numbers, so it’s ‘1’ instead of 1 here. ‘1’ has an ASCII code of 49, so when compared to integers, it is the same as 49. And ‘2’ is 50, so on.
- Use switch instruction to identify and carry out the command.
- Start moving the wheels, and wait for a period of time. So the wheels will keep running for such time period.
- Afterwards, stop the wheels and wait briefly. So you can see that an action is done.
TROUBLESHOOTING
- Refer to the illustration and double check your connections. Make sure the shield and jumper wires are firmly connected
LEARN BY DOING
- Make your own commands to make the rover move in different patterns
- Give it more tools to use by adding more parts on it, and add new functions in the rover so it can use the new parts
“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”
– C.A.R. Hoare