-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrect_car.ino
201 lines (199 loc) · 6.19 KB
/
correct_car.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const int IN1 = 2;
const int IN2 = 3;
const int IN3 = 4;
const int IN4 = 5;
const int LEDPin[] = {6, 7, 8, 9};
const int ENA = 10;
const int ENB = 11;
const int SW = 12;
const int joystickAnalog[] = {A0, A1};
const int IRPin[] = {A2, A3, A4, A5};
int joystickAnalogValue[] = {0, 0};
int i;
int IR_SensorValue[] = {0,0,0,0};
unsigned long previousMillis = 0;
const long interval = 300;
const int F = 255, H = 125, Z = 0;
int ledState = HIGH;
int forward = 1, backward = 1;
void setup() {
// put your setup code here, to run once:
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
for(i=0; i<4; i++)
{
pinMode(LEDPin[i], OUTPUT);
digitalWrite(LEDPin[i], LOW);
}
pinMode(SW, INPUT);
digitalWrite(SW, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
while(digitalRead(SW) == 1)
Serial.println("First");
while(digitalRead(SW) == 0);
while(digitalRead(SW) == 1)
{
for(i=0;i<4;i++)
IR_SensorValue[i] = analogRead(IRPin[i]);
if(IR_SensorValue[0] < 100 || IR_SensorValue[1] <100)
forward = 0;
else
forward = 1;
if(IR_SensorValue[2] < 100 || IR_SensorValue[3] <100)
backward = 0;
else
backward = 1;
joystickAnalogValue[0] = analogRead(joystickAnalog[0]);
joystickAnalogValue[1] = analogRead(joystickAnalog[1]);
Serial.println(joystickAnalogValue[0]);
Serial.println(joystickAnalogValue[1]);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == HIGH)
ledState = LOW;
else
ledState = HIGH;
}
for(i=0; i<2; i++)
{
if(joystickAnalogValue[i] >= 0 && joystickAnalogValue[i] <= 255)
joystickAnalogValue[i] = 0;
else if(joystickAnalogValue[i] >= 256 && joystickAnalogValue[i] <= 767)
joystickAnalogValue[i] = 1;
else
joystickAnalogValue[i] = 2;
}
Serial.println(joystickAnalogValue[0]);
Serial.println(joystickAnalogValue[1]);
if(joystickAnalogValue[0] == 2)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
else if(joystickAnalogValue[0] == 1 && joystickAnalogValue[1] == 1)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
//NORTH
if(joystickAnalogValue[0] == 0 && joystickAnalogValue[1] == 1 && forward == 1)
{
analogWrite(ENA, F);
analogWrite(ENB, F);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], LOW);
Serial.println("north");
}
//NORTH EAST
else if(joystickAnalogValue[0] == 0 && joystickAnalogValue[1] == 0 && forward == 1)
{
analogWrite(ENA, F);
analogWrite(ENB, H);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], ledState);
digitalWrite(LEDPin[3], LOW);
Serial.println("north east");
}
//EAST
else if(joystickAnalogValue[0] == 1 && joystickAnalogValue[1] == 0 && IR_SensorValue[0] < 100 && IR_SensorValue[3] < 100)
{
analogWrite(ENA, F);
analogWrite(ENB, Z);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], ledState);
digitalWrite(LEDPin[3], LOW);
Serial.println("east");
}
//SOUTH EAST
else if(joystickAnalogValue[0] == 2 && joystickAnalogValue[1] == 0 && backward == 1)
{
analogWrite(ENA, F);
analogWrite(ENB, H);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], ledState);
digitalWrite(LEDPin[3], HIGH);
Serial.println("south east");
}
//SOUTH
else if(joystickAnalogValue[0] == 2 && joystickAnalogValue[1] == 1 && backward == 1)
{
analogWrite(ENA, F);
analogWrite(ENB, F);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], HIGH);
Serial.println("south");
}
//SOUTH WEST
else if(joystickAnalogValue[0] == 2 && joystickAnalogValue[1] == 2 && backward == 1)
{
analogWrite(ENA, H);
analogWrite(ENB, F);
digitalWrite(LEDPin[0], ledState);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], HIGH);
Serial.println("south west");
}
//WEST
else if(joystickAnalogValue[0] == 1 && joystickAnalogValue[1] == 2 IR_SensorValue[1] < 100 && IR_SensorValue[2] < 100)
{
analogWrite(ENA, Z);
analogWrite(ENB, F);
digitalWrite(LEDPin[0], ledState);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], LOW);
Serial.println("west");
}
//NORTH WEST
else if(joystickAnalogValue[0] == 0 && joystickAnalogValue[1] == 2 && forward == 1)
{
analogWrite(ENA, H);
analogWrite(ENB, F);
digitalWrite(LEDPin[0], ledState);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], LOW);
Serial.println("north west");
}
//CENTRE
else
{
analogWrite(ENA, Z);
analogWrite(ENB, Z);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], LOW);
digitalWrite(LEDPin[3], LOW);
Serial.println("centre");
}
}
while(digitalRead(SW) == 0);