-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdust.ino
99 lines (86 loc) · 2.08 KB
/
dust.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
unsigned long time_rise=0;
unsigned long time_fall=0;
unsigned long time_mus=0;
unsigned int ov_counter=0;
int quantity=0;//2011/12/29 change by bruce
//double quantity=0;
float time_sum=0;
float rate=0;
boolean newValue = false;
void setupDust(){
pinMode(13,OUTPUT);
pinMode(8,INPUT);
pinMode(6,OUTPUT);
//set 16bit counter for measure the wide of sensor
TCCR1A = 0;
TCCR1B |=1<<(CS12)|0<<(CS11)|1<<(CS10);//Set clock 1024/16MHz,unit is 6.4us
TIMSK1 |=1<<(ICIE1)|1<<(TOIE1); //enable capture interrupt and overflow interrupt
TCNT1 = 0;
sei();
}
void reportDust(){
if(newValue){
sendJsonData();
/*
Serial.print("{\"type\":\"dust\",\"quantity\":");
Serial.print(quantity);
Serial.print(",\"rate\":");
Serial.print(rate,8);
Serial.println("}");*/
newValue = false;
}
}
void sendJsonData(){
/* Serial.print("{\"type\":\"dustLOP\",\"value\":");
Serial.print(lowpulseoccupancy);
Serial.println("}");*/
Serial.print("{\"type\":\"dustConcentration\",\"value\":");
Serial.print(quantity);
Serial.println("}");
Serial.print("{\"type\":\"dustRatio\",\"value\":");
Serial.print(rate,8);
Serial.println("}");
}
ISR(TIMER1_OVF_vect)
{
if(ov_counter==7)
{
PORTD^=0x40;
ov_counter=0;
rate=(float)(time_sum/336000);
if(rate<=8)
{
quantity=rate*562.5;//8 equal 4500 pcs Particle according to the datasheet.
}
else{
quantity=4500+(rate-8)*750;
}
time_sum=0;
newValue = true;
}
else
{
ov_counter++;
}
}
ISR(TIMER1_CAPT_vect)
{
if((PORTB^0x01)==1)
{
//time_fall=ICR1;
time_fall=micros();
TCCR1B=0x45; //change to rising capture and with 1024 prescaler
digitalWrite(13,HIGH);
//TIFR1|=1<<(TOV1);//reset the flag
}
else
{
time_rise=micros();
TCCR1B=0x05; //change to negative and with 1024 prescaler
digitalWrite(13,LOW);
//TIFR1|=1<<(TOV1);//reset the flag
if(time_rise>time_fall)
time_mus=20000+(time_rise-time_fall);//20000 is countervail for program run
time_sum+=+time_mus;
}
};