1 | /* |
---|
2 | |
---|
3 | Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker |
---|
4 | because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when |
---|
5 | the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library |
---|
6 | allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When |
---|
7 | SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the |
---|
8 | frequency was actually changed. |
---|
9 | |
---|
10 | This example runs on mega and uno. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <PWM.h> |
---|
14 | |
---|
15 | char outerCode[]={66,66,80,51,36,51,80,66,66,66,80,51,36,51,80,66,66,80,51,36,51,80,66,66,66,80,51,36,51,36,36,51,36,51,36,36,51,36,51,36,51,80,66,66,66,80,51,36,51,80}; |
---|
16 | |
---|
17 | //use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz |
---|
18 | int IR = 10; // the pin that the Infrared LED is attached to |
---|
19 | int ON = 128; // 17.5us/26us = 172.3/256 |
---|
20 | int OFF = 0; |
---|
21 | int32_t frequency = 38400; //frequency (in Hz) |
---|
22 | |
---|
23 | //variables start with "_" means global variable |
---|
24 | char _height=88; |
---|
25 | char _pitch=63; |
---|
26 | char _rotation=78; |
---|
27 | char _channel='A'; |
---|
28 | char _command; |
---|
29 | |
---|
30 | //generate ON/OFF control signals, with starting and stopping PWM generator |
---|
31 | void innerCycle(int onTime, int offTime) |
---|
32 | { |
---|
33 | pwmWrite(IR, ON); |
---|
34 | delayMicroseconds(onTime); |
---|
35 | pwmWrite(IR, OFF); |
---|
36 | delayMicroseconds(offTime); |
---|
37 | } |
---|
38 | |
---|
39 | void emitCode(char BIT) |
---|
40 | { |
---|
41 | if (BIT) innerCycle(671,732);// 1 |
---|
42 | else innerCycle(337,402);// 0 |
---|
43 | } |
---|
44 | |
---|
45 | void sendCode(char height,char rotation,char pitch) |
---|
46 | { |
---|
47 | char n; |
---|
48 | |
---|
49 | //starting code, with special time period. |
---|
50 | innerCycle(730,392); //(773 414)->824 450 |
---|
51 | innerCycle(730,392); |
---|
52 | //height |
---|
53 | for (n=6; n>=0; n--) emitCode((height >> n) & 1);//emitCode generate LOWs between HIGHs as same as the parameter. |
---|
54 | |
---|
55 | emitCode(1);// meaning unclear |
---|
56 | |
---|
57 | //channel selection first half |
---|
58 | if (_channel=='C') emitCode(1); |
---|
59 | else emitCode(0); //this digit equals 0 in channel A or B |
---|
60 | |
---|
61 | for (n=6; n>=0; n--) emitCode((rotation >> n) & 1);//rotation |
---|
62 | |
---|
63 | //channel selection second half |
---|
64 | if (_channel=='A') emitCode(1); |
---|
65 | else emitCode(0); //if channel B or C, this digit equals 0; |
---|
66 | |
---|
67 | emitCode(0);//meaning unclear |
---|
68 | |
---|
69 | for (n=6; n>=0; n--) emitCode((pitch >> n) & 1);//pitch |
---|
70 | |
---|
71 | // unclear, "random" code |
---|
72 | emitCode(0); emitCode(1); emitCode(0); |
---|
73 | |
---|
74 | //finish code |
---|
75 | emitCode(1); |
---|
76 | } |
---|
77 | |
---|
78 | void setup() |
---|
79 | { |
---|
80 | pinMode(13, OUTPUT); |
---|
81 | digitalWrite(13,LOW); |
---|
82 | //initialize all timers except for 0, to save time keeping functions |
---|
83 | InitTimersSafe(); |
---|
84 | |
---|
85 | //sets the frequency for the specified pin |
---|
86 | bool success = SetPinFrequencySafe(IR, frequency); |
---|
87 | |
---|
88 | //if the pin frequency was set successfully, turn pin 13 on |
---|
89 | if(success) { |
---|
90 | digitalWrite(13, HIGH); |
---|
91 | } |
---|
92 | Serial.begin(9600); |
---|
93 | } |
---|
94 | |
---|
95 | void loop() |
---|
96 | {int i; |
---|
97 | // 668533535335358 |
---|
98 | for (i=1;i<sizeof(outerCode);i++) |
---|
99 | { |
---|
100 | |
---|
101 | if (Serial.available() > 0) |
---|
102 | { |
---|
103 | _command = Serial.read(); |
---|
104 | switch (_command) |
---|
105 | { |
---|
106 | case 'U': _height+=5; break; |
---|
107 | case 'D': _height-=5; break; |
---|
108 | case 'L': _rotation+=5; break; |
---|
109 | case 'R': _rotation-=5; break; |
---|
110 | case 'F': _pitch+=5; break; |
---|
111 | case 'B': _pitch-=5; break; |
---|
112 | case '1': _channel='A'; break; |
---|
113 | case '2': _channel='B'; break; |
---|
114 | case '3': _channel='C'; break; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | sendCode(_height,_rotation,_pitch); |
---|
119 | delay(outerCode[i]); |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|