-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSimpleRcScanner.ino
60 lines (46 loc) · 1.22 KB
/
SimpleRcScanner.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
#define SAMPLESIZE 500
static unsigned int timings[SAMPLESIZE];
static unsigned int pos = 0;
static unsigned long lastTime = 0;
static int receiverPin = 2;
static int interruptPin = 0;
void setup() {
interruptPin = digitalPinToInterrupt(receiverPin);
Serial.begin(9600);
attachInterrupt(interruptPin, handleInterrupt, CHANGE);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
for (int i = 5; i>0; i--) {
Serial.print(i);
Serial.print("... ");
delay(900);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
Serial.println();
detachInterrupt(interruptPin);
int finalstate = digitalRead(receiverPin);
char s = Serial.read();
for (unsigned int i = pos + finalstate; i< SAMPLESIZE; i++) {
Serial.print( timings[i] );
Serial.print(",");
}
for (unsigned int i = 0; i < pos; i++) {
Serial.print( timings[i] );
Serial.print(",");
}
Serial.println("");
Serial.println("Reset your Arduino to scan again...");
while(true) {}
}
void handleInterrupt() {
const long time = micros();
timings[pos] = time - lastTime;
lastTime = time;
if (++pos > SAMPLESIZE-1) {
pos = 0;
}
}