-
Notifications
You must be signed in to change notification settings - Fork 1
/
infrared.ino
87 lines (73 loc) · 2.19 KB
/
infrared.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
void IR_send_signal(unsigned int IR_signal[]) {
for(int i = 0; i < IR_SIGNAL_LENGTH; i++){
unsigned IR_signal_bit = IR_signal[i];
if(i % 2 == 0) IR_send_pulse(IR_signal_bit);
else IR_send_pause(IR_signal_bit);
}
}
void IR_send_pulse(unsigned int pulse_length) {
// Send one pulse of a given length
// Pin must be toggling between ON and OFF at 37-38kHz
// This variable keeps track of wether the pin is ON or OFF
int IR_on = 0;
long startMicros = micros();
while (micros() < (startMicros + pulse_length)){
// toggle pin and wait 26 us to make it a pulse
IR_on = 1 - IR_on;
digitalWrite(IR_EMITTER_PIN, IR_on);
// IR LED frequency must be around 37-38kHz
// Approx pulse period = 26us
// A period consists of the LED being toggled twice => 13us between toggles
delayMicroseconds(13);
}
// turn off IR after pulse is complete
digitalWrite(IR_EMITTER_PIN, LOW);
}
void IR_send_pause(unsigned int pause_length) {
// A pause is just not doing anything for a given amount of time
delayMicroseconds(pause_length);
}
// LEGACY CODE FROM HERE
//void IR_send_signal(int IR_signal[]) {
//
// for (int i=0; i < IR_SIGNAL_LENGTH; i++){
//
// int IR_signal_bit = IR_signal[i];
//
// if (IR_signal_bit > 0) {
// IR_send_pulse(IR_signal_bit);
// }
// else {
// IR_send_pause(-IR_signal_bit);
// }
// }
//}
//void IR_send_pulse(int pulse_length) {
// // Send one pulse of a given length
//
// int IR_on = 0;
// long startMicros = micros();
// while (micros() < (startMicros + pulse_length)){
// // toggle pin and wait 26 us to make it a pulse
// if (IR_on == 0) {
// IR_on = 1;
// }
// else {
// IR_on = 0;
// }
// digitalWrite(IR_LED_PIN, IR_on);
//
// // IR LED frequency must be around 37-38kHz
// // Approx pulse period = 26us
// // A period consists of the LED being toggled twice => 13us between toggles
// delayMicroseconds(13);
// }
//
// // turn off IR after pulse is complete
// digitalWrite(IR_LED_PIN, LOW);
//}
//
//void IR_send_pause(int pause_length) {
// // A pause is just not doing anything for a given amount of time
// delayMicroseconds(pause_length);
//}