1#include <IRremote.h>
2#define irPin 3
3
4#include "SD.h"
5#define SD_ChipSelectPin 4
6#include "TMRpcm.h"
7#include "SPI.h"
8TMRpcm tmrpcm;
9
10IRrecv irrecv(irPin);
11decode_results results;
12
13
14int leftMotorForward = A3;
15int rightMotorForward = A1;
16int leftMotorBackward = A4;
17int rightMotorBackward = A2;
18
19
20int rightMotorENB = A0;
21int leftMotorENB = A5;
22
23void setup()
24{
25 tmrpcm.speakerPin = 9;
26Serial.begin(9600);
27if (!SD.begin(SD_ChipSelectPin)) {
28Serial.println("SD fail");
29return;
30}
31
32tmrpcm.setVolume(5);
33
34
35
36 irrecv.enableIRIn();
37
38 pinMode(leftMotorForward, OUTPUT);
39 pinMode(rightMotorForward, OUTPUT);
40 pinMode(leftMotorBackward, OUTPUT);
41 pinMode(rightMotorBackward, OUTPUT);
42
43
44 pinMode(leftMotorENB, OUTPUT);
45 pinMode(rightMotorENB, OUTPUT);
46
47
48}
49
50void loop()
51{
52 if (irrecv.decode(&results))
53 {
54 Serial.print(results.value, HEX);
55 Serial.print(" - ");
56 Serial.println(results.value);
57 switch (results.value)
58 {
59 case 1077989423:
60 MotorForward();
61 break;
62 case 1077964943:
63 MotorBackward();
64 break;
65 case 1077938423:
66 TurnLeft();
67 break;
68 case 1077971063:
69 TurnRight();
70 break;
71 default:
72 MotorStop();
73 }
74
75 irrecv.resume();
76 }
77
78}
79
80
81void MotorForward(void)
82{
83 tmrpcm.play("forward.wav");
84 digitalWrite(leftMotorENB,HIGH);
85 digitalWrite(rightMotorENB,HIGH);
86 digitalWrite(leftMotorForward,HIGH);
87 digitalWrite(rightMotorForward,HIGH);
88 digitalWrite(leftMotorBackward,LOW);
89 digitalWrite(rightMotorBackward,LOW);
90}
91
92
93void MotorBackward(void)
94{
95tmrpcm.play("backward.wav");
96 digitalWrite(leftMotorENB,HIGH);
97 digitalWrite(rightMotorENB,HIGH);
98 digitalWrite(leftMotorBackward,HIGH);
99 digitalWrite(rightMotorBackward,HIGH);
100 digitalWrite(leftMotorForward,LOW);
101 digitalWrite(rightMotorForward,LOW);
102}
103
104
105void TurnLeft(void)
106{
107 tmrpcm.play("left.wav");
108 digitalWrite(leftMotorENB,HIGH);
109 digitalWrite(rightMotorENB,HIGH);
110 digitalWrite(leftMotorForward,LOW);
111 digitalWrite(rightMotorForward,HIGH);
112 digitalWrite(rightMotorBackward,LOW);
113 digitalWrite(leftMotorBackward,HIGH);
114}
115
116
117void TurnRight(void)
118{
119 tmrpcm.play("right.wav");
120 digitalWrite(leftMotorENB,HIGH);
121 digitalWrite(rightMotorENB,HIGH);
122 digitalWrite(leftMotorForward,HIGH);
123 digitalWrite(rightMotorForward,LOW);
124 digitalWrite(rightMotorBackward,HIGH);
125 digitalWrite(leftMotorBackward,LOW);
126}
127
128
129void MotorStop(void)
130{
131 digitalWrite(leftMotorENB,LOW);
132 digitalWrite(rightMotorENB,LOW);
133 digitalWrite(leftMotorForward,LOW);
134 digitalWrite(leftMotorBackward,LOW);
135 digitalWrite(rightMotorForward,LOW);
136 digitalWrite(rightMotorBackward,LOW);
137}