diff --git a/sw/boitarire_sw/boitarire_sw.ino b/sw/boitarire_sw/boitarire_sw.ino index a62dd7ee6db34abc98ca0a3b186df0909ebc73ee..23b3389eac080e6c20d5f1f39eff54b573fbd9d4 100644 --- a/sw/boitarire_sw/boitarire_sw.ino +++ b/sw/boitarire_sw/boitarire_sw.ino @@ -16,12 +16,15 @@ https://code.electrolab.fr/Flax/boitarire */ +#include + // Defines - Macros - Constants #define SENSOR_INCLINATION #define SENSOR_ACCELEROMETER #define SENSOR_TOUCH_SENSOR1 #define SENSOR_TOUCH_SENSOR2 //#define MP3_RESET +//#define DEBUG_UART_ACCELEROMETER // Warning : conflict with MP3 player // Other constants #define PIN_SENSOR_POSITION 9 // Position sensor input @@ -31,6 +34,9 @@ #define PLAY_BLANK_DELAY 1000 // Blanking when playing a sound (ms) #define DELAY_MP3_RESET 100 // Time of ground cut of MP3 module for re-init (ms) #define DEBOUNCE_DELAY 200 // Position sensor input debounce (ms) +#define ACCEL_THRES_GYX 1000 +#define ACCEL_THRES_GYY 1000 +#define ACCEL_THRES_GYZ 1000 // Typedefs @@ -60,6 +66,11 @@ bool stringComplete = false; // whether the string is complete unsigned long debounce_delay; int input_position_sensor, input_touch_sensor1, input_touch_sensor2; unsigned long play_blank_delay, mp3_reset_delay; +unsigned long millis_temp; + +const int MPU=0x68; +int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; +bool accel_detect; // State machines states tStateMachine stmState; @@ -100,6 +111,28 @@ void setup() { input_touch_sensor1 = LOW; input_touch_sensor2 = LOW; +#ifdef SENSOR_ACCELEROMETER + // Accelerometer setup + Wire.begin(); + Wire.beginTransmission(MPU); + Wire.write(0x6B); + Wire.write(0); + Wire.endTransmission(true); + + // Set FIFO enable register + /* Wire.beginTransmission(MPU); + Wire.write(0x23); + Wire.write(0x78); + Wire.endTransmission(true);*/ + + // Set filters + Wire.beginTransmission(MPU); + Wire.write(0x1A); + Wire.write(0x06); + Wire.endTransmission(true); +#endif + accel_detect = false; + // Connect ground for MP3 module digitalWrite(PIN_GROUND_SWITCH, HIGH); } @@ -123,6 +156,37 @@ void loop() { inputString = ""; stringComplete = false; } + +#ifdef SENSOR_ACCELEROMETER + // Accelerometer communication + Wire.beginTransmission(MPU); + Wire.write(0x3B); + Wire.endTransmission(false); + Wire.requestFrom(MPU,14,true); + AcX=Wire.read()<<8|Wire.read(); + AcY=Wire.read()<<8|Wire.read(); + AcZ=Wire.read()<<8|Wire.read(); + Tmp=Wire.read()<<8|Wire.read(); + GyX=Wire.read()<<8|Wire.read(); + GyY=Wire.read()<<8|Wire.read(); + GyZ=Wire.read()<<8|Wire.read(); + + if ((GyX > ACCEL_THRES_GYX) || + (GyY > ACCEL_THRES_GYY) || + (GyZ > ACCEL_THRES_GYZ) || + (GyX < ((int16_t)(-1) * ACCEL_THRES_GYX)) || + (GyY < ((int16_t)(-1) * ACCEL_THRES_GYY)) || + (GyZ < ((int16_t)(-1) * ACCEL_THRES_GYZ))) + { + accel_detect = true; + } + else + { + accel_detect = false; + } +#else + accel_detect = false; +#endif #ifdef SENSOR_INCLINATION // Read instantaneous value of position sensor @@ -152,25 +216,35 @@ void loop() { break; case STM_DEBOUNCE_DETECT_0: + millis_temp = millis(); if (input_position_sensor == HIGH) { stmDebounceState = STM_DEBOUNCE_IDLE_1; } - else if ((millis() - debounce_delay) > DEBOUNCE_DELAY) + else if ((millis_temp >= debounce_delay) && ((millis_temp - debounce_delay) > DEBOUNCE_DELAY)) { stmDebounceState = STM_DEBOUNCE_DEBOUNCE_0; } + else if (millis_temp < debounce_delay) + { // Overflow protection + debounce_delay = 0; + } break; case STM_DEBOUNCE_DETECT_1: + millis_temp = millis(); if (input_position_sensor == LOW) { stmDebounceState = STM_DEBOUNCE_IDLE_0; } - else if ((millis() - debounce_delay) > DEBOUNCE_DELAY) + else if ((millis_temp >= debounce_delay) && ((millis_temp - debounce_delay) > DEBOUNCE_DELAY)) { stmDebounceState = STM_DEBOUNCE_DEBOUNCE_1; } + else if (millis_temp < debounce_delay) + { // Overflow protection + debounce_delay = 0; + } break; case STM_DEBOUNCE_DEBOUNCE_0: @@ -208,12 +282,10 @@ void loop() { break; case STM_IDLE: - if (stmDebounceState == STM_DEBOUNCE_DEBOUNCE_1) - { - stmState = STM_PLAY; - } - - if ((input_touch_sensor1 == HIGH) | (input_touch_sensor2 == HIGH)) + if ((stmDebounceState == STM_DEBOUNCE_DEBOUNCE_1) || + (input_touch_sensor1 == HIGH) || + (input_touch_sensor2 == HIGH) || + (accel_detect == true)) { stmState = STM_PLAY; } @@ -226,7 +298,8 @@ void loop() { break; case STM_BLANK: - if ((millis() - play_blank_delay) > PLAY_BLANK_DELAY) + millis_temp = millis(); + if ((millis_temp >= play_blank_delay) && ((millis_temp - play_blank_delay) > PLAY_BLANK_DELAY)) { #ifdef MP3_RESET stmState = STM_MP3_RESET; @@ -236,14 +309,23 @@ void loop() { stmState = STM_IDLE; #endif } + else if (millis_temp < play_blank_delay) + { // Overflow protection + play_blank_delay = 0; + } break; case STM_MP3_RESET: - if ((millis() - mp3_reset_delay) > DELAY_MP3_RESET) + millis_temp = millis(); + if ((millis_temp >= mp3_reset_delay) && ((millis_temp - mp3_reset_delay) > DELAY_MP3_RESET)) { stmState = STM_IDLE; digitalWrite(PIN_GROUND_SWITCH, HIGH); } + else if (millis_temp < mp3_reset_delay) + { // Overflow protection + mp3_reset_delay = 0; + } break; default: @@ -251,6 +333,20 @@ void loop() { break; } +#ifdef DEBUG_UART_ACCELEROMETER + Serial.print("Accelerometer: "); + Serial.print("X = "); Serial.print(AcX); + Serial.print(" | Y = "); Serial.print(AcY); + Serial.print(" | Z = "); Serial.println(AcZ); + + Serial.print("Gyroscope: "); + Serial.print("X = "); Serial.print(GyX); + Serial.print(" | Y = "); Serial.print(GyY); + Serial.print(" | Z = "); Serial.println(GyZ); + + Serial.print("Temperature: "); Serial.print(Tmp); + Serial.println(" "); +#endif } /*