Back to Parent

/*  retrospectre particle argon iot board source code
 *  accelerometer triggers cloud upload of light and temp sensor inputs
 *  Harley Guo
 *  March 2022
 *  Carnegie Mellon University
 */


#include <string.h>


const int xPin = A1; 
const int yPin = A2; 
const int zPin = A3;
const int lightPin = A5;
const int tempPin = A4;
const double THRESHOLD = 0.05;


bool threshold(double x) {
    return x > THRESHOLD;
}


void setup()
{  
    pinMode(lightPin, INPUT);
    pinMode(tempPin, INPUT);
}  
void loop()
{   
    int x = 0;
    int y = 0;
    int z = 0;    
    int light, temp;
    double xG, yG, zG;

    char outbuf[50];
    char *outp = outbuf;
    
    // avg 10 accelerometer reads
    for (int i=0; i<10; i++) {
        x += analogRead(xPin);  
        y += analogRead(yPin); 
        z += analogRead(zPin);  
    }
    
    x /= 10; 
    y /= 10;
    z /= 10;
    
    // calculate g from voltage
    xG = ((((double)(x * 3.3)/4095) - 1.65 ) / 0.330 );   
    yG = ((((double)(y * 3.3)/4095) - 1.65 ) / 0.330 );   
    zG = (((((double)(z * 3.3)/4095) - 1.80 ) / 0.330 ) - 0.60); 

    
    // accelerometer triggers
    if (threshold(xG) || threshold(yG) || threshold(zG)) {
        light = analogRead(lightPin);
        temp = (analogRead(tempPin)-500)/10;
        
        // write buffer
        outp += sprintf(outp, "%d", light);
        outp += sprintf(outp, "%s", "|");
        outp += sprintf(outp, "%d", temp);
        
        //publish
        if (Particle.connected()) {
            Particle.publish("info", outbuf);
        }
        
        delay(10000);
    }
    
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0