source: orbit/arduino/puzzlebox_orbit_A_HMRMFM_V2/puzzlebox_orbit_A_HMRMFM_V2.ino @ cdbf6d2

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

working sketch Version 2, clear out code

  • Property mode set to 100644
File size: 3.5 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 _height=88;
23char _pitch=63;
24char _rotation=78;
25char _channel='A';
26char _command;
27
28//generate ON/OFF control signals, with starting and stopping PWM generator
29void innerCycle(int onTime, int offTime)
30{
31  pwmWrite(IR, ON);
32  delayMicroseconds(onTime);
33  pwmWrite(IR, OFF);
34  delayMicroseconds(offTime);
35}
36
37void emitCode(char BIT)
38{
39  if (BIT) innerCycle(671,732);// 1
40  else innerCycle(337,402);// 0
41}
42
43void sendCode(char height,char rotation,char pitch)
44{
45  char n;
46 
47  //starting code, with special time period.
48  innerCycle(730,392); //(773 414)->824 450
49  innerCycle(730,392);
50  //height
51  for (n=6; n>=0; n--)  emitCode((height >> n) & 1);//emitCode generate LOWs between HIGHs as same as the parameter.
52 
53  emitCode(1);// meaning unclear
54 
55  //channel selection first half
56  if (_channel=='C') emitCode(1);
57  else emitCode(0);  //this digit equals 0 in channel A or B
58 
59  for (n=6; n>=0; n--)  emitCode((rotation >> n) & 1);//rotation
60 
61  //channel selection second half
62  if (_channel=='A') emitCode(1);
63  else emitCode(0); //if channel B or C, this digit equals 0;
64 
65  emitCode(0);//meaning unclear
66 
67  for (n=6; n>=0; n--)  emitCode((pitch >> n) & 1);//pitch 
68   
69  // unclear, "random" code 
70  emitCode(0); emitCode(1); emitCode(0);
71 
72  //finish code
73  emitCode(1);
74}
75
76void setup()
77{
78  pinMode(13, OUTPUT);
79  digitalWrite(13,LOW);
80  //initialize all timers except for 0, to save time keeping functions
81  InitTimersSafe();
82
83  //sets the frequency for the specified pin
84  bool success = SetPinFrequencySafe(IR, frequency);
85 
86  //if the pin frequency was set successfully, turn pin 13 on
87  if(success) {
88    digitalWrite(13, HIGH);   
89  }
90  Serial.begin(9600);
91}
92
93void loop()
94{int i;
95  if (Serial.available() > 0)
96  {
97    _command = Serial.read();
98    switch (_command)
99    {
100      case 'U':  _height+=5;  Serial.print("_height="); Serial.println(int(_height)); break;
101      case 'D':  _height-=5;  Serial.print("_height="); Serial.println(int(_height)); break;
102      case 'L':  _rotation+=5;  Serial.print("_rotation="); Serial.println(int(_rotation)); break;
103      case 'R':  _rotation-=5;  Serial.print("_rotation="); Serial.println(int(_rotation)); break;
104      case 'F':  _pitch+=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
105      case 'B':  _pitch-=5;  Serial.print("_pitch="); Serial.println(int(_pitch)); break;
106      case '1':  _channel='A';  Serial.print("_channel=A"); break;
107      case '2':  _channel='B';  Serial.print("_height=B"); break;
108      case '3':  _channel='C';  Serial.print("_height=C"); break;
109    }
110  }
111 
112  sendCode(_height,_rotation,_pitch);
113  delay(51); //originally 36, 51, 66, 80
114
115}
Note: See TracBrowser for help on using the repository browser.