Hi,
I’m working on a prototype to prevent bad posture, and I’m using two MMA8452 accelerometers. However, I got some trouble with the calculated values when using the MMA8452-Accelerometer-Library-Spark-Core library. The not calculated values is working fine, or at least I get different values when moving the accelerometers, but when I change to the calculated values I only get the value 0 or 3.
Hope you guys can help me out…
Regards Mads Svendsen
My code looks like this: (It’s mainly the PublishVal function that’s interesting)
// This #include statement was automatically added by the Spark IDE.
#include "SparkTime/SparkTime.h"
// This #include statement was automatically added by the Spark IDE.
#include "MMA8452-Accelerometer-Library-Spark-Core/MMA8452-Accelerometer-Library-Spark-Core.h"
//******************************************************************************
MMA8452Q accel1(0x1D);
MMA8452Q accel2(0x1C);
int PublishTime;
int Posturevalue;
int x;
int y;
int z;
long Timer;
int ButtonPin = D4;
int VibratorPin = D5;
#define ONE_HOUR_MILLIS (60 * 60 * 1000)
#define POSTUREVAL_INTERVAL (5 * 1000)
unsigned long lastSync = millis();
void setup()
{
accel1.init(); //initialise accelerometer1
accel2.init(); //initialise accelerometer2
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(VibratorPin, OUTPUT);
Timer = millis();
}
void loop()
{
Vibrator();
PublishVal();
UpdateTime();
}
void Vibrator() {
if (digitalRead(ButtonPin) == LOW) {
digitalWrite(VibratorPin, HIGH);
}
else {
digitalWrite(VibratorPin, LOW);
}
}
void PublishVal() {
if (accel1.available() && accel2.available() && millis()-Timer >= POSTUREVAL_INTERVAL){
accel1.read();
accel2.read();
PublishTime = Time.now();
if (PublishTime != NULL) {
x = accel1.cx;
y = accel1.cy;
z = accel1.cz;
if (x != NULL && y != NULL && z != NULL) {
Spark.variable("x", &x, INT);
Spark.variable("y", &y, INT);
Spark.variable("z", &z, INT);
}
x = accel2.cx;
y = accel2.cy;
z = accel2.cz;
if (x != NULL && y != NULL && z != NULL) {
Spark.variable("x", &x, INT);
Spark.variable("y", &y, INT);
Spark.variable("z", &z, INT);
}
Spark.variable("Time", &PublishTime, INT);
}
Timer = millis();
}
}
void UpdateTime() {
if (millis() - lastSync > ONE_HOUR_MILLIS) {
// Request time synchronization from the Particle Cloud
Spark.syncTime();
lastSync = millis();
}
}