-
Notifications
You must be signed in to change notification settings - Fork 1
/
iot_electronic_mos_killer.ino
101 lines (79 loc) · 2.26 KB
/
iot_electronic_mos_killer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
int SER_Pin = A5;
int RCLK_Pin = A4;
int SRCLK_Pin = A3;
byte digit[4] ={0};
byte Digicount = 0;
byte seven_seg_digits[10] = { B00000011, // = 0
B10011111, // = 1
B00100101, // = 2
B00001101, // = 3
B10011001, // = 4
B01001001, // = 5
B01000001, // = 6
B00011111, // = 7
B00000001, // = 8
B00011001 // = 9
};
volatile uint32_t lastInterrupt = 0;
volatile uint32_t length = 0;
volatile bool interrupted = 0;
volatile bool eventStart = 0;
void pinChanged(){
//Serial.println("INT");
if(!digitalRead(7)){
//Serial.println("Low");
//要超過500ms
if(millis()-lastInterrupt>500){
lastInterrupt = millis();
eventStart = 1;//蚊子被電死:事件發生
}
}
else{
//Serial.println("High");
if(!interrupted && eventStart==1){
length = millis()-lastInterrupt;
interrupted = 1;
eventStart = 0;
}
}
}
void setup(){
Serial.begin(115200);
Serial1.begin(115200);
//while(!Serial);
pinMode(A5,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A3,OUTPUT);
Serial.println("Started");
pinMode(7, INPUT);
//當pin2改變時發interrupt ,執行change
attachInterrupt(4,pinChanged, CHANGE);
}
uint32_t count = 0;
uint32_t pulseStart = 0;
bool pulseEnter = 0;
void loop(){
updateDisplay();
if(interrupted){
count++;
//send to mcs
Serial.print(count);Serial.print(",");Serial.println(length);
Serial1.print(count);Serial1.print(",");Serial1.println(length);
uint8_t disp = count%10000;
digit[3] = disp/1000;
disp = disp%1000;
digit[2] = disp/100;
disp = disp%100;
digit[1] = disp/10;
disp = disp%10;
digit[0] = disp;
interrupted = 0;
}
}
void updateDisplay(){
Digicount = (Digicount+1)%4;
digitalWrite(RCLK_Pin, LOW);
shiftOut(SER_Pin, SRCLK_Pin, LSBFIRST, seven_seg_digits[digit[Digicount]]);
shiftOut(SER_Pin, SRCLK_Pin, MSBFIRST, (uint8_t)1<<Digicount);
digitalWrite(RCLK_Pin, HIGH);
}