/**************************************************************************/
/*!
@file Adafruit_MMA8451.h
@author K. Townsend (Adafruit Industries)
@license BSD (see license.txt)
This is an example for the Adafruit MMA8451 Accel breakout board
----> https://www.adafruit.com/products/2019
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include "neopixel.h"
/*# define STATE_UNMUTE 0
# define STATE_MUTE 1*/
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D4
#define PIXEL_COUNT 1
#define PIXEL_TYPE SK6812RGBW
int state = 1;
float xData = 0;
int sampleWindow = 50;//sample every 50ms
int brightness = 0;
float rmult = 0.33;
float gmult = 0.84;
float bmult = 0.75;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
/*Timer timer(500, doUnmuteState);*/
Adafruit_MMA8451 mma = Adafruit_MMA8451();
int timeStart;
bool triggeredMute = true;
bool triggered = false;
float freq = 3000;
float val = 0;
void signOn(){
//Turn on the acceleraometer and check if it's receiving data
Serial.println("Adafruit MMA8451 test!");
if (! mma.begin()) {
Serial.println("Couldnt start");
while (1);
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G);
Serial.print("Range = "); Serial.print(2 << mma.getRange());
Serial.println("G");
}
int triggerFor = 5000;
int triggerFor2 = 10000;
int triggerFor3 = 3000;
int triggeredAt = -1;
int triggeredAt2 = -1;
int triggeredAt3 = -1;
int ftrigger = -1;
void setup() {
Serial.begin(9600);
signOn();//check that the accelerometer is getting data
//initialion the D7 light
delay(5000);
pinMode( D7 , OUTPUT);
pinMode( D4 , OUTPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
timeStart= millis();
}
void loop() {
run();
Serial.print("triggerMute = ");
Serial.print(triggeredMute);
Serial.print("\t");
Serial.print("trigger = ");
Serial.println(triggered);
Serial.print("triggeredAt = ");
Serial.print(triggeredAt);
Serial.print("\t");
Serial.print("triggeredAt2 = ");
Serial.print(triggeredAt2);
Serial.print("\t");
Serial.print("triggeredAt3 = ");
Serial.println(triggeredAt3);
/*Serial.println(xData);
Serial.println(triggered);*/
/*Serial.print(sampling());
Serial.print("\t");
Serial.print("Timer: ");
Serial.println(timer.isActive());*/
delay(5);
}
int checkX() {
// Read the 'raw' data in 14-bit counts
mma.read();
// Get a new sensor event
sensors_event_t event;
mma.getEvent(&event);
unsigned long startMillis = millis();
float highest_sample = 0;
float lowest_sample = 0;
float peakToPeak = 0;
const int sampleWindow = 50;
while (millis() - startMillis < sampleWindow)
{
xData = event.acceleration.x;
// invert the range, and convert it to a percent
if ( xData > highest_sample )
{
highest_sample = xData ;
}
if ( xData < lowest_sample ){
lowest_sample = xData;
}
}
if (highest_sample<1)
{
peakToPeak = highest_sample + lowest_sample;
}
else
{
peakToPeak = highest_sample - lowest_sample;
}
/*Serial.print("xData: ");
Serial.print(xData);
Serial.print("\t");
Serial.print("P2P: ");
Serial.println(peakToPeak);*/
return peakToPeak;
}
void run(){
checkX();
if(xData > 8.0 && xData < 11){
triggered = true;
if( triggeredAt == -1)
triggeredAt = millis();
}else{
Serial.println("NoHand");
triggered = false;
triggeredAt = -1;
}
if(triggered == false ){
if(triggeredMute == true){
triggeredAt2 = -1;
triggeredAt3 = -1;
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
constLed();
}else if(triggeredMute == false){
if( triggeredAt2 + triggerFor2 > millis() ){
triggeredAt3 = millis();
rmult = 0.5;
gmult = 0.35;
bmult = 0.65;
constLed();
}else if(triggeredAt3 + triggerFor3 < millis() ){
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
frequency();
breathe();
triggeredAt2 = -1;
if(freq < 500){
doMuteState();
ftrigger =-1;
}
}
}
}else if(triggered == true ){
if(triggeredMute == true){
rmult = 0.5;
gmult = 0.35;
bmult = 0.65;
frequency();
breathe();
if( triggeredAt + triggerFor < millis() ){
triggeredMute = false;
triggeredAt2 = millis();
doUnmuteState();
}else if(triggeredMute == false){
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
frequency();
breathe();
}
}
}
}
float frequency(){
if( ftrigger == -1){
ftrigger = millis();
}
freq = (-0.5*(millis() - ftrigger)) + 3000.0;
}
void doMuteState(){
Serial.println("Mute");
Particle.publish("diot2017/mute");
triggeredMute = true;
}
void doUnmuteState(){
Serial.println("Not Mute");
Particle.publish("diot2017/unmute");
/*delay(15000);*/
}
float breathe(){
float i = freq;
val = (exp(sin(millis()/i*M_PI)) - 0.36787944)*108.0;
Serial.print("i= ");
Serial.println(i);
uint32_t c = strip.Color(val*rmult, val*gmult, val*bmult, 0);
strip.setPixelColor(0, c);
strip.show();
}
float constLed(){
float val = 255;
uint32_t c = strip.Color(val*rmult, val*gmult, val*bmult, 0);
strip.setPixelColor(0, c);
strip.show();
ftrigger = -1;
}
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. .