Components and supplies
3.7 V Battery (18650)
Rubber/Plastic wheel
9V battery (generic)
9V Battery Clip
DC Motor 6 V
Ultrasonic Sensor - HC-SR04 (Generic)
H Bridge L298N
Battery Holder, 18650 x 2
Jumper wires (generic)
Male/Female Jumper Wires
Arduino Mega 2560
Apps and platforms
Arduino IDE
Project description
Code
Code
c_cpp
This is the code that needs to be loaded on the Arduino
1// NewPing library is included 2#include <NewPing.h> 3// Here I declare what is connected to each pin on the Arduino 4int ENA_LEFT = 22; 5int IN1_LEFT = 24; 6int IN2_LEFT = 26; 7int IN3_LEFT = 28; 8int IN4_LEFT = 30; 9int ENB_LEFT = 32; 10int ENA_RIGHT = 40; 11int IN1_RIGHT = 42; 12int IN2_RIGHT = 44; 13int IN3_RIGHT = 46; 14int IN4_RIGHT = 48; 15int ENB_RIGHT = 50; 16int Distance; 17 18int TRIGGER_PIN = 51; 19int ECHO_PIN = 53; 20int MAX_DISTANCE = 500; 21NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 22 23// All pins are declared as outputs 24// pins of the sensor (TRIGGER AND ECHO) doesn't have to be declared here 25void setup() { 26 pinMode(ENA_LEFT, OUTPUT); 27 pinMode(IN1_LEFT, OUTPUT); 28 pinMode(IN2_LEFT, OUTPUT); 29 pinMode(IN3_LEFT, OUTPUT); 30 pinMode(IN4_LEFT, OUTPUT); 31 pinMode(ENB_LEFT, OUTPUT); 32 pinMode(ENA_RIGHT, OUTPUT); 33 pinMode(IN1_RIGHT, OUTPUT); 34 pinMode(IN2_RIGHT, OUTPUT); 35 pinMode(IN3_RIGHT, OUTPUT); 36 pinMode(IN4_RIGHT, OUTPUT); 37 pinMode(ENB_RIGHT, OUTPUT); 38 39} 40 41void loop() { 42 // The sensor measures de distance and stores in a variable called 43 // "Distance" 44 unsigned int uS = sonar.ping(); 45 delay(32); 46 Distance = sonar.convert_cm(uS); 47 48 // If the distance is 57 or more the robot starts rotating in its own axis 49 // 57 is the distance in cm between a soda can and the sensor 50 // you can adjust this value if needed 51 if ((Distance > 57) or (Distance = 0)){ 52 turn(); 53 } 54 55 // If the distance is less than 57 cm it moves foward and then backwards 56 // 900 is the time in miliseconds the motors stay turned on 57 // while moving foward or backwards you can adjust this value if needed 58 else{ 59 turn(); 60 delay(50); 61 stop_(); 62 foward(); 63 delay(900); 64 stop_(); 65 delay(400); 66 back(); 67 delay(900); 68 stop_(); 69 70 } 71 72} 73 74// functions that are called on voidloop() 75void foward(){ 76 digitalWrite(ENA_LEFT, HIGH); 77 digitalWrite(ENB_LEFT, HIGH); 78 digitalWrite(IN1_LEFT, HIGH); 79 digitalWrite(IN2_LEFT, LOW); 80 digitalWrite(IN3_LEFT, HIGH); 81 digitalWrite(IN4_LEFT, LOW); 82 83 digitalWrite(ENA_RIGHT, HIGH); 84 digitalWrite(ENB_RIGHT, HIGH); 85 digitalWrite(IN1_RIGHT, HIGH); 86 digitalWrite(IN2_RIGHT, LOW); 87 digitalWrite(IN3_RIGHT, HIGH); 88 digitalWrite(IN4_RIGHT, LOW); 89} 90 91void stop_(){ 92 digitalWrite(ENA_LEFT, LOW); 93 digitalWrite(ENB_LEFT, LOW); 94 digitalWrite(ENA_RIGHT, LOW); 95 digitalWrite(ENB_RIGHT, LOW); 96} 97 98void back(){ 99 digitalWrite(ENA_LEFT, HIGH); 100 digitalWrite(ENB_LEFT, HIGH); 101 digitalWrite(IN1_LEFT, LOW); 102 digitalWrite(IN2_LEFT, HIGH); 103 digitalWrite(IN3_LEFT, LOW); 104 digitalWrite(IN4_LEFT, HIGH); 105 106 digitalWrite(ENA_RIGHT, HIGH); 107 digitalWrite(ENB_RIGHT, HIGH); 108 digitalWrite(IN1_RIGHT, LOW); 109 digitalWrite(IN2_RIGHT, HIGH); 110 digitalWrite(IN3_RIGHT, LOW); 111 digitalWrite(IN4_RIGHT, HIGH); 112} 113 114void turn(){ 115 digitalWrite(ENB_RIGHT, HIGH); 116 digitalWrite(ENA_RIGHT, HIGH); 117 digitalWrite(IN3_RIGHT, HIGH); 118 digitalWrite(IN4_RIGHT, LOW); 119 digitalWrite(IN1_RIGHT, HIGH); 120 digitalWrite(IN2_RIGHT, LOW); 121} 122
Code
c_cpp
This is the code that needs to be loaded on the Arduino
1// NewPing library is included 2#include <NewPing.h> 3// Here I declare what is connected to each pin on the Arduino 4int ENA_LEFT = 22; 5int IN1_LEFT = 24; 6int IN2_LEFT = 26; 7int IN3_LEFT = 28; 8int IN4_LEFT = 30; 9int ENB_LEFT = 32; 10int ENA_RIGHT = 40; 11int IN1_RIGHT = 42; 12int IN2_RIGHT = 44; 13int IN3_RIGHT = 46; 14int IN4_RIGHT = 48; 15int ENB_RIGHT = 50; 16int Distance; 17 18int TRIGGER_PIN = 51; 19int ECHO_PIN = 53; 20int MAX_DISTANCE = 500; 21NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 22 23// All pins are declared as outputs 24// pins of the sensor (TRIGGER AND ECHO) doesn't have to be declared here 25void setup() { 26 pinMode(ENA_LEFT, OUTPUT); 27 pinMode(IN1_LEFT, OUTPUT); 28 pinMode(IN2_LEFT, OUTPUT); 29 pinMode(IN3_LEFT, OUTPUT); 30 pinMode(IN4_LEFT, OUTPUT); 31 pinMode(ENB_LEFT, OUTPUT); 32 pinMode(ENA_RIGHT, OUTPUT); 33 pinMode(IN1_RIGHT, OUTPUT); 34 pinMode(IN2_RIGHT, OUTPUT); 35 pinMode(IN3_RIGHT, OUTPUT); 36 pinMode(IN4_RIGHT, OUTPUT); 37 pinMode(ENB_RIGHT, OUTPUT); 38 39} 40 41void loop() { 42 // The sensor measures de distance and stores in a variable called 43 // "Distance" 44 unsigned int uS = sonar.ping(); 45 delay(32); 46 Distance = sonar.convert_cm(uS); 47 48 // If the distance is 57 or more the robot starts rotating in its own axis 49 // 57 is the distance in cm between a soda can and the sensor 50 // you can adjust this value if needed 51 if ((Distance > 57) or (Distance = 0)){ 52 turn(); 53 } 54 55 // If the distance is less than 57 cm it moves foward and then backwards 56 // 900 is the time in miliseconds the motors stay turned on 57 // while moving foward or backwards you can adjust this value if needed 58 else{ 59 turn(); 60 delay(50); 61 stop_(); 62 foward(); 63 delay(900); 64 stop_(); 65 delay(400); 66 back(); 67 delay(900); 68 stop_(); 69 70 } 71 72} 73 74// functions that are called on voidloop() 75void foward(){ 76 digitalWrite(ENA_LEFT, HIGH); 77 digitalWrite(ENB_LEFT, HIGH); 78 digitalWrite(IN1_LEFT, HIGH); 79 digitalWrite(IN2_LEFT, LOW); 80 digitalWrite(IN3_LEFT, HIGH); 81 digitalWrite(IN4_LEFT, LOW); 82 83 digitalWrite(ENA_RIGHT, HIGH); 84 digitalWrite(ENB_RIGHT, HIGH); 85 digitalWrite(IN1_RIGHT, HIGH); 86 digitalWrite(IN2_RIGHT, LOW); 87 digitalWrite(IN3_RIGHT, HIGH); 88 digitalWrite(IN4_RIGHT, LOW); 89} 90 91void stop_(){ 92 digitalWrite(ENA_LEFT, LOW); 93 digitalWrite(ENB_LEFT, LOW); 94 digitalWrite(ENA_RIGHT, LOW); 95 digitalWrite(ENB_RIGHT, LOW); 96} 97 98void back(){ 99 digitalWrite(ENA_LEFT, HIGH); 100 digitalWrite(ENB_LEFT, HIGH); 101 digitalWrite(IN1_LEFT, LOW); 102 digitalWrite(IN2_LEFT, HIGH); 103 digitalWrite(IN3_LEFT, LOW); 104 digitalWrite(IN4_LEFT, HIGH); 105 106 digitalWrite(ENA_RIGHT, HIGH); 107 digitalWrite(ENB_RIGHT, HIGH); 108 digitalWrite(IN1_RIGHT, LOW); 109 digitalWrite(IN2_RIGHT, HIGH); 110 digitalWrite(IN3_RIGHT, LOW); 111 digitalWrite(IN4_RIGHT, HIGH); 112} 113 114void turn(){ 115 digitalWrite(ENB_RIGHT, HIGH); 116 digitalWrite(ENA_RIGHT, HIGH); 117 digitalWrite(IN3_RIGHT, HIGH); 118 digitalWrite(IN4_RIGHT, LOW); 119 digitalWrite(IN1_RIGHT, HIGH); 120 digitalWrite(IN2_RIGHT, LOW); 121} 122
Downloadable files
Diagram
This diagram shows the connections of each component
Diagram
Comments
Only logged in users can leave comments
virtualblack
0 Followers
•0 Projects
Table of contents
Intro
0
0