-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmCube.ino
74 lines (68 loc) · 1.78 KB
/
mCube.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// this only works on atmels with the 16bit timer such as the ATmega328
int signal = 400;
int times[] = {signal, 1100, signal, 1100, signal, 700, signal, 1100, signal, 700, signal, 700, signal, 700, signal, 700, signal, 11168};
int timeslen = 18;
int timecounter = 0;
void setTimes(int roll, int pitch, int yaw, int thrust)
{
// pitch (1): 0 -> forwards, 255 -> backwards
// roll (2): 0 -> right, 255 -> left
// thrust (3): 0 -> down, 255 -> up
// yaw (4): 0 -> right, 255 -> left
int low = 700;
int high = 1500;
int length = 22000;
times[1] = map(pitch, 0, 255, low, high);
times[3] = map(roll, 0, 255, low, high);
times[5] = map(thrust, 0, 255, low, high);
times[7] = map(yaw, 0, 255, low, high);
int sum = 0;
for(int i=0; i < timeslen-1; i++)
sum += times[i];
times[timeslen-1] = length-sum;
}
void setup()
{
// set pin 9 (OC1A) to output
pinMode(9, OUTPUT);
// 16bit timer1
noInterrupts();
// clear timer registers
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TIMSK1 = 0;
// toggle OC1A on compare match
TCCR1A |= (1 << COM1A0);
// set mode to CTC with compare to OCR1A
TCCR1B |= (1 << WGM12);
// set /8 prescale
TCCR1B |= (1 << CS11);
// enable ouptu compare A interrupts
TIMSK1 |= (1 << OCIE1A);
// set compare match register
OCR1A = 2000;
interrupts();
Serial.begin(115200);
}
ISR(TIMER1_COMPA_vect) // interrupt service routine
{
OCR1A = times[timecounter]*2;
timecounter++;
if(timecounter >= timeslen)
timecounter = 0;
}
void loop()
{
if(Serial.available())
{
int roll = Serial.parseInt();
int pitch = Serial.parseInt();
int yaw = Serial.parseInt();
int thrust = Serial.parseInt();
if (Serial.read() == '\n')
{
setTimes(roll, pitch, yaw, thrust);
}
}
}