/
Combine0.6.ino
129 lines (94 loc) · 2.98 KB
/
Combine0.6.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This RFduino sketch demonstrates a Bluetooth LowEnergy connection between an iPhone application and an RFduino.
This sketch also demonstrates how to get air quality / temperature data from the sensors.
Author: Stream Gao
*/
#include <RFduinoBLEiAir.h>
int current_quality =-1;
int SWITCH = 6;
int RED_LED_PIN = 2;
int GREEN_LED_PIN = 3;
int BLUE_LED_PIN = 4;
int velocity=SECONDS(3);
float sensorValue = 0;
float temp = 0;
void setup() {
pinMode(SWITCH, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
Serial.begin(9600);
RFduinoBLE.advertisementData = "AirQuality";
// start the BLE stack
RFduinoBLE.begin();
Serial.println( RFduinoBLE.deviceName );
}
void loop() {
// sample once per second
RFduino_ULPDelay( velocity );
sensorValue = analogRead(5);
temp = RFduino_temperature(CELSIUS);
sensorValue = sensorValue+temp/100;
Serial.println(sensorValue);
RFduinoBLE.sendFloat(sensorValue); // send the sample to the iPhone
Serial.print("digitalread:");
Serial.println(digitalRead(SWITCH));
if ( digitalRead(SWITCH) == HIGH ) //if turn on the switch to light up
ledon( calibration(sensorValue) );
else //turn off the leds
ledoff();
}
void RFduinoBLE_onConnect()
{
Serial.println("connect!");
velocity=SECONDS(3);
sensorValue = analogRead(5);
temp = RFduino_temperature(CELSIUS);
sensorValue = sensorValue+temp/100;
Serial.println(sensorValue);
RFduinoBLE.sendFloat(sensorValue); // send the sample to the iPhone
}
void RFduinoBLE_onDisconnect()
{
velocity=INFINITE;
ledoff();
Serial.println("disconnect!");
}
void RFduinoBLE_onReceive(char *data, int len)
{
Serial.println("on Receive!!");
if (data[0]=='0') // IF turn the led on
Serial.println(data);
else //if not turn the leds off
Serial.println("on receive but no data!");
ledon( calibration(sensorValue) );
}
int calibration(float aqindex){ // need to change the whole library
//if( aqindex>200 ){ Serial.println("Super High Pollution!"); return 3;}
if(aqindex>160) { Serial.println("High Pollution!"); return 2;}
else if(aqindex<160 && aqindex>100) { Serial.println("Low Pollution!"); return 1;}
else if(aqindex<100) { Serial.println("Fresh Air! Yay~"); return 0; } //fresh air
else { Serial.println("Sensor error!"); return -1; }
}
void ledon( int which){
if( which == 0){//fresh air
analogWrite(RED_LED_PIN, 0);
analogWrite(BLUE_LED_PIN, 0);
analogWrite(GREEN_LED_PIN, 250);
}else if( which == 1) { //low pollution
analogWrite(RED_LED_PIN, 250);
analogWrite(BLUE_LED_PIN, 0);
analogWrite(GREEN_LED_PIN, 0);
}
else if( which == 2) { //high pollution
analogWrite(RED_LED_PIN, 250);
analogWrite(BLUE_LED_PIN, 200);
analogWrite(GREEN_LED_PIN, 0);
}
delay(3000); //light up at least for 3 seconds
}
void ledoff(){
analogWrite(RED_LED_PIN, 0);
analogWrite(BLUE_LED_PIN, 0);
analogWrite(GREEN_LED_PIN, 0);
}