YahBoom BatCar Programming Manual

Arduino batmobile ide programming tutorial

Advertisement

Quick Links

2019
Yahboom Arduino Batmobile
Arduino IDE Programming Tutorials
0

Advertisement

Table of Contents
loading

Summary of Contents for YahBoom BatCar

  • Page 1 2019 Yahboom Arduino Batmobile Arduino IDE Programming Tutorials...
  • Page 2: Table Of Contents

    C programming 1. Go ahead 2. Button start 3. Line Walking 4. Infrared obstacle avoidance 5. Tracking 6. Ultrasonic distance measurement 7. Ultrasonic check obstacle and avoid 8. Line Walking and check obstacle 9. Remote control...
  • Page 3: Go Ahead

    The purpose of the experiment: The purpose of the experiment in this lesson is to open the power switch of the BatCar after the program is uploaded, and the BatCar starts to move forward after 2 seconds of stationary. List of components required for the experiment:...
  • Page 4 void loop() delay(2000); //delay 2s run(10); //Run Experimental steps: 1. We need to open the code of this experiment: advance.ino, click“√” under the menu bar to compile the code, and wait for the word "Done compiling " in the lower right corner, as shown in the figure below.
  • Page 5 3.Click [Tools]---[Board]---Select Arduino Uno as shown below. 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 6 4. After the program is successfully uploaded, as shown in Figure 1, press the power switch, you can see that the BatCar starts moving forward after 2 seconds . Figure 1 Figure 2...
  • Page 7: Button Start

    The purpose of the experiment: After uploading the Button start program, turn on the power switch and press the start button K1. After a short whistle, the BatCar starts to move forward, backward, left turn, right turn, rotate left, rotate right.
  • Page 8 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,200);//PWM--Pulse Width Modulation(0~255). It can be adjusted to control speed. analogWrite(Left_motor_back,0); delay(time * 100); //Running time can be adjusted void brake(int time) //stop digitalWrite(Right_motor_go,LOW); digitalWrite(Right_motor_back,LOW); digitalWrite(Left_motor_go,LOW); digitalWrite(Left_motor_back,LOW); delay(time * 100); void left(int time) //turn left digitalWrite(Right_motor_go,HIGH);// right motor go ahead digitalWrite(Right_motor_back,LOW);...
  • Page 9 delay(time * 100); void spin_right(int time) //Right rotation digitalWrite(Right_motor_go,LOW); // right motor back off digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,200);// PWM--Pulse Width Modulation(0~255) control speed digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,200);// PWM--Pulse Width Modulation(0~255) control speed analogWrite(Left_motor_back,0); delay(time * 100); void back(int time) //back off digitalWrite(Right_motor_go,LOW);...
  • Page 10 void loop() delay(2000); //delay 2s keysacn(); back(10); //back off for 1s brake(5); //stop for 0.5s run(10);//go ahead for 1s brake(5); //stop for 0.5s left(10);//turn left for 1s right(10);//turn right for 1s spin_left(20);//Left rotation for 2s spin_right(20);//Right rotation for 2s brake(5); //stop for 0.5s Experimental steps: 1.
  • Page 11 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 12 4. Unplug the USB cable, put the in an open place, turn on the power switch, and BatCar is still at rest until the start button K1 is pressed. After a short whistle, BatCar starts to move forward and backward. Turn left, turn right, etc.
  • Page 13: Line Walking

    3- Line Walking The purpose of the experiment: After uploading the program, unplug the USB data cable, put the BatCar on the patrol tarck.Adjust the potentionmeterSW3 and SW4 so that the photoelectric sensor can recognize the black line.Press the start button K1, the BatCar starts running along the black line.
  • Page 14 digitalWrite(key,HIGH);//Initialize button digitalWrite(beep,HIGH);// set buzzer mute //=======================Motor========================= void run() digitalWrite(Right_motor_go,HIGH);// right motor go ahead digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,100);//PWM--Pulse Width Modulation(0~255). It can be adjusted to control speed. analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,HIGH);// set left motor go ahead digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,100);//PWM--Pulse Width Modulation(0~255). It can be adjusted to control speed. analogWrite(Left_motor_back,0);...
  • Page 15 digitalWrite(Right_motor_go,LOW); // right motor stop digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,100); analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control speed void spin_right(int time) //Right rotation digitalWrite(Right_motor_go,LOW); // right motor back off digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,200);// PWM--Pulse Width Modulation(0~255) control speed digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW);...
  • Page 16 if(val==LOW) //Double check the button is pressed digitalWrite(beep,LOW);//The buzzer sounds delay(50);//delay 50ms while(!digitalRead(key)) //Determine if the button is released or not digitalWrite(beep,HIGH);//mute else digitalWrite(beep,HIGH);//mute /*main loop*/ void loop() keysacn();//Press the button to start while(1) /************************************************************************************** Infrared signal back means white undersurface ,returns low level and led lights up. Infrared signal gone means black undersurface ,returns high level and led lights off.
  • Page 17 2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】--- selecting the port that the serial number displayed by the device manager just now, shown in the figure below.
  • Page 18 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 19 4. After the program is successfully downloaded, adjust potentiometers SW3 and SW4 to the correct angle. 5.Put the on the patrol tarck and press the start button K1. With a short whistle, BatCar starts running along the black line. BatCar...
  • Page 20: Infrared Obstacle Avoidance

    Debug potentiometers SW1 and SW2 according to the debug diagram at the end of the article.Turn on the power switch of the BatCar, press the start button K1, and when you hear the short whistle, the BatCar avoids the obstacle.
  • Page 21 pinMode(SensorLeft, INPUT); // Set left Line Walking Infrared sensor as input pinMode(SensorRight_2, INPUT); //Set Right Infrared sensor as input pinMode(SensorLeft_2, INPUT); //Set left Infrared sensor as input digitalWrite(key,HIGH);//Initialize button digitalWrite(beep,HIGH);// set buzzer mute //=======================Motor========================= void run() digitalWrite(Right_motor_go,HIGH);// right motor go ahead digitalWrite(Right_motor_back,LOW);...
  • Page 22 delay(time * 100); void right() //turn right digitalWrite(Right_motor_go,LOW); // right motor stop digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,100); analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control speed void spin_right(int time) //Right rotation digitalWrite(Right_motor_go,LOW); // right motor back off digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0);...
  • Page 23 delay(10); //delay 10ms val=digitalRead(key);// Reads the button ,the level value assigns to val if(val==LOW) //Double check the button is pressed digitalWrite(beep,LOW);//The buzzer sounds delay(50);//delay 50ms while(!digitalRead(key)) //Determine if the button is released or not digitalWrite(beep,HIGH);//mute else digitalWrite(beep,HIGH);//mute /*main loop*/ void loop() keysacn();//Press the button to start while(1) /**************************************************************************************...
  • Page 24 2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】--- selecting the port that the serial number displayed by the device manager just now, shown in the figure below.
  • Page 25 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 26 4. After the program is successfully uploaded, debug the BatCar as shown below. 5. Place the BatCar in an open space and place some cartons as obstacles. Turn on the BatCar's power switch, press the start button K1, and after hearing the short whistle,...
  • Page 27: Tracking

    After the program is uploaded, debug BatCar's potentiometers SW1 and SW2 according to the debugging method at the end of the article, open the BatCar's power switch, press the start button K1, and after hearing the short whistle, BatCar starts to follow the previous object.
  • Page 28 pinMode(SensorLeft, INPUT); // Set left Line Walking Infrared sensor as input pinMode(SensorRight_2, INPUT); //Set Right Infrared sensor as input pinMode(SensorLeft_2, INPUT); //Set left Infrared sensor as input digitalWrite(key,HIGH);//Initialize button digitalWrite(beep,HIGH);// set buzzer mute //=======================Motor======================== void run() digitalWrite(Right_motor_go,HIGH);// right motor go ahead digitalWrite(Right_motor_back,LOW);...
  • Page 29 delay(time * 100); void right() //turn right digitalWrite(Right_motor_go,LOW); // right motor stop digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,100); analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control speed void spin_right(int time) //Right rotation digitalWrite(Right_motor_go,LOW); // right motor back off digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0);...
  • Page 30 delay(10); //delay 10ms val=digitalRead(key);// Reads the button ,the level value assigns to val if(val==LOW) //Double check the button is pressed digitalWrite(beep,LOW);//The buzzer sounds delay(50);//delay 50ms while(!digitalRead(key)) //Determine if the button is released or not digitalWrite(beep,HIGH);//mute else digitalWrite(beep,HIGH);//mute /*main loop*/ void loop() keysacn();...
  • Page 31 2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】--- selecting the port that the serial number displayed by the device manager just now, shown in the figure below.
  • Page 32 4. After the program download is successful, debug potentiometers SW1 and SW2 as shown below. 5.Turn on the BatCar's power switch, press the start button K1, and after hearing the whistle, BatCar starts to follow the previous object.
  • Page 33: Ultrasonic Distance Measurement

    The purpose of the experiment: When the program upload is complete, open the serial monitor and you can see the distance measured by the ultrasonic sensor. List of components required for the experiment: BatCar*1 USB data cable*1 Ultrasonic sensor*1 Experimental code analysis: int Echo = A1;...
  • Page 34 Fdistance= Fdistance/58; // Distance(m) =(time(s) * 344(m/s)) / 2 /****** The speed of sound is 344m/s.*******/ μs) * 0.0344(cm/μs) // ==> 2*Distance(cm) = time( μs) * 0.0172 = time(μs) / 58 // ==> Distance(cm) = time( Serial.print("Distance:"); //Output Distance(cm) Serial.println(Fdistance); //display distance Distance = Fdistance;...
  • Page 35 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 36 4. After the program is successfully uploaded, do not unplug the USB cable. Click on the serial port monitor as shown below. 5. The serial port baud rate is 9600 as shown below.
  • Page 37 6. It can be seen that the data measured by the ultrasonic sensor is refreshed every 200 milliseconds in the serial monitor, and the data is slowly moved forward by the hand in front of the ultrasonic sensor, and the data measured by the ultrasonic is gradually reduced.
  • Page 38: Ultrasonic Check Obstacle And Avoid

    7- Ultrasonic check obstacle and avoid The purpose of the experiment: After the program is uploaded, place the BatCar in an open place and place some cartons as obstacles. Turn on the BatCar's power switch, press the start button K1, and after hearing the whistle, BatCar starts using the ultrasonic sensor to detect obstacles in front and avoid obstacles.
  • Page 39 int key=4; /*Set BUZZER port*/ int beep=3; void setup() //Initialize motor drive for output mode pinMode(Left_motor_go,OUTPUT); pinMode(Left_motor_back,OUTPUT); pinMode(Right_motor_go,OUTPUT); pinMode(Right_motor_back,OUTPUT); pinMode(key,INPUT);// Set button as input pinMode(beep,OUTPUT);// Set buzzer as output pinMode(Echo, INPUT); // Set Ultrasonic echo port as input pinMode(Trig, OUTPUT); // Set Ultrasonic trig port as input digitalWrite(key,HIGH);//Initialize button digitalWrite(beep,HIGH);// set buzzer mute //=======================Motor=========================...
  • Page 40 digitalWrite(Right_motor_go,HIGH); // right motor go ahead digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,100); // PWM--Pulse Width Modulation(0~255) control speed analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,LOW); // left motor back off digitalWrite(Left_motor_back,HIGH); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,100); // PWM--Pulse Width Modulation(0~255) control speed delay(time * 100); void right() //turn right digitalWrite(Right_motor_go,LOW); // right motor stop digitalWrite(Right_motor_back,LOW);...
  • Page 41 //========================================================== void keysacn() int val; val=digitalRead(key);// Reads the button ,the level value assigns to val while(digitalRead(key))// When the button is not pressed val=digitalRead(key); while(!digitalRead(key))// When the button is pressed delay(10); //delay 10ms val=digitalRead(key);// Reads the button ,the level value assigns to val if(val==LOW) //Double check the button is pressed digitalWrite(beep,LOW);//The buzzer sounds delay(50);//delay 50ms...
  • Page 42 delay(10); Distance_test();//Measuring front distance while(Distance<25)//Determine whether there is an obstruction again.If there is obstacle , turn the direction and determine again. spin_right(3);//Right rotation for 300ms brake();//stop delay(100); Distance_test();//Measuring front distance else run();//There is nothing obstacled. Go ahead. Experimental steps: 1. We need to open the code of this experiment:Ultrasonic_check_obstacle_and_avoid.ino, click“√”...
  • Page 43 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 44 4.After the program is uploaded, place the BatCar in an open place and place some cartons as obstacles. Turn on the BatCar's power switch, press the start button K1, and after hearing the whistle, BatCar starts using the ultrasonic sensor to detect obstacles in front and avoid obstacles.
  • Page 45: Line Walking And Check Obstacle

    After the program Tracking ultrasonic is successfully uploaded, unplug the USB cable and place the BatCar on the patrol tarck. After turning on the power switch and pressing the start button K1, BatCar starts to patrol the black line. If the ultrasonic sensor detects an obstacle within 20 cm of its front side, BatCar stops, otherwise it continues to patrol the black line.
  • Page 46 int Trig =A0; // Set Trig port int Distance = 0; void setup() //Initialize motor drive for output mode pinMode(Left_motor_go,OUTPUT); pinMode(Left_motor_back,OUTPUT); pinMode(Right_motor_go,OUTPUT); pinMode(Right_motor_back,OUTPUT); pinMode(key,INPUT);// Set button as input pinMode(beep,OUTPUT);// Set buzzer as output pinMode(SensorRight, INPUT); // Set Right Line Walking Infrared sensor as input pinMode(SensorLeft, INPUT);...
  • Page 47 void spin_left(int time) //Left rotation digitalWrite(Right_motor_go,HIGH); // right motor go ahead digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,100); // PWM--Pulse Width Modulation(0~255) control speed analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,LOW); // left motor back off digitalWrite(Left_motor_back,HIGH); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,100); // PWM--Pulse Width Modulation(0~255) control speed delay(time * 100); void right() //turn right digitalWrite(Right_motor_go,LOW);...
  • Page 48 delay(time * 100); //========================================================== void keysacn() int val; val=digitalRead(key);// Reads the button ,the level value assigns to val while(digitalRead(key))// When the button is not pressed val=digitalRead(key); while(!digitalRead(key))// When the button is pressed delay(10); //delay 10ms val=digitalRead(key);// Reads the button ,the level value assigns to val if(val==LOW) //Double check the button is pressed digitalWrite(beep,LOW);//The buzzer sounds delay(50);//delay 50ms...
  • Page 49 SR = digitalRead(SensorRight);//Right Line Walking Infrared sensor against white undersurface,then LED[L2] light illuminates and while against black undersurface,LED[L2] goes off SL = digitalRead(SensorLeft);//Left Line Walking Infrared sensor against white undersurface,then LED[L3] light illuminates and while against black undersurface,LED[L3] goes off Distance_test();// Measuring front distance if((Distance <...
  • Page 50 3. After the selection is completed, you need to click “→”under the menu bar to upload the code to the Arduino UNO board. When the word “Done uploading” appears in the lower left corner, the code has been successfully uploaded to the Arduino UNO board, as shown in the figure below.
  • Page 51 BatCar on the tour linepatrol tarck after debugging according to the following figure. 5.After turning on the power switch and pressing the start button K1, BatCar starts to patrol the black line. If the ultrasonic sensor detects an obstacle within 20 cm of its front...
  • Page 52: Remote Control

    Align the infrared emitter of the infrared remote control with the infrared receiver at the rear of the BatCar, then press the numeric keypad of the infrared remote control to control the BatCar to complete the corresponding action.
  • Page 53 int Right_motor_go = 6; int Right_motor_back = 10; int Right_motor_en = 8; int Left_motor_en = 7; void setup() //Initialize motor drive for output mode pinMode(Left_motor_go,OUTPUT); pinMode(Left_motor_back,OUTPUT); pinMode(Right_motor_go,OUTPUT); pinMode(Right_motor_back,OUTPUT); pinMode(13, OUTPUT);//Show Infrared Remote Control Received Signal irrecv.enableIRIn(); // Start the receiver void run() digitalWrite(Right_motor_go,HIGH);// right motor go ahead digitalWrite(Right_motor_back,LOW);...
  • Page 54 digitalWrite(Right_motor_back,HIGH); digitalWrite(Left_motor_go,HIGH);// left motor go ahead digitalWrite(Left_motor_back,LOW); void back() //back off digitalWrite(Right_motor_go,LOW); //right motor back off digitalWrite(Right_motor_back,HIGH); digitalWrite(Left_motor_go,LOW); //left motor back off digitalWrite(Left_motor_back,HIGH); //========================================================== void dump(decode_results *results)//Decode Infrared Remote Control Received Signal int count = results->rawlen; if (results->decode_type == UNKNOWN) brake();...
  • Page 55 2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】--- selecting the port that the serial number displayed by the device manager just now, shown in the figure below.
  • Page 56 Arduino UNO board, as shown in the figure below. 4. After the program of Remote contorl is uploaded, put the BatCar indoors and pull the curtains to block the outdoor lights. Align the infrared remote control with the...
  • Page 57 BatCar. Press the digital button on the infrared remote control to control the BatCar to complete the corresponding action. The following is the user code value corresponding to the infrared remote control. Remote...

Table of Contents