Skip to content

Commit f63ce01

Browse files
committed
New motor-driving with IR, + some other smaller updates
1 parent 6635f6d commit f63ce01

File tree

3 files changed

+662
-0
lines changed

3 files changed

+662
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Yu Hin Hau
2+
// Robotic Car via H-Bridge (L298)
3+
// June 5, 2012
4+
// Modified for infrared by: Lars Age Kamfjord github.com/laaknor
5+
6+
#include <IRremote.h>
7+
8+
const int irPin = 3;
9+
10+
IRrecv irrecv (irPin);
11+
decode_results results;
12+
13+
//Define Pins
14+
int enableA = 2; // ENA
15+
int pinA1 = 1; // IN2
16+
int pinA2 = 0; // IN1
17+
18+
int enableB = 6; // ENB
19+
int pinB1 = 5; //IN4
20+
int pinB2 = 4; // IN3
21+
22+
void setup() {
23+
24+
pinMode(enableA, OUTPUT);
25+
pinMode(pinA1, OUTPUT);
26+
pinMode(pinA2, OUTPUT);
27+
28+
pinMode(enableB, OUTPUT);
29+
pinMode(pinB1, OUTPUT);
30+
pinMode(pinB2, OUTPUT);
31+
irrecv.enableIRIn();
32+
Serial.begin(9600);
33+
34+
} // End setup
35+
36+
37+
//command sequence
38+
void loop() {
39+
40+
if(irrecv.decode(&results)) {
41+
irrecv.resume();
42+
if(results.value == 0xFF9867) {
43+
Serial.println("Going forward!");
44+
enableMotors();
45+
forward(1000);
46+
disableMotors();
47+
} // End if results == FF9867 UP
48+
else if(results.value == 0xFF30CF) {
49+
Serial.println("Going LEFT");
50+
enableMotors();
51+
turnLeft(250);
52+
disableMotors();
53+
} // End if results == FF30CF LEFT
54+
else if(results.value == 0xFF18E7) {
55+
Serial.println("Going back!");
56+
enableMotors();
57+
backward(500);
58+
disableMotors();
59+
} // End if results == FF18E7 BACK
60+
else if(results.value == 0xFF7A85) {
61+
Serial.println("Going RIGHT!");
62+
enableMotors();
63+
turnRight(250);
64+
disableMotors();
65+
} // End if results == FF7A85 RIGHT
66+
67+
// else disableMotors();
68+
} // end irrecv.decode
69+
} // End loop
70+
71+
//Define Low Level H-Bridge Commands
72+
73+
//enable motors
74+
void motorAOn()
75+
{
76+
digitalWrite(enableA, HIGH);
77+
Serial.println("enableA=HIGH");
78+
}
79+
80+
void motorBOn()
81+
{
82+
digitalWrite(enableB, HIGH);
83+
Serial.println("enableB=HIGH");
84+
}
85+
86+
//disable motors
87+
void motorAOff()
88+
{
89+
digitalWrite(enableA, LOW);
90+
Serial.println("enableA=LOW");
91+
}
92+
93+
void motorBOff()
94+
{
95+
digitalWrite(enableB, LOW);
96+
Serial.println("enableB=LOW");
97+
}
98+
99+
//motor A controls
100+
void motorAForward()
101+
{
102+
digitalWrite(pinA1, HIGH);
103+
digitalWrite(pinA2, LOW);
104+
Serial.println("Motor A, HIGH-LOW");
105+
}
106+
107+
void motorABackward()
108+
{
109+
digitalWrite(pinA1, LOW);
110+
digitalWrite(pinA2, HIGH);
111+
Serial.println("Motor A, LOW-HIGH");
112+
}
113+
114+
//motor B contorls
115+
void motorBForward()
116+
{
117+
digitalWrite(pinB1, HIGH);
118+
digitalWrite(pinB2, LOW);
119+
Serial.println("Motor B, HIGH-LOW");
120+
}
121+
122+
void motorBBackward()
123+
{
124+
digitalWrite(pinB1, LOW);
125+
digitalWrite(pinB2, HIGH);
126+
Serial.println("Motor B, LOW-HIGH");
127+
}
128+
129+
//coasting and braking
130+
void motorACoast()
131+
{
132+
digitalWrite(pinA1, LOW);
133+
digitalWrite(pinA2, LOW);
134+
Serial.println("Motor A, both LOW");
135+
}
136+
137+
void motorABrake()
138+
{
139+
digitalWrite(pinA1, HIGH);
140+
digitalWrite(pinA2, HIGH);
141+
Serial.println("Motor A, both HIGH");
142+
}
143+
144+
void motorBCoast()
145+
{
146+
digitalWrite(pinB1, LOW);
147+
digitalWrite(pinB2, LOW);
148+
Serial.println("Motor B, both LOW");
149+
}
150+
151+
void motorBBrake()
152+
{
153+
digitalWrite(pinB1, HIGH);
154+
digitalWrite(pinB2, HIGH);
155+
Serial.println("Motor B, both HIGH");
156+
}
157+
158+
//Define High Level Commands
159+
160+
void enableMotors()
161+
{
162+
motorAOn();
163+
motorBOn();
164+
}
165+
166+
void disableMotors()
167+
{
168+
motorAOff();
169+
motorBOff();
170+
}
171+
172+
void forward(int time)
173+
{
174+
motorAForward();
175+
motorBForward();
176+
delay(time);
177+
}
178+
179+
void backward(int time)
180+
{
181+
motorABackward();
182+
motorBBackward();
183+
delay(time);
184+
}
185+
186+
void turnLeft(int time)
187+
{
188+
motorABackward();
189+
motorBForward();
190+
delay(time);
191+
}
192+
193+
void turnRight(int time)
194+
{
195+
motorAForward();
196+
motorBBackward();
197+
delay(time);
198+
}
199+
200+
void coast(int time)
201+
{
202+
motorACoast();
203+
motorBCoast();
204+
delay(time);
205+
}
206+
207+
void brake(int time)
208+
{
209+
motorABrake();
210+
motorBBrake();
211+
delay(time);
212+
}

0 commit comments

Comments
 (0)