source: orbit/arduino/puzzlebox_orbit_Uno_V3/puzzlebox_orbit_Uno_V3.ino

Last change on this file was 7b573dd, checked in by Steve Castellotti <sc@…>, 7 years ago
  • added support for external triggering of flight
  • Property mode set to 100644
File size: 6.6 KB
Line 
1/*
2
3Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker
4because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when
5the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library
6allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When
7SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the
8frequency was actually changed.
9
10This example runs on mega and uno.
11*/
12
13#include <PWM.h>
14
15// use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
16// int IR = 10;  // the pin that the Infrared LED is attached to
17int IR = 11;  // the pin that the Infrared LED is attached to
18int ON = 128; // 17.5us/26us = 172.3/256
19int OFF = 0;
20int32_t frequency = 38400; // frequency (in Hz)
21
22// variables start with "_" means global variable
23char _throttle=0; // 32~127, default 0
24char _pitch=31;   // 0~63, default 31
25char _yaw=78;     // 16~127, default 78
26char _channel='A';
27char _command;
28// long _mainCode=0;
29
30
31// generate ON/OFF control signals, with starting and stopping PWM generator
32void innerCycle(int onTime, int offTime)
33{
34pwmWrite(IR, ON);
35delayMicroseconds(onTime);
36pwmWrite(IR, OFF);
37delayMicroseconds(offTime);
38} // innerCycle
39
40
41void emitCode(char BIT) // emitCode generate LOWs between HIGHs as same as the parameter.
42{
43        if (BIT) innerCycle(671,732); // 1
44        else innerCycle(337,402); // 0
45} // emitCode
46
47
48void sendCode(long code)
49{
50        char n;
51        //starting code, with special time period.
52        innerCycle(730,392); //(773 414)->824 450
53        innerCycle(730,392);
54       
55        for (n=28;n>=0;n--)  emitCode((code >> n) & 1); // getting bits out from code
56       
57} // sendCode
58
59
60long formCode(char throttle,char yaw,char pitch)
61{
62        char n;
63        long mainCode=0;
64        int checkSum=0;
65       
66        // throttle
67        for (n=6; n>=0; n--)  bitWrite(mainCode,17+n,bitRead(throttle,n)); // getting the first 7 digits to mainCode
68       
69        bitWrite(mainCode,16,1); // meaning unclear, possibly left button.
70       
71        //channel selection first half
72        if (_channel=='C') bitWrite(mainCode,15,1);
73        else bitWrite(mainCode,15,0); // this digit equals 0 in channel A or B
74       
75        for (n=6; n>=0; n--)  bitWrite(mainCode,8+n,bitRead(yaw,n)); // yaw
76       
77        //channel selection second half
78        if (_channel=='A') bitWrite(mainCode,7,1);
79        else bitWrite(mainCode,7,0); // if channel B or C, this digit equals 0;
80       
81        bitWrite(mainCode,6,0); // meaning unclear, possibly right button.
82       
83        for (n=5; n>=0; n--)  bitWrite(mainCode,n,bitRead(pitch,n)); // pitch 
84       
85        // CheckSum
86        for (n=0; n<=20; n=n+4)  checkSum += ((mainCode >> n) & B1111); // sum up every 4 digits in the code
87       
88        checkSum=checkSum & B1111; // get the last 4 bits of the sum
89        checkSum=(16-checkSum) & B1111; // 16-sum is the formula of this helicopter
90       
91        mainCode= (mainCode << 5) | (checkSum << 1); // get the last 4 digit of CheckSum
92       
93        bitWrite(mainCode,0,1); // finish code
94        return mainCode; // mainCode is a 29 bit binary number
95       
96} // formCode
97
98
99void setup()
100{
101        pinMode(13, OUTPUT);
102        digitalWrite(13,LOW);
103        //initialize all timers except for 0, to save time keeping functions
104        InitTimersSafe();
105
106        //sets the frequency for the specified pin
107        bool success = SetPinFrequencySafe(IR, frequency);
108
109        //if the pin frequency was set successfully, turn pin 13 on
110        if(success) {
111                digitalWrite(13, HIGH);   
112        }
113       
114        Serial.begin(9600);
115       
116} // setup
117
118
119void setPitch() {
120
121        char inByte=0;
122        int a=0;
123        int b=0;
124        int c=0;
125        int newPitch=0;
126
127        while (Serial.available() == 0);
128        inByte = Serial.read() - '0';
129        //Serial.println(inByte);
130        a = inByte;
131
132        while (Serial.available() == 0);
133        inByte = Serial.read() - '0';
134        //Serial.println(inByte);
135        b = inByte;
136
137        while (Serial.available() == 0);
138        inByte = Serial.read() - '0';
139        //Serial.println(inByte);
140        c = inByte;
141
142        newPitch = (a * 100) + (b * 10) + c;
143
144        if (newPitch < 0)
145                newPitch=0;
146
147        if (newPitch > 100)
148                newPitch=100;
149               
150        _pitch=newPitch;
151
152        Serial.print("_pitch=");
153        Serial.println(int(_pitch));
154
155} // setPitch
156
157
158void setThrottle() {
159
160        char inByte=0;
161        int a=0;
162        int b=0;
163        int c=0;
164        int newThrottle=0;
165
166        while (Serial.available() == 0);
167        inByte = Serial.read() - '0';
168        //Serial.println(inByte);
169        a = inByte;
170
171        while (Serial.available() == 0);
172        inByte = Serial.read() - '0';
173        //Serial.println(inByte);
174        b = inByte;
175
176        while (Serial.available() == 0);
177        inByte = Serial.read() - '0';
178        //Serial.println(inByte);
179        c = inByte;
180
181        newThrottle = (a * 100) + (b * 10) + c;
182
183        if (newThrottle < 0)
184                newThrottle=0;
185
186        if (newThrottle > 100)
187                newThrottle=100;
188               
189        _throttle=newThrottle;
190
191        Serial.print("_throttle=");
192        Serial.println(int(_throttle));
193
194} // setThrottle
195
196
197void setYaw() {
198
199        char inByte=0;
200        int a=0;
201        int b=0;
202        int c=0;
203        int newYaw=0;
204
205        while (Serial.available() == 0);
206        inByte = Serial.read() - '0';
207        //Serial.println(inByte);
208        a = inByte;
209
210        while (Serial.available() == 0);
211        inByte = Serial.read() - '0';
212        //Serial.println(inByte);
213        b = inByte;
214
215        while (Serial.available() == 0);
216        inByte = Serial.read() - '0';
217        //Serial.println(inByte);
218        c = inByte;
219
220        newYaw = (a * 100) + (b * 10) + c;
221
222        if (newYaw < 0)
223                newYaw=0;
224
225        if (newYaw > 100)
226                newYaw=100;
227               
228        _yaw=newYaw;
229
230        Serial.print("_yaw=");
231        Serial.println(int(_yaw));
232
233} // setYaw
234
235
236void loop()
237{
238       
239        uint8_t flightActive = map(analogRead(A0), 0, 1023, 0, 100);
240        Serial.print("flightActive [A0]: ");
241        Serial.println(flightActive, DEC);
242       
243        if (flightActive > 85)
244                _throttle=85;
245        else
246                _throttle=0;
247       
248        int i;
249        if (Serial.available() > 0)
250        {
251                _command = Serial.read();
252                switch (_command)
253                {
254                        case 'P': _throttle=85; Serial.print("_throttle="); Serial.println(int(_throttle)); break;
255                        case 'O': _throttle=0; Serial.print("_throttle="); Serial.println(int(_throttle)); break;
256                        case 'U':  _throttle+=1;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
257                        case 'D':  _throttle-=1;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
258                        case 'L':  _yaw+=15;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
259                        case 'R':  _yaw-=15;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
260                        case 'F':  _pitch+=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
261                        case 'B':  _pitch-=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
262                        case '1':  _channel='A';  Serial.println("_channel=A"); break;
263                        case '2':  _channel='B';  Serial.println("_channel=B"); break;
264                        case '3':  _channel='C';  Serial.println("_channel=C"); break;
265                        case 'p':  setPitch(); break;
266                        case 't':  setThrottle(); break;
267                        case 'y':  setYaw(); break;
268                        case 'x':  setThrottle(); break;
269                }
270        }
271       
272        sendCode(formCode(_throttle,_yaw,_pitch));
273        delay(80); //originally 36, 51, 66, 80
274       
275} // loop
276
Note: See TracBrowser for help on using the repository browser.