// This #include statement was automatically added by the Particle IDE.
#include <SparkFunMMA8452Q.h>
#include <math.h>
int sound_pin = A5;
// int acc_pin = A4; // TODO:
int sol_pin = D3;
int sound_reading = 0;
int acceleration = 0;
double midaccel = 0.99;
MMA8452Q accel; // Default constructor, SA0 pin is HIGH
double accRead = 0;
double accReadTrans = 0;
void setup() {
pinMode(sound_pin, INPUT);
pinMode(sol_pin, OUTPUT);
Particle.variable("sound", &sound_reading, INT); //Set up sound on consol for reading
accel.begin(SCALE_4G, ODR_1); // Set up accel with +/-2g range, and slowest (1Hz) ODR
Particle.variable("analogvalue", accRead); //Set up accelerometer on console for monitoring
}
void loop() {
// accel.available() will return 1 if new data is available, 0 otherwise
if (accel.available())
{
// To update acceleration values from the accelerometer, call accel.read();
accel.read();
// After reading, six class variables are updated: x, y, z, cx, cy, and cz.
// Those are the raw, 12-bit values (x, y, and z) and the calculated
// acceleration's in units of g (cx, cy, and cz).
// use the printAccelGraph funciton to print the values along with a bar
// graph, to see their relation to eachother:
printAccelGraph(accel.cx, "X", 20, 2.0);
printAccelGraph(accel.cy, "Y", 20, 2.0);
printAccelGraph(accel.cz, "Z", 20, 2.0);
Serial.println();
// Particle.publish( "accelerometerX", String(accel.cx));
accRead = sqrt(accel.cx*accel.cx + accel.cy*accel.cy + accel.cz*accel.cz);
Particle.publish( "accReadtrans", String( accReadTrans) );
//Particle.publish( "accRead", String( accRead) );
delay(3000);
// Particle.variable("analogvalue", accRead);
}else{
Particle.publish( "accelError" );
delay(3000);
}
// int sound_threshold[] = {50, 40, 30}; // TODO: define the threshold of sound (descending)
// int acc_threshold[] = {300, 200, 100}; // TODO: threshold of acceleration (descend)
Serial.begin(9600);
sound_reading = get_db(sound_pin);
Particle.publish( "sound_read", String(sound_reading));
// acceleration = get_acc(acc_pin);
// if(sound_reading >= sound_threshold[0] || acceleration >= acc_threshold[0])
// vibrate(3);
// else if(sound_reading >= sound_threshold[1] || acceleration >= acc_threshold[1])
// vibrate(2);
// else if(sound_reading >= sound_threshold[2] || acceleration >= acc_threshold[2])
// vibrate(1);
// else
// vibrate(0);
// delay(10000);
/* for test
if(sound_reading < 100)
vibrate(0);
else if(sound_reading < 200)
vibrate(1);
else if(sound_reading < 1000)
vibrate(2);
else
vibrate(3);
delay(1000);
*/
accReadTrans = accRead - midaccel;
if (accReadTrans < 0){
accReadTrans = -1 * accReadTrans;
}
if (sound_reading > 400 || accReadTrans > 0.07){
Particle.publish("diot/2019/paired/guardianteddy/vibrate", "2");
//level 2
vibrate(2);
}else{
if (sound_reading > 100 || accReadTrans > 0.02){
Particle.publish("diot/2019/paired/guardianteddy/vibrate", "1");
//level 1
vibrate(1);
}else{
//Particle.publish("diot/2019/paired/guardianteddy/vibrate", "0");
//level 0
vibrate(0);
}
}
/*
if(sound_reading < 100 && accRead < 1.1 && accRead > 0.9)
vibrate(0);
else if(sound_reading < 500 && accRead < 1.6 && accRead > 0.5)
vibrate(1);
else if(sound_reading < 1000 && accRead < 2 && accRead > 0.3)
vibrate(2);
else
vibrate(3);
delay(1000);
*/
}
int get_db(int sound_pin) {
int n = 15;
int sound = 0;
for(int i = 0; i < n; i++) {
sound += analogRead(sound_pin);
delay(10);
}
return sound / n;
}
void vibrate(int level) {
if(level == 0)
return;
for(int i = 0; i < 3 * level; i++) {
digitalWrite(sol_pin, HIGH);
delay(100 * level);
digitalWrite(sol_pin, LOW);
delay(500 / level);
}
}
void printAccelGraph(float value, String name, int numBarsFull, float rangeAbs)
{
// Calculate the number of bars to fill, ignoring the sign of numBars for now.
int numBars = abs(value / (rangeAbs / numBarsFull));
Serial.print(name + ": "); // Print the axis name and a colon:
// Do the negative half of the graph first:
for (int i=0; i<numBarsFull; i++)
{
if (value < 0) // If the value is negative
{
// If our position in the graph is in the range we want to graph
if (i >= (numBarsFull - numBars))
Serial.print('='); // Print an '='
else
Serial.print(' '); // print spaces otherwise
}
else // If our value is positive, just print spaces
Serial.print(' ');
}
Serial.print('|'); // Print a pipe (|) to represent the 0-point
// Do the positive half of the graph last:
for (int i=0; i<numBarsFull; i++)
{
if (value > 0)
{ // If our position in the graph is in the range we want to graph
if (i <= numBars)
Serial.print('='); // Print an '='
else
Serial.print(' '); // otherwise print spaces
}
else // If value is negative, just print spaces
Serial.print(' ');
}
// To end the line, print the actual value:
Serial.println(" (" + String(value, 2) + " g)");
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .