I found this code in this community and I have altered to my requirements. But i am getting sensor high at alternative times. Kindly help me to sort this out
// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>
#include "application.h"
SYSTEM_THREAD(ENABLED);
#define TOKEN "BBFF-a1aXeKjT3QlzwOnae53ovppXPolR8k" // Put here your Ubidots TOKEN
Ubidots ubidots(TOKEN);
const char *eventName = "HeartBeat";
const char *eventBattery = "LowBattery";
// Various timing constants
const unsigned long MAX_TIME_TO_PUBLISH_MS = 120000; // Only stay awake for 60 seconds trying to connect to the cloud and publish
const unsigned long TIME_AFTER_PUBLISH_MS = 10000; // After publish, wait 4 seconds for data to go out
const unsigned long TIME_AFTER_BOOT_MS = 10000; // At boot, wait 5 seconds before going to sleep again (after coming online)
const unsigned long TIME_PUBLISH_BATTERY_SEC = 5 * 60; // every 4 hours, send a battery update
int count;
float value;
// Stuff for the finite state machine
enum State { ONLINE_WAIT_STATE, RESET_STATE, RESET_WAIT_STATE, PUBLISH_STATE, SLEEP_STATE, SLEEP_WAIT_STATE, BOOT_WAIT_STATE, LOW_BAT_STATE, ALERT_STATE };
State state = ONLINE_WAIT_STATE;
unsigned long stateTime = 0;
int awake = 0;
void setup() {
Serial.begin(9600);
FuelGauge fuel;
value = fuel.getVCell();
Particle.variable("SensorData", awake);
}
void loop() {
switch(state) {
case ONLINE_WAIT_STATE:
if (Particle.connected()) {
state = RESET_STATE;
}
if (millis() - stateTime > 5000) {
stateTime = millis();
Serial.println("waiting to come online");
}
break;
case RESET_STATE:
Serial.println("resetting sensor");
if (!(analogRead(A7))) {
Serial.println("Movement not found");
state = SLEEP_STATE;
break;
}
state = BOOT_WAIT_STATE;
break;
case PUBLISH_STATE:
if (Particle.connected()) {
char data[32];
snprintf(data, sizeof(data), "%d,%.02f,%.02f", awake, value);
Particle.publish(eventName, data, 60);
ubidots.add("SENSORDATA", awake);
ubidots.add("BATTERYLEVEL",value);
ubidots.sendAll();
stateTime = millis();
state = SLEEP_WAIT_STATE;
while(analogRead(A7)){
for(count=0; count ++;){
Serial.print(count);
snprintf(data, sizeof(data), "%d", count);
Particle.publish("Motioncount", data, 60);
}
}
} else {
// Haven't come online yet
if (millis() - stateTime >= MAX_TIME_TO_PUBLISH_MS) {
// Took too long to publish, just go to sleep
state = SLEEP_STATE;
}
}
break;
case SLEEP_WAIT_STATE:
if (millis() - stateTime >= TIME_AFTER_PUBLISH_MS) {
state = SLEEP_STATE;
}
break;
case BOOT_WAIT_STATE:
if (millis() - stateTime >= TIME_AFTER_BOOT_MS) {
// To publish the battery stats after boot, set state to PUBLISH_STATE
// To go to sleep immediately, set state to SLEEP_STATE
state = PUBLISH_STATE;
}
break;
case SLEEP_STATE:
// Sleep
System.sleep(A7, RISING, TIME_PUBLISH_BATTERY_SEC, SLEEP_NETWORK_STANDBY);
awake = ((analogRead(A7)) != 0);
Serial.printlnf("awake=%d", awake);
state = PUBLISH_STATE;
stateTime = millis();
break;
case LOW_BAT_STATE:
if(value <= 3.3){
state = ALERT_STATE;
}
else{
state = PUBLISH_STATE;
ubidots.sendAll();
}
break;
case ALERT_STATE:
if (Particle.connected()) {
char data1[32];
snprintf(data1, sizeof(data1), "%d,%.02f", value);
Particle.publish(eventBattery, data1, 60);
stateTime = millis();
state = SLEEP_WAIT_STATE;
} else {
// Haven't come online yet
if (millis() - stateTime >= MAX_TIME_TO_PUBLISH_MS) {
// Took too long to publish, just go to sleep
state = SLEEP_STATE;
}
}
break;
}
}