0% found this document useful (0 votes)
103 views8 pages

Código Arduino para Carro

This Arduino code controls an obstacle avoiding car. It uses ultrasonic sensors to measure distance and detect obstacles. When an obstacle is detected within 25 cm, the car backs up, pauses, then turns right or left depending on which direction has more clearance. If no obstacle is detected, the car moves forward. The code defines functions for motor control, reading sensor distance, and turning behaviors.

Uploaded by

Roberto Morales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views8 pages

Código Arduino para Carro

This Arduino code controls an obstacle avoiding car. It uses ultrasonic sensors to measure distance and detect obstacles. When an obstacle is detected within 25 cm, the car backs up, pauses, then turns right or left depending on which direction has more clearance. If no obstacle is detected, the car moves forward. The code defines functions for motor control, reading sensor distance, and turning behaviors.

Uploaded by

Roberto Morales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Código Arduino para carro

1. //ARDUINO OBSTACLE AVOIDING CAR Video - https://youtu.be/h-B42_HXL00//


2. // Before uploading the code you have to install the necessary library//
3. //AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
4. //NewPing Library https://github.com/eliteio/Arduino_New_Ping.git //
5. //Servo Library https://github.com/arduino-libraries/Servo.git //
6.
7. #include <AFMotor.h>
8. #include <NewPing.h>
9. #include <Servo.h>
10.
11. #define TRIG_PIN A0
12. #define ECHO_PIN A1
13. #define MAX_DISTANCE 100
14. #define MAX_SPEED 150 // sets speed of DC motors
15. #define MAX_SPEED_OFFSET 20
16.
17.
18. NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
19.
20. AF_DCMotor motor1(1, MOTOR12_64KHZ);
21. AF_DCMotor motor2(2, MOTOR12_64KHZ);
22. AF_DCMotor motor3(3, MOTOR34_64KHZ);
23. AF_DCMotor motor4(4, MOTOR34_64KHZ);
24. Servo myservo;
25.
26. boolean goesForward=false;
27. int distance = 100;
28. int speedSet = 0;
29.
30. void setup() {
31.
32. myservo.attach(10);
33. myservo.write(115);
34. delay(2000);
35. distance = readPing();
36. delay(100);
37. distance = readPing();
38. delay(100);
39. distance = readPing();
40. delay(100);
41. distance = readPing();
42. delay(100);
43. }
44.
45. void loop() {
46. int distanceR = 0;
47. int distanceL = 0;
48. delay(40);
49.
50. if(distance<=25)
51. {
52. moveStop();
53. delay(100);
54. moveBackward();
55. delay(200);
56. moveStop();
57. delay(200);
58. distanceR = lookRight();
59. delay(200);
60. distanceL = lookLeft();
61. delay(200);
62.
63. if(distanceR>=distanceL)
64. {
65. turnRight();
66. moveStop();
67. }
68.
69. else
70.
71. {
72. turnLeft();
73. moveStop();
74. }
75. }
76.
77. else
78. {
79. moveForward();
80. }
81. distance = readPing();
82. }
83.
84. int lookRight()
85. {
86. myservo.write(50);
87. delay(500);
88. int distance = readPing();
89. delay(100);
90. myservo.write(115);
91. return distance;
92. }
93.
94. int lookLeft()
95. {
96. myservo.write(170);
97. delay(500);
98. int distance = readPing();
99. delay(100);
100. myservo.write(115);
101. return distance;
102. delay(100);
103. }
104.
105. int readPing() {
106. delay(100);
107. int cm = sonar.ping_cm();
108. if(cm==0)
109. {
110. cm = 250;
111. }
112. return cm;
113. }
114.
115. void moveStop() {
116. motor1.run(RELEASE);
117. motor2.run(RELEASE);
118. motor3.run(RELEASE);
119. motor4.run(RELEASE);
120. }
121.
122. void moveForward() {
123.
124. if(!goesForward)
125. {
126. goesForward=true;
127. motor1.run(FORWARD);
128. motor2.run(FORWARD);
129. motor3.run(FORWARD);
130. motor4.run(FORWARD);
131. for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the
speed up to avoid loading down the batteries too quickly
132. {
133. motor1.setSpeed(speedSet);
134. motor2.setSpeed(speedSet);
135. motor3.setSpeed(speedSet);
136. motor4.setSpeed(speedSet);
137. delay(5);
138. }
139. }
140. }
141.
142. void moveBackward() {
143. goesForward=false;
144. motor1.run(BACKWARD);
145. motor2.run(BACKWARD);
146. motor3.run(BACKWARD);
147. motor4.run(BACKWARD);
148. for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed
up to avoid loading down the batteries too quickly
149. {
150. motor1.setSpeed(speedSet);
151. motor2.setSpeed(speedSet);
152. motor3.setSpeed(speedSet);
153. motor4.setSpeed(speedSet);
154. delay(5);
155. }
156. }
157.
158. void turnRight() {
159. motor1.run(FORWARD);
160. motor2.run(FORWARD);
161. motor3.run(BACKWARD);
162. motor4.run(BACKWARD);
163. delay(500);
164. motor1.run(FORWARD);
165. motor2.run(FORWARD);
166. motor3.run(FORWARD);
167. motor4.run(FORWARD);
168. }
169.
170. void turnLeft() {
171. motor1.run(BACKWARD);
172. motor2.run(BACKWARD);
173. motor3.run(FORWARD);
174. motor4.run(FORWARD);
175. delay(500);
176. motor1.run(FORWARD);
177. motor2.run(FORWARD);
178. motor3.run(FORWARD);
179. motor4.run(FORWARD);
RAW Paste Data

//ARDUINO OBSTACLE AVOIDING CAR Video - https://youtu.be/h-B42_HXL00//


// Before uploading the code you have to install the necessary library//
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
//NewPing Library https://github.com/eliteio/Arduino_New_Ping.git //
//Servo Library https://github.com/arduino-libraries/Servo.git //

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>

#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 100
#define MAX_SPEED 150 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_64KHZ);


AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR34_64KHZ);
AF_DCMotor motor4(4, MOTOR34_64KHZ);
Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);

if(distance<=25)
{
moveStop();
delay(100);
moveBackward();
delay(200);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);

if(distanceR>=distanceL)
{
turnRight();
moveStop();
}

else

{
turnLeft();
moveStop();
}
}

else
{
moveForward();
}
distance = readPing();
}

int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}

int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}

int readPing() {
delay(100);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}

void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}

void moveForward() {

if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid
loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}

void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid
loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}

void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

You might also like