-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniQ2WD_IrRemoteControl.ino
147 lines (125 loc) · 3.71 KB
/
MiniQ2WD_IrRemoteControl.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
/*
* Copyright (C) 2020 Luis Alejandro Bernal Romero (Aztlek)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* This is a program for the MiniQ 2WD robot, it is used to control the robot
* with the infrared remote control that comes in the kit. But the control that
* is achieved with this program is much more complete than the example robot
* program. Some of the additional features are:
*
* - The turns of the robot are more controllable.
* - Turns can also be done when the robot is moving forward, backward, or stopped.
* - The speed of the robot can be increased and decreased.
*
* Miniq is an Arduino
* -------------------
*
* The MiniQ 2WD robot is a Leonardo Arduino. Therefore you have to configure it as
* such in the Arduino IDE. To do this select Tools -> Board -> Arduino Leonardo.
*/
#include <IRremote.h>
// Constants for velocity
#define DELTA_V 10
#define MAX_VELOCITY 255
// Ir remote control buttons
#define BACKWARD_BUTTON 0x00fd20df
#define FORDWARD_BUTTON 0x00fd609f
#define POWEOFF_BUTTON 0x00fd00ff
#define VOL_PLUS_BUTTON 0x00fd807f
#define VOL_MINUS_BUTTON 0x00fd906f
#define PLAY_STOP_BUTTOM 0XfdA05f
// Motor constants
#define RIGHT_MOTOR_PIN 6
#define RIGHT_MOTOR_DIRECTION_PIN 7
#define LEFT_MOTOR_PIN 5
#define LEFT_MOTOR_DIRECTION_PIN 12
#define FORW 0//go forward
#define BACK 1//go back
// IR control constants
#define IR_IN 17//IR receiver pin
IRrecv irrecv(IR_IN);
decode_results results;
int velocity = 0;
void motor_control(int right_motor_dir, int speed_right_motor, int left_motor_dir, int speed_left_motor)
{
// Right motor
digitalWrite(RIGHT_MOTOR_DIRECTION_PIN, right_motor_dir);
analogWrite(RIGHT_MOTOR_PIN, speed_right_motor);
// Left motor
digitalWrite(LEFT_MOTOR_DIRECTION_PIN, left_motor_dir);
analogWrite(LEFT_MOTOR_PIN, speed_left_motor);
}
void dump(decode_results *results)
{
switch(results->value) {
case PLAY_STOP_BUTTOM:
// Stop
velocity = 0;
break;
case VOL_PLUS_BUTTON:
// Increase speed
velocity += DELTA_V;
break;
case VOL_MINUS_BUTTON:
// Decrease speed
velocity -= DELTA_V;
break;
case BACKWARD_BUTTON:
// Turn Left
motor_control(FORW,100,BACK,100);
delay(50);
break;
case FORDWARD_BUTTON:
// Turn Right
motor_control(BACK,100,FORW,100);
delay(50);
break;
}
velocity = constrain(velocity, -MAX_VELOCITY, MAX_VELOCITY);
Serial.print("velocity = ");
Serial.println(velocity);
if(velocity >= 0) {
// forward or stop
motor_control(FORW, velocity, FORW, velocity);
}
else if(velocity < 0) {
// backward
motor_control(BACK, -velocity, BACK, -velocity);
}
}
void setup()
{
// init the motor driver pins
pinMode(RIGHT_MOTOR_PIN, OUTPUT);
pinMode(RIGHT_MOTOR_DIRECTION_PIN, OUTPUT);
pinMode(LEFT_MOTOR_PIN, OUTPUT);
pinMode(LEFT_MOTOR_DIRECTION_PIN, OUTPUT);
// Init IR control
irrecv.enableIRIn();
// Init serial port
Serial.begin(9600);
// Init motors
motor_control(FORW, 0, FORW, 0);//run motor
}
void loop()
{
if(irrecv.decode(&results))
{
dump(&results);
irrecv.resume();
}
}