-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smart_car_using_Ir_sensor.ino
95 lines (85 loc) · 2.65 KB
/
Smart_car_using_Ir_sensor.ino
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define ENA 9 // ENA speed pin for left motor
#define IN1 8 // IN1 on Motor controller board
#define IN2 10 // IN2
#define ENB 11 // ENB speed pin for right motor
#define IN3 12 // IN3 on Motor controller board
#define IN4 13 // IN4
int LS=2; // left sensor
int MS=4; // middle sensor
int RS=6; // right sensor
int rightspeed = 180;
int leftspeed = 180;
void setup()
{
pinMode(ENA, OUTPUT); //set up left motor
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT); //sets up right motor
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(LS, INPUT);
pinMode(MS, INPUT);
pinMode(RS, INPUT);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(7,OUTPUT);
analogWrite(ENA, rightspeed); //set speed to mid
analogWrite(ENB, leftspeed); //set speed to mid
}
void loop()
{
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
{
if(digitalRead(LS)==LOW && digitalRead(MS)==LOW && digitalRead(RS)==LOW) // Move Forward
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if(digitalRead(LS)==HIGH && digitalRead(MS)==LOW && digitalRead(RS)==LOW) // Turn right
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if(digitalRead(LS)==LOW && digitalRead(MS)==LOW && digitalRead(RS)==HIGH) // turn left
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if(digitalRead(LS)==LOW && digitalRead(MS)==HIGH && digitalRead(RS)==LOW) // Move back
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
else if(digitalRead(LS)==HIGH && digitalRead(MS)==HIGH && digitalRead(RS)==LOW) // Turn right
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if(digitalRead(LS)==LOW && digitalRead(MS)==HIGH && digitalRead(RS)==HIGH) // turn left
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if(digitalRead(LS)==HIGH && digitalRead(MS)==HIGH && digitalRead(RS)==HIGH) // Move back
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
}
}