Before building the remote control part of the project, we want to make sure that the hardware is wired correctly, especially between the Arduino Uno board and the different motors and sensors. This is why we are first going to build a simple sketch for the Arduino Uno board to test the different components.
At this point, we are going to turn the motors of the robot on; so make sure the robot is standing on a small platform, for example, to prevent it from moving around while you are testing your different Arduino sketches with the USB cable connected to your computer.
The sketch starts by declaring the pins for the motors, as shown in the following code. Note that these pins are specifically for the motor shield we are using; please refer to the datasheet of your shield if you are using a different one.
int speed_motor1 = 6; int speed_motor2 = 5; int direction_motor1 = 7; int direction_motor2 = 4;
Declare the pin used by the ultrasonic sensor as follows:
int distance_sensor = A0;
We also want to make the speed of the motor vary during operation, so we declare the variable as follows:
int robot_speed;
In the setup()
part of the sketch, we need to specify that the motor pins will behave as output pins, as shown in the following code:
for(int i=4;i<=7;i++) { pinMode(i, OUTPUT); }
We also need to set a starting speed for the robot. Note that the speed of each motor will be set by PWM commands coming from the Arduino, so we have to specify a value between 0 (no voltage applied to the motor) and 255 (maximum voltage applied to the motor). Also, because of mechanical resistance on the motors, there is no linear relation between the value of the PWM command and the speed of the motor.
We used the value 75 as a starting speed, which is a very slow speed on our DC motors. However, depending on your own setup, this value will have a completely different effect. At this point, you can also experiment to see what the maximum PWM value is that will give you exactly zero speed on your DC motors. Make sure that the robot is not on the floor just yet as it would start to move forward and possibly damage things. We put it on a small stand so the wheels don't touch anything.
In the loop()
part, everything is done by the function send_motor_command
, which will be called for both motors. For example:
send_motor_command(speed_motor1,direction_motor1,robot_speed,1);
Let's see the details of this function. It starts by writing the speed of the motor on the correct pin as follows:
analogWrite(speed_pin,pwm);
Then, we need to set the the direction pin to the correct direction. This is done by a simple digitalWrite
function as follows:
digitalWrite(direction_pin,dir);
Still in the loop()
function, we call a function to measure the distance in front of the robot and print the result on the Serial
port:
Serial.println(measure_distance(A0));
Let's see the details of this function. It starts by getting the raw measurement from the sensor using the pulseIn
function. Basically, the sensor returns a pulse whose length is proportional to the measured distance. The length of the pulse is measured with the following function of Arduino dedicated for that purpose:
unsigned long DistanceMeasured=pulseIn(pin,LOW);
Then, we check whether the reading is valid and if it is, we convert it to centimeters using the following formula:
Distance=DistanceMeasured/50;
This is returned with the following code:
return Distance;
Finally, we update the speed at every iteration of the loop by increasing it by one unit, and we reset it if it reaches 255, as shown in the following code:
robot_speed++; if (robot_speed > 255) {robot_speed = 75;}
The code for this section is available at the GitHub repository of the book and is stored in a file called robot_test
: https://github.com/openhomeautomation/geeky-projects-yun/tree/master/chapter4/robot_test
It's now time to upload the code to the robot. Before doing so, please make sure that the robot is powered by the battery. Both motors of the robot should gradually accelerate upon reaching the maximum speed and then start again at a lower speed.
You can also open the serial monitor at this point to check the readings from the distance sensor. Try moving your hand or an object in front of the robot; you should see the distance changing accordingly on the serial monitor.