#include <Audio.h>
#include <Wire.h>
//#include "arduinoFFT.h"
#include "audiostream.h"
//arduinoFFT FFT = arduinoFFT();
//GUItool: begin automatically generated code
//AudioSynthWaveformSine sine1,sine2,sine3; //xy=265.0000305175781,430.0000629425049
AudioSynthWaveform waveform1;
AudioMixer4 mixer1;
AudioOutputUSB usb1; //xy=454.0000114440918,462.00001430511475
//AudioConnection patchCord1(sine1, 0, mixer1, 0);
//AudioConnection patchCord2(sine2, 0, mixer1, 1);
AudioConnection patchCord1(waveform1, 0, usb1, 0);
//AudioConnection patchCord4(mixer1, 0, usb1, 0);
//extern const int16_t myWaveform[256];
AudioOutputI2S out1;
//GUItool: end automatically generated code
//************************//
//**************************adxl345 address*****************//
//Direccion del dispositivo
const int DEVICE_ADDRESS = (0x53); //Comprobado con el I2C Scanner
byte _buff[6];
//**************************ADXL345 registers********************//
char POWER_CTL = 0x2D;
char DATA_FORMAT = 0x31;
char DATA_RATE = 0X2C;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
uint8_t numBytesToRead = 6;
//**********alpha gravity**************//
-
-double alphaX = 0.80;
-double alphaY = 0.80;
-double alphaZ = 0.80;
-double gx,gy,gz;
-double acc_z_final;
-double acc_y_final;
-double acc_x_final;
+//
+//double alphaX = 0.80;
+//double alphaY = 0.80;
+//double alphaZ = 0.80;
+//double gx,gy,gz;
+//double acc_z_final;
+//double acc_y_final;
+//double acc_x_final;
+//unsigned long time_since_last_reset = 0;
+//int interval_one = 5000; //5 seconds.
+//int interval_two = 5000; //5 segundos.
+//double final_data[3];
+//short bufSize = 6;
+//bool message = true;
+
+float alphaX = 0.80;
+float alphaY = 0.80;
+float alphaZ = 0.80;
+float gx,gy,gz;
+float acc_z_final;
+float acc_y_final;
+float acc_x_final;
unsigned long time_since_last_reset = 0;
int interval_one = 5000; //2 seconds. Loop principal de muestreo (por defecto estaba a 20 o a 10)
int interval_two = 5000; //5 segundos. Loop secunedario de muestreo.
-double final_data[3];
+float final_data[3];
short bufSize = 6;
bool message = true;
//Filter
//Eje Z
-double EMA_a_Z = 0.93;
-double EMA_s_Z = 0;
-double highpass_Z = 0;
+//double EMA_a_Z = 0.93;
+//double EMA_s_Z = 0;
+//double highpass_Z = 0;
+
+float EMA_a_Z = 0.93;
+float EMA_s_Z = 0;
+float highpass_Z = 0;
//Eje Y
-double EMA_a_Y = 0.93;
-double EMA_s_Y = 0;
-double highpass_Y = 0;
+//double EMA_a_Y = 0.93;
+//double EMA_s_Y = 0;
+//double highpass_Y = 0;
+
+float EMA_a_Y = 0.93;
+float EMA_s_Y = 0;
+float highpass_Y = 0;
//Eje X
-double EMA_a_X = 0.93;
-double EMA_s_X = 0;
-double highpass_X = 0;
+//double EMA_a_X = 0.93;
+//double EMA_s_X = 0;
+//double highpass_X = 0;
+
+float EMA_a_X = 0.93;
+float EMA_s_X = 0;
+float highpass_X = 0;
void writeTo(int device, byte address, byte val) {
//
//G force without the EMA filter
-// gx = alphaX * gx + (1 - alphaX)*x;
-// gy = alphaY * gy + (1 - alphaY)*y;
-// gz = alphaZ * gz + (1 - alphaZ)*z;
+ gx = alphaX * gx + (1 - alphaX)*x;
+ gy = alphaY * gy + (1 - alphaY)*y;
+ gz = alphaZ * gz + (1 - alphaZ)*z;
//G force with the EMA filter
- gx = alphaX * gx + (1 - alphaX)*highpass_X;
- gy = alphaY * gy + (1 - alphaY)*highpass_Y;
- gz = alphaZ * gz + (1 - alphaZ)*highpass_Z;
+// gx = alphaX * gx + (1 - alphaX)*highpass_X;
+// gy = alphaY * gy + (1 - alphaY)*highpass_Y;
+// gz = alphaZ * gz + (1 - alphaZ)*highpass_Z;
//m/s2 output withouth EMA
-// acc_x_final = (x - gx) * 0.0039* 9.81;
-// acc_y_final = (y - gy) * 0.0039 *9.81;
-// acc_z_final = (z - gz) * 0.0039 *9.81;
+ acc_x_final = (x - gx) * 0.0039* 9.81;
+ acc_y_final = (y - gy) * 0.0039 *9.81;
+ acc_z_final = (z - gz) * 0.0039 *9.81;
//m/s2 output with EMA
- acc_z_final = (highpass_Z - gz) * 0.0039 *9.81;
- acc_x_final = (highpass_X - gx) * 0.0039* 9.81;
- acc_y_final = (highpass_Y - gy) * 0.0039 *9.81;
+// acc_z_final = (highpass_Z - gz) * 0.0039 *9.81;
+// acc_x_final = (highpass_X - gx) * 0.0039* 9.81;
+// acc_y_final = (highpass_Y - gy) * 0.0039 *9.81;
//
// Serial.print("\t");
// Serial.print(acc_y_final);
// Serial.print("\t");
-// Serial.println(acc_z_final);
+ Serial.println(acc_z_final);
+ delay(5);
// sine1.frequency(acc_x_final);
// sine1.amplitude(acc_x_final);
// sine2.frequency(acc_y_final);
// sine2.amplitude(acc_y_final);
- waveform1.frequency(acc_z_final);
- waveform1.amplitude(acc_z_final);
+// waveform1.frequency(acc_z_final);
+// waveform1.amplitude(acc_z_final);
// Serial.write(acc_z_final);
//
// sine1.frequency(0);
}
- time_since_last_reset = millis();
- while((millis() - time_since_last_reset) < interval_two){
+ //time_since_last_reset = millis();
+ //while((millis() - time_since_last_reset) < interval_two){
// if(message)
//{ message = false;
- waveform1.frequency(160);
- waveform1.amplitude(1.0);
+ //waveform1.frequency(160);
+ //waveform1.amplitude(1.0);
// delay(1);
//
////}
//time_since_last_reset = millis();
-}
+//}