Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Created September 25, 2015 01:56
Show Gist options
  • Save harrisonhjones/9952547e6aad769992bf to your computer and use it in GitHub Desktop.
Save harrisonhjones/9952547e6aad769992bf to your computer and use it in GitHub Desktop.
Particle IO - ElapsedMillis Example
#include "elapsedMillis/elapsedMillis.h"
elapsedMillis timeElapsed;
unsigned char readSoilStateMachine = 0;
int rd = 0; // Global soil moisture value
void loop()
{
readSoil(); // Run this as often as possible (it's non-blocking)
}
int readSoil() {
if(readSoilStateMachine == 0) // Need to power up sensors
{
timeElapsed = 0; // Reset the timer
digitalWrite(power1, HIGH); // Turn the probes on
readSoilStateMachine = 1; // Next wait for the sensors to power up
}
else if(readSoilStateMachine == 1) // Waiting for sensors to power up
{
if(timeElapsed > 400)
{
readSoilStateMachine = 2; // Next we read the sensors
}
}
else if(readSoilStateMachine == 2) // Read the sensors
{
// Read the sensors and update the GLOBAL soil variable
int r1 = map(analogRead(soil1),4095,1500, 0,20);
int r2 = map(analogRead(soil2),4095,1500, 0,20);
rd = r1*100 + r2;
timeElapsed = 0; // Reset the timer
readSoilStateMachine = 3; // Next wait to power down the sensors
}
else if(readSoilStateMachine == 3) // Waiting to power down the sensors
{
if(timeElapsed > 100)
{
readSoilStateMachine = 4; // Next we wait to power down the sensors
}
}
else if(readSoilStateMachine == 4) // Power down the sensors
{
digitalWrite(power1, LOW); // Turn probes off
readSoilStateMachine = 5; // Next we wait until another reading is to take place
timeElapsed = 0; // Reset the timer
}
else if(readSoilStateMachine == 5) // Waiting to go through above states again (to read the sensors again)
{
if(timeElapsed > 1000)
{
readSoilStateMachine = 0; // Next we start from the beginning again
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment