source: orbit/arduino/puzzlebox_orbit_Uno_V3/puzzlebox_orbit_Uno_V3.ino @ f0fb814

RawEEGRaw_EEG_PlotServoTab_Interfacepyramid
Last change on this file since f0fb814 was f0fb814, checked in by AzureViolin <zhanghaotz@…>, 7 years ago

fully control helicopter

  • Property mode set to 100644
File size: 4.2 KB
Line 
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//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
16int IR = 10;                // the pin that the Infrared LED is attached to
17int ON = 128;         // 17.5us/26us = 172.3/256
18int OFF = 0;
19int32_t frequency = 38400; //frequency (in Hz)
20
21//variables start with "_" means global variable
22char _throttle=87; //32~127
23char _pitch=31; //0~63
24char _yaw=78; //16~127
25char _channel='A';
26char _command;
27//long _mainCode=0;
28
29//generate ON/OFF control signals, with starting and stopping PWM generator
30void innerCycle(int onTime, int offTime)
31{
32  pwmWrite(IR, ON);
33  delayMicroseconds(onTime);
34  pwmWrite(IR, OFF);
35  delayMicroseconds(offTime);
36}
37
38void emitCode(char BIT)//emitCode generate LOWs between HIGHs as same as the parameter.
39{
40  if (BIT) innerCycle(671,732);// 1
41  else innerCycle(337,402);// 0
42}
43
44void sendCode(long code)
45{
46 char n;
47  //starting code, with special time period.
48  innerCycle(730,392); //(773 414)->824 450
49  innerCycle(730,392);
50 
51  for (n=28;n>=0;n--)  emitCode((code >> n) & 1); //getting bits out from code
52
53}
54
55long formCode(char throttle,char yaw,char pitch)
56{
57  char n;
58  long mainCode=0;
59  int checkSum=0;
60
61  //throttle
62  for (n=6; n>=0; n--)  bitWrite(mainCode,17+n,bitRead(throttle,n)); //getting the first 7 digits to mainCode
63 
64  bitWrite(mainCode,16,1);  //meaning unclear, possibly left button.
65 
66  //channel selection first half
67  if (_channel=='C') bitWrite(mainCode,15,1);
68  else bitWrite(mainCode,15,0); //this digit equals 0 in channel A or B
69 
70  for (n=6; n>=0; n--)  bitWrite(mainCode,8+n,bitRead(yaw,n));//yaw
71 
72  //channel selection second half
73  if (_channel=='A') bitWrite(mainCode,7,1);
74  else bitWrite(mainCode,7,0); //if channel B or C, this digit equals 0;
75 
76  bitWrite(mainCode,6,0);// meaning unclear, possibly right button.
77 
78  for (n=5; n>=0; n--)  bitWrite(mainCode,n,bitRead(pitch,n));//pitch 
79   
80  // CheckSum
81  for (n=0; n<=20; n=n+4)  checkSum += ((mainCode >> n) & B1111);//sum up every 4 digits in the code
82 
83  checkSum=checkSum & B1111; //get the last 4 bits of the sum
84  checkSum=(16-checkSum) & B1111;//16-sum is the formula of this helicopter
85 
86  mainCode= (mainCode << 5) | (checkSum << 1); //get the last 4 digit of CheckSum, use XOR (^) to flip bits
87 
88  bitWrite(mainCode,0,1);  //finish code
89  return mainCode; //mainCode is a 29 bit binary number
90}
91
92void setup()
93{
94  pinMode(13, OUTPUT);
95  digitalWrite(13,LOW);
96  //initialize all timers except for 0, to save time keeping functions
97  InitTimersSafe();
98
99  //sets the frequency for the specified pin
100  bool success = SetPinFrequencySafe(IR, frequency);
101 
102  //if the pin frequency was set successfully, turn pin 13 on
103  if(success) {
104    digitalWrite(13, HIGH);   
105  }
106  Serial.begin(9600);
107}
108
109void loop()
110{int i;
111  if (Serial.available() > 0)
112  {
113    _command = Serial.read();
114    switch (_command)
115    {
116      case 'U':  _throttle+=5;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
117      case 'D':  _throttle-=5;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
118      case 'L':  _yaw+=5;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
119      case 'R':  _yaw-=5;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
120      case 'F':  _pitch+=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
121      case 'B':  _pitch-=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
122      case '1':  _channel='A';  Serial.print("_channel=A"); break;
123      case '2':  _channel='B';  Serial.print("_throttle=B"); break;
124      case '3':  _channel='C';  Serial.print("_throttle=C"); break;
125    }
126  }
127 
128  sendCode(formCode(_throttle,_yaw,_pitch));
129  delay(80); //originally 36, 51, 66, 80
130
131}
Note: See TracBrowser for help on using the repository browser.