Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (60 sloc) 2.45 KB
#include <math.h>
int pin = 0;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup() {
pinMode(pin,INPUT);
starttime = millis();
}
void loop() {
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
char data[1000];
sprintf(data, "{ lpo: %lu, r: %f, c: %f }", lowpulseoccupancy, ratio, concentration);
Spark.publish("yahi", data);
lowpulseoccupancy = 0;
starttime = millis();
}
}
/*
* pulseIn Function for the Spark Core - Version 0.1.1 (Beta)
* Copyright (2014) Timothy Brown - See: LICENSE
*
* Due to the current timeout issues with Spark Cloud
* this will return after 10 seconds, even if the
* input pulse hasn't finished.
*
* Input: Trigger Pin, Trigger State
* Output: Pulse Length in Microseconds (10uS to 10S)
*
*/
unsigned long pulseIn(uint16_t pin, uint8_t state) {
GPIO_TypeDef* portMask = (PIN_MAP[pin].gpio_peripheral); // Cache the target's peripheral mask to speed up the loops.
uint16_t pinMask = (PIN_MAP[pin].gpio_pin); // Cache the target's GPIO pin mask to speed up the loops.
unsigned long pulseCount = 0; // Initialize the pulseCount variable now to save time.
unsigned long loopCount = 0; // Initialize the loopCount variable now to save time.
unsigned long loopMax = 20000000; // Roughly just under 10 seconds timeout to maintain the Spark Cloud connection.
// Wait for the pin to enter target state while keeping track of the timeout.
while (GPIO_ReadInputDataBit(portMask, pinMask) != state) {
if (loopCount++ == loopMax) {
return 0;
}
}
// Iterate the pulseCount variable each time through the loop to measure the pulse length; we also still keep track of the timeout.
while (GPIO_ReadInputDataBit(portMask, pinMask) == state) {
if (loopCount++ == loopMax) {
return 0;
}
pulseCount++;
}
// Return the pulse time in microseconds by multiplying the pulseCount variable with the time it takes to run once through the loop.
return pulseCount * 0.405; // Calculated the pulseCount++ loop to be about 0.405uS in length.
}