-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoIrMotorController.ino.bkp
106 lines (80 loc) · 2.56 KB
/
ArduinoIrMotorController.ino.bkp
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
#include <Arduino.h>
#include <IRremote.h>
// Arduino Pins
const int RECV_PIN = 11;
const int ENGINE_FORWARD_PIN = 6;
const int LIGHT_PIN = 3;
const int ENGINE_BACKWARD_PIN = 5;
// Remote Key Values
const unsigned long KEY_PLAY_PAUSE = 0x77E12084;
const unsigned long KEY_FORWARD = 0x77E1E084;
const unsigned long KEY_BACKWARD = 0x77E11084;
const unsigned long KEY_UP = 0x77E1D084;
const unsigned long KEY_DOWN = 0x77E1B084;
const unsigned long KEY_MENU = 0x77E14084;
const unsigned long FOLLOWUP_KEYPRESS = 0xFFFFFFFF;
const int MAX_VELOCITY = 7;
const int MAX_OUTPUT_LEVEL = 255;
// variables
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key;
unsigned long lastKey = 0;
bool light = false;
int enginePace = 0; // -MAX_VELOCITY .. 0 .. MAX_VELOCITY
int engineVelocity = 0; // abs(speed) -> 0 .. MAX_VELOCITY
int engineOutputPinLevel = 0;
int engineRoute = 0; // -1, 1
void setup()
{
// Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
// Serial.println(results.value, HEX);
key = results.value;
if (key == FOLLOWUP_KEYPRESS) {
key = lastKey;
}
switch(key) {
case KEY_PLAY_PAUSE:
// Serial.println("KEY_PLAY_PAUSE");
enginePace = 0;
break;
case KEY_FORWARD:
// Serial.println("KEY_FORWARD");
enginePace < MAX_VELOCITY ? enginePace++ : enginePace;
break;
case KEY_BACKWARD:
// Serial.println("KEY_BACKWARD");
enginePace > -MAX_VELOCITY ? enginePace-- : enginePace;
break;
case KEY_MENU:
// Serial.println("KEY_MENU");
light = !light;
break;
}
engineVelocity = abs(enginePace);
engineRoute = enginePace >> 7;
engineOutputPinLevel = (MAX_OUTPUT_LEVEL * engineVelocity ) / MAX_VELOCITY;
int forwardLevel = (1+engineRoute) * engineOutputPinLevel;
int backwardLevel = -1 * engineRoute * engineOutputPinLevel;
analogWrite(ENGINE_FORWARD_PIN, forwardLevel );
analogWrite(ENGINE_BACKWARD_PIN, backwardLevel);
// Serial.print("enginePace: ");
// Serial.println(enginePace);
// Serial.print("engineVelocity: ");
// Serial.println(engineVelocity);
// Serial.print("engineRoute: ");
// Serial.println(engineRoute);
// Serial.print("forwardLevel: ");
// Serial.println(forwardLevel);
// Serial.print("backwardLevel: ");
// Serial.println(backwardLevel);
// Serial.println("\n");
analogWrite(LIGHT_PIN, 255 * light);
lastKey = key;
irrecv.resume(); // Receive the next value
}
}