source: orbit/arduino/puzzlebox_orbit_Uno_V3/puzzlebox_orbit_Uno_V3.ino @ 4af2eca

RawEEGRaw_EEG_PlotServoTab_Interfacepyramid
Last change on this file since 4af2eca was 4af2eca, checked in by Steve Castellotti <sc@…>, 7 years ago
  • Added support for "x" control command, allowing throttle percentage to be set directly (for example x000 for 0% and x100 for 100%)
  • Property mode set to 100644
File size: 4.9 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 setThrottle() {
110 
111  char inByte=0;
112  int a=0;
113  int b=0;
114  int c=0;
115  int newThrottle=0;
116 
117  while (Serial.available() == 0);
118  inByte = Serial.read() - '0';
119  //Serial.println(inByte);
120  a = inByte;
121 
122  while (Serial.available() == 0);
123  inByte = Serial.read() - '0';
124  //Serial.println(inByte);
125  b = inByte;
126 
127  while (Serial.available() == 0);
128  inByte = Serial.read() - '0';
129  //Serial.println(inByte);
130  c = inByte;
131 
132  newThrottle = (a * 100) + (b * 10) + c;
133 
134  if (newThrottle < 0)
135    newThrottle=0;
136 
137  if (newThrottle > 100)
138    newThrottle=100;
139   
140  _throttle=newThrottle;
141 
142  Serial.print("_throttle=");
143  Serial.println(int(_throttle));
144 
145}
146
147void loop()
148{int i;
149  if (Serial.available() > 0)
150  {
151    _command = Serial.read();
152    switch (_command)
153    {
154      case 'U':  _throttle+=5;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
155      case 'D':  _throttle-=5;  Serial.print("_throttle="); Serial.println(int(_throttle)); break;
156      case 'L':  _yaw+=5;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
157      case 'R':  _yaw-=5;  Serial.print("_yaw="); Serial.println(int(_yaw)); break;
158      case 'F':  _pitch+=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
159      case 'B':  _pitch-=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
160      case '1':  _channel='A';  Serial.print("_channel=A"); break;
161      case '2':  _channel='B';  Serial.print("_throttle=B"); break;
162      case '3':  _channel='C';  Serial.print("_throttle=C"); break;
163      case 'x':  setThrottle(); break;
164    }
165  }
166 
167  sendCode(formCode(_throttle,_yaw,_pitch));
168  delay(80); //originally 36, 51, 66, 80
169
170}
Note: See TracBrowser for help on using the repository browser.