Most people already know by now that programming is what controls the operation of computerized devices. A program is a list of instructions and must be written in a language that can be understood by the device. In the case of the Arduino, this can be any number of languages such as SQL, Java, Python, or C++.
To program the Arduino robot, the first requirement will be to download the Arduino software to your computer. The software download can be found here.
The Arduino Integrated Development Environment or Arduino Software (IDE), has virtually everything you need to write programs(sketches) and to transfer them to the Arduino. These sketches are written in the text editor and are saved with the file extension .ino. The editor has features for cutting/pasting and for searching/replacing text.
Once the Arduino Software (IDE) is downloaded, you then need to setup the environment menu to Tools and select the type of Arduino you want to program, in our case it’s the Arduino UNO R3. The text editor screen will then appear. You can type or paste the code for your sketch into the editor. no. To transfer the program from your computer to the Arduino board, you need a USB cable. Once connected the Arduino’s onboard LED will start blinking and you are all set to load the sketch into the Arduino.
In order to write the sketch to control your object avoidance robot, you will need to have noted the pins you used on the Arduino board to connect the external components. These are listed below.
Digital ports (pins on the Arduino board) 11(go), 12(danger) and 13(power on) are used to supplypower to the LED circuits connected Arduino.
The TRIG pin of the sensor HC-SRO4 is connected to digital port 6 on the Arduino ;
The ECHO pin of the sensor HC-SRO4 is connected to digital port 7 of the Arduino;
The GND pin of the sensor HC-SRO4 is connected to GND on the Arduino ;
The PWR pin of the sensor HC-SRO4 is connected to 5 Volt pin on the Arduino;
The control lead of the left servo is connected to pin 9 of the Arduino;
The control lead of the right servo is connected to pin 10 of the Arduino;
I have written a sketch to control the object avoidance robot just to allow you to see from the comments how it works. The Comments follow the // on each line of the sketch. The // makes the comment invisible to the text editor.
//Sketch to control the object avoidance robot
#include //Include Servo Library
int trigPin = 7; //creates variable “trigPin” whose value is 7
int echoPin = 6; //creates variable “echoPin” whose value is 6
int gopin = 11; //creates variable “gopin” whose value is 11
int dangerpin = 12 //creates variable “dangerpin” whose value is 12
Servo servoLeft; //names one servo object “servoLeft”
Servo servoRight; //names one servo object “servoLeft”
int forward = 180; // full speed forward
int backward = 0; // full speed backward
void setup() { //run only once at beginning of the program
servoLeft.attach(9); // Set left servo to digital pin 9
servoRight.attach(10); // Set right servo to digital pin 10
Serial.begin (9600); //Sets the data rate 9600 (baud)
pinMode(LED_BUILTIN, OUTPUT); //LED pin 13 used as blinking power indicator
pinMode(trigPin, OUTPUT); //The Trig pin will be used to send the signal
pinMode(echoPin, INPUT); //The Echo pin will be used to receive the return
}
void loop() //Runs ONCE at the very beginning of your program
{
digitalWrite(LED_BUILTIN, HIGH); //turns on blinking power indicator(blue)
digitalWrite(gopin, HIGH); //turns on safe to go LED indicator(green)
servoLeft.write(forward); //sets both servos to full speed in the same direction servoRight.write(backward) //sets both servos to full speed in the same direction
digitalWrite(trigPin, LOW); //prepare to send initial ultrasonic pulse
delayMicroseconds(2); //prepare to send initial ultrasonic pulse