-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_channel.ino
66 lines (52 loc) · 1.7 KB
/
one_channel.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
#include <MsTimer2.h>
volatile unsigned long oldTime = micros ();
volatile unsigned long currentTime = micros ();
const int datalen = 150;
volatile unsigned long data [datalen] = {};
volatile int counter = 0;
// is there anything other than zeros?
boolean isnone (unsigned long thedata [datalen]) {
noInterrupts (); // disable interrupts
for (int i = 0; i <datalen; i ++) {
if (thedata [i]> 0) {
interrupts (); // enable interrupts
return true;
}
}
interrupts (); // enable interrupts
return false;
}
void by_timer () {
noInterrupts (); // disable interrupts
if (isnone (data)) {
for (int i = 0; i <datalen; i ++) {
Serial.print (data [i]);
Serial.print (":");
}
Serial.println ();
}
memset (data, 0, sizeof (data)); // clear output
counter = 0; // clear counter
interrupts (); // enable interrupts
}
void by_interrupt () {
noInterrupts (); // disable interrupts
if (counter <datalen) {
currentTime = micros (); // ms from interrupt
data [counter] = currentTime - oldTime; // add to the array (old time minus current)
data [counter + 1] = digitalRead (2);
counter = counter + 2;
oldTime = currentTime; // update minus current time
interrupts (); // enable interrupts
MsTimer2 :: start (); // if after the last interrupt n seconds have passed, "by_timer ()" will be called
}
}
void setup () {
Serial.begin (115200);
MsTimer2 :: set (1000, by_timer);
MsTimer2 :: start ();
pinMode (2, INPUT_PULLUP); // if there is no INPUT_PULLUP , it is necessary to pull pin 2 to the ground by 10k-100k resistor
attachInterrupt (0, by_interrupt, CHANGE); // CHANGE FALLING //Interrupt 0 is digital pin 2
}
void loop () {
}