-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar_Project.ino
193 lines (143 loc) · 3.72 KB
/
Car_Project.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
#include <IRremote.h>
#include <MotorDriver.h>
// IR Remote Setups
const int RECV_PIN = 10; // Define the pin where your IR receiver is connected
unsigned long lastValue = 0; // Variable to store the last valid received value
IRrecv irrecv(RECV_PIN);
decode_results results;
// Buttons
#define Button_0 0xFF6897
#define Button_1 0xFF30CF
#define Button_2 0xFF18E7
#define Button_3 0xFF7A85
#define Button_4 0xFF10EF
#define Button_5 0xFF38C7
#define Button_6 0xFF5AA5
#define Button_7 0xFF42BD
#define Button_8 0xFF4AB5
#define Button_9 0xFF52AD
// Motor Pins
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 8;
int motor2pin2 = 9;
// YFROBOT PM-R3 SETUP
#define MOTORTYPE YF_PMR3
MotorDriver motorDriver = MotorDriver(MOTORTYPE);
const int offseta = 1;
const int offsetb = 1;
uint8_t SerialDebug = 1;
// Functions For Moving
// Button 0
void stop()
{
motorDriver.setMotor(0, 0);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
// Button 1
void left_diagonal_up()
{
motorDriver.setMotor(0, 255);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
// Button 2
void forward() {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
motorDriver.setMotor(255, 255);
}
// Button 3
void right_diagonal_up()
{
motorDriver.setMotor(255, 0);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
// Button 4
void left()
{
motorDriver.setMotor(-255, 255);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
}
// Button 6
void right()
{
motorDriver.setMotor(255, -255);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
// Button 7
void left_diagonal_down()
{
motorDriver.setMotor(-255, 0);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
// Button 8
void backwards() {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
motorDriver.setMotor(-255, -255);
}
// Button 9
void right_diagonal_down()
{
motorDriver.setMotor(0, -255);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
}
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
// Motor Pin Modes
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
// PM-R3 Motor Setup
motorDriver.motorConfig(offseta, offsetb);
motorDriver.setMotor(0, 0);
}
void loop() {
if (irrecv.decode(&results)) { //if we have received an IR signal
if (results.value == 0xFFFFFFFF) {
results.value = lastValue;
}
switch (results.value) {
case Button_0: stop(); break;
case Button_1: left_diagonal_up(); break;
case Button_2: forward(); break;
case Button_3: right_diagonal_up(); break;
case Button_4: left(); break;
case Button_5: automatic(); break;
case Button_6: right(); break;
case Button_7: left_diagonal_down(); break;
case Button_8: backwards(); break;
case Button_9: right_diagonal_down(); break;
}
Serial.println(results.value, HEX); //display HEX results
irrecv.resume(); //next value
}
lastValue = results.value;
}