Haha, I tried to clean it up some and use indents… All works as is, at the 20th sample of sensor-3 a value of 100 is added to sensor[4] value (this holds the past three sensor values averaged) to trip an alert event. This part is located in the ‘BEGIN SENSOR VALUE EVENT CHECK’ with the following code:
if (sc == 20)
sv[3][4] = sv[3][4] + 100; // ADD 100 TO SENSOR-3 VALUE TO SIMULATE EVENT
Sensor alert values are set per sensor, 1 through 6 since each sensor could and most probably will have a different trip alert then the others. See this on line 6:
int sa[7] = {10, 11, 12, 13, 14, 15}; // SET ARRAY FOR SENSOR ALERT
Full code:
#include "application.h"
using namespace std;
// BEGIN USER CONFIGURABLE PARAMETERS
int sa[7] = {10, 11, 12, 13, 14, 15}; // SET ARRAY FOR SENSOR ALERT
int samplei = 2; // SAMPLE INTERVAL, HOW MANY SECONDS TO WAIT BETWEEN EACH
int publishf = 15; // WAIT UNTIL x NUMBER OF SAMPLES ARE GATHERED TO BURST PUBLISH
int scasamplei = 1; // ALERT SAMPLE INTERVAL, HOW MANY SECONDS TO WAIT BETWEEN EACH
int scacr = 2; // WHILE IN SENSOR ALERT - HOW MANY COMPLETE PUBLISH CYCLES (AS DEFINED ABOVE) TO LOOP THROUGH
int scac = 31; //*HOW MANY SENSOR SAMPLES TO TAKE WHILE IN EVENT/ALERT MODE
// ^ = ( publishf * 2 ) + 1
float storage[16][7]; //*DIMENSION THE STORAGE CONTAINER ARRAY
// ^ publishf + 1
// END OF USER CONFIGURABLE PARAMETERS
// DECLARE ITEMS USED AND SET INITIAL PARAMETERS
int csamplei = samplei; // SET CURRENT SAMPLE INTERVAL TO NORMAL SAMPLE INTERVAL AS ABOVE
char fullpublish[500]; //fullpublish[0] = '\0'; // CONTAINS THE STRING TO BE PUBLISHED
char tempd[5]; // TEMPORY HOLDING STRING USED FOR FULLPUBLISH EVENT
int satemp = 0; // USED AS A TEMP CONTAINER IN SENSOR ALERT CHECKING
int v = 6; // THE NUMBER IF VALUES TO COLLECT PER SAMPLE
int w = 0; // THE NUMBER OF SAMPLES TO GATHER
int x = 0; // THE NUMBER OF SAMPLES COUNTER
int xx = 0; // THE NUMBER OF SAMPLES COUNTER in ALERT STATE
int y = 0; // THE SAMPLE PER COUNT GATHERED
int z = 0; // FOR TO LOOP TO SIMULATE 'VOID LOOP()'
int pc = 0; // COUNTER FOF ALL PUBLISHES PERFORMED
int sc = 0; // COUNTER FOF ALL SAMPLES PERFORMED
int ec = 0; // SENSOR SAMPLE EVENT CHECK
int ecd = 0; // SENSOR SAMPLE EVENT CHECK DEPTH
int ecdiff = 0; // SENSOR EVENT CHECK DIFFERENCE
int scact; // CYCLE MARK BASED ON 'SC' TO CONTINUE ALERT REPORTING
int t2 = 0; // EQUALS TIME(0) + SAMPLEI, USED TO DETERMINE WHEN TO TAKE SAMPLE
int cias = 0; // CHANGE IN ALERT STATE FLAG
float sv[6][6]; // DIMENSION THE SENSOR VALUE ARRAY
// END OF DECLARATIONS AND INITIAL PARAMETERS
void setup()
{
Serial.begin(9600);
}
void loop()
{
// BEGIN TO COLLECT SENSOR VALUES AND PLACE IN SV[ARRAY]
sv[0][0] = 440;
sv[1][0] = 441;
sv[2][0] = 422;
sv[3][0] = 463;
sv[4][0] = 444;
sv[5][0] = 445;
// END OF COLLECTING SENSOR VALUES THAT WERE PLACED IN SV[ARRAY]
///\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\///
// GATHER, EVENT CHECK, AND PUBLISH COLLECTED SENSOR VALUES BASED ON csamplei AND publishf //
///\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\///
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// BEGIN SENSOR VALUE EVENT CHECK //
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
if (sc > ec) {
ec = sc;
if (ecd == 3) {
ecd = 1;
}
else {
ecd++;
}
for (w = 0; w < v; w++) {
sv[w][ecd] = sv[w][0];
sv[w][4] = ((sv[w][1] + sv[w][2] + sv[w][3]) / 3);
if (sc == 20)
sv[3][4] = sv[3][4] + 100; // ADD 100 TO SENSOR-3 VALUE TO SIMULATE EVENT
if (sc > 3) {
satemp = sv[w][0] > sv[w][4] ? sv[w][0] - sv[w][4] : sv[w][4] - sv[w][0];
if (satemp > sa[w]) {
Serial.printf("Out of SA change on sensor %d, ", w);
Serial.printlnf("difference of %d, threshold set at %d.", satemp, sa[w]);
// DO SOMETHING HERE LIKE ALTER REPORTING TIMES OR SEND ALERT
cias = 1; // SIGNIFIES FRESH CHANGE INTO ALERT STATE SO PUBLISHER WILL PUBLISH STORAGE AND THEN START ALERT REPORTING
csamplei = scasamplei;
scact = sc + scac;
}
}
}
}
// CHECK IF WE ARE IN AN ALERT REPORT CONDITION AND SEE IF WE SHOULD COME OUT OF IT
if (sc > scact)
csamplei = samplei;
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// END SENSOR VALUR EVENT CHECK //
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// START OF SAMPLING TIME CHECK //
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
if (Time.now() > t2) {
for (w = 0; w < v; w++) {
storage[x][w] = sv[w][0];
}
x++;
t2 = Time.now() + csamplei;
if (scact > sc) {
Serial.printf("EVENT STATE: Saved readings in Storage: %d \n", x); // sc, scac, scact
Serial.printf("Current sampling count (sc) = %d / Alert sample count (scac) = %d / Current alert sample count (scact) = %d \n", sc, scac, scact);
}
else {
Serial.printf("Saved readings in Storage: %d \n", x);
}
sc++;
}
///\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// END OF SAMPLING TIME CHECK //
///\/\/\/\/\/\/\/\/\/\/\/\/\/\//
//\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// ALERT STATE PUBLISH FLUSH //
//\/\/\/\/\/\/\/\/\/\/\/\/\/\//
if (cias == 1 && scact > sc) {
for (xx = 0; xx < x; xx++) {
for (y = 0; y < v; y++) {
snprintf(tempd, sizeof(tempd), "%.0f%s", storage[x][y], (y == 5) ? "^" : ",");
strncat(fullpublish, tempd, sizeof(fullpublish) - strlen(fullpublish) - 1); // better to check the boundaries
}
} // BUILT FULLPUBLISH STRING
// DO EVENT WITH FULLPUBLISH SAMPLE - PARTICLE.PUBLISH EVENT
x = 0;
xx = 0;
cias = 0;
Serial.println("Published readings. ");
Serial.println(fullpublish);
string str(fullpublish);
Serial.printf("The size of published string is %d bytes. \n", str.length());
fullpublish[0] = 0; // CLEAR THE FULLPUBLISH STRING
pc++;
}
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// END OF ALERT STATE PUBLISH FLUSH //
///\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// START OF SAMPLES TAKEN CHECK, IF MET THEN PUBLISH //
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
if (x == publishf) {
for (x = 0; x < publishf; x++) {
for (y = 0; y < v; y++) {
if (scact > sc) {
snprintf(tempd, sizeof(tempd), "%.0f%s", storage[x][y], (y == 5) ? "!" : ",");
}
else {
snprintf(tempd, sizeof(tempd), "%.0f%s", storage[x][y], (y == 5) ? "^" : ",");
}
strncat(fullpublish, tempd, sizeof(fullpublish) - strlen(fullpublish) - 1); // better to check the boundaries
}
} // BUILT FULLPUBLISH STRING
// DO EVENT WITH FULLPUBLISH SAMPLE - ONCE ON ELECTRON, THIS WILL BE PARTICLE.PUBLISH EVENT
x = 0;
Serial.println("Published readings. ");
Serial.println(fullpublish);
string str(fullpublish);
Serial.printf("The size of published string is %d bytes. \n", str.length());
fullpublish[0] = 0; // CLEAR THE FULLPUBLISH STRING
pc++;
}
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
// END OF SAMPLES TAKEN CHECK, IF MET THEN PUBLISH //
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
///\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\///
// END OF GATHER AND PUBLISH COLLECTED SENSOR VALUES BASED ON csamplei & publishf //
///\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\///
}
Code is also shared via Particle Web IDE HERE.
Thanks much,
Kolbi