Created
June 8, 2016 04:53
-
-
Save monpetit/3ef5bb49e1763a4ec675263dc886e461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define ARRAY_SIZE 30 | |
| #define MEASURE_INVERVAL_MS 100 | |
| #define REPORT_INVERVAL_MS 5000 | |
| const int measurePin = A0; | |
| const int ledPower = 3; | |
| const int samplingTime = 280; | |
| const int deltaTime = 40; | |
| const int sleepTime = 9680; | |
| float voMeasured = 0; | |
| float calcVoltage = 0; | |
| float dustDensity = 0; | |
| float dustDensityList[ARRAY_SIZE]; | |
| uint32_t index = 0; | |
| uint32_t measure_tick, last_measure_tick; | |
| uint32_t report_tick, last_report_tick; | |
| void adc10_measure_dust_density(void) | |
| { | |
| digitalWrite(ledPower, LOW); // power on the LED | |
| delayMicroseconds(samplingTime); | |
| voMeasured = analogRead(measurePin); // read the dust value | |
| delayMicroseconds(deltaTime); | |
| digitalWrite(ledPower, HIGH); // turn the LED off | |
| delayMicroseconds(sleepTime); | |
| // 0 - 5.0V mapped to 0 - 1023 integer values | |
| // recover voltage | |
| calcVoltage = (voMeasured + 1) * (5.0 / 1024); | |
| // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ | |
| // Chris Nafis (c) 2012 | |
| dustDensity = ((0.6 / 3.5) * calcVoltage - 0.1) * 1000; | |
| if (dustDensity < 0.0) | |
| dustDensity = 0.0; | |
| } | |
| void prepare_data(void) | |
| { | |
| for (index = 0; index < ARRAY_SIZE; index++) { | |
| adc10_measure_dust_density(); | |
| delay(2); | |
| dustDensityList[index] = dustDensity; | |
| } | |
| } | |
| void adc10_append_dust_density(void) | |
| { | |
| adc10_measure_dust_density(); | |
| dustDensityList[index % ARRAY_SIZE] = dustDensity; | |
| index++; | |
| } | |
| float average_dust_density(void) | |
| { | |
| float sum = 0.0; | |
| for (int i = 0; i < ARRAY_SIZE; i++) | |
| sum += dustDensityList[i]; | |
| return (sum / ARRAY_SIZE); | |
| } | |
| void report_dust_density(void) | |
| { | |
| Serial.write(0x11); | |
| delayMicroseconds(2); | |
| Serial.write(0x13); | |
| delayMicroseconds(2); | |
| Serial.write(0x00); | |
| delayMicroseconds(2); | |
| Serial.println("DUST DENSITY:"); | |
| Serial.print(" "); | |
| Serial.print(average_dust_density()); | |
| Serial.print(" "); | |
| Serial.write(0xE4); // micro(u) | |
| Serial.print("g/m"); | |
| Serial.write(0xAE); // "3" | |
| } | |
| void setup(void) | |
| { | |
| pinMode(ledPower, OUTPUT); | |
| analogReference(EXTERNAL); | |
| Serial.begin(9600); | |
| prepare_data(); | |
| last_measure_tick = last_report_tick = millis(); | |
| } | |
| void loop(void) | |
| { | |
| measure_tick = report_tick = millis(); | |
| if ((measure_tick - last_measure_tick) >= MEASURE_INVERVAL_MS) { | |
| last_measure_tick = measure_tick; | |
| adc10_append_dust_density(); | |
| } | |
| if ((report_tick - last_report_tick) >= REPORT_INVERVAL_MS) { | |
| last_report_tick = report_tick; | |
| report_dust_density(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment