-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_code.ino
More file actions
77 lines (77 loc) · 1.75 KB
/
Arduino_code.ino
File metadata and controls
77 lines (77 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define trigPin 12 // OPTIONAL: Defining the trigger pin of the sensor to the 12th pin of UNO
#define echoPin 13 // OPTIONAL: Defining the echo pin of the sensor to the 13th pin of UNO
#include <Servo.h> // OPTIONAL: We added a servo motor for sweeping the obstacles infront of the robo :-P
Servo servoMain;
int value=0;
int input[]={2,3,4,5};
int output[]={8,9,10,11};
void setup()
{
servoMain.attach(7); // Attaching the servo to the 7th pin fo the UNO
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
int thispin=0;
for(thispin=0;thispin<4;thispin++)
{
pinMode (input[thispin],INPUT);
pinMode (output[thispin],OUTPUT);
}}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(distance<8)
{
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
servoMain.write(160); // Turn Servo Left to 160 degrees
delay(1500); // Wait 1 second
servoMain.write(10); // Turn Servo Left to 10 degrees
delay(1500);
}
else
{
value=digitalRead(2)+2*digitalRead(3)+4*digitalRead(4)+8*digitalRead(5);
switch(value)
{
case 13: // FORWARD
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
break;
case 11: //RIGHT
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
break;
case 9: //LEFT
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
break;
case 7: //REVERSE
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
break;
default: //STOP
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
break;
}}
delay(500);
}