From 5df78294223cd8501e5248c340ac424fb709576a Mon Sep 17 00:00:00 2001 From: Flax Date: Thu, 25 Nov 2021 23:37:06 +0100 Subject: [PATCH] SW : added touch sensors management. --- sw/boitarire_sw/boitarire_sw.ino | 47 +++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/sw/boitarire_sw/boitarire_sw.ino b/sw/boitarire_sw/boitarire_sw.ino index a7c1f64..a62dd7e 100644 --- a/sw/boitarire_sw/boitarire_sw.ino +++ b/sw/boitarire_sw/boitarire_sw.ino @@ -19,12 +19,14 @@ // Defines - Macros - Constants #define SENSOR_INCLINATION #define SENSOR_ACCELEROMETER -#define SENSOR_TOUCHSENSOR1 -#define SENSOR_TOUCHSENSOR2 +#define SENSOR_TOUCH_SENSOR1 +#define SENSOR_TOUCH_SENSOR2 //#define MP3_RESET // Other constants #define PIN_SENSOR_POSITION 9 // Position sensor input +#define PIN_TOUCH_SENSOR1 8 // Touch sensor 1 input +#define PIN_TOUCH_SENSOR2 5 // Touch sensor 2 input #define PIN_GROUND_SWITCH 7 // MP3 module ground switch command output #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) @@ -56,7 +58,7 @@ String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete unsigned long debounce_delay; -bool input_now; +int input_position_sensor, input_touch_sensor1, input_touch_sensor2; unsigned long play_blank_delay, mp3_reset_delay; // State machines states @@ -77,6 +79,12 @@ void setup() { // Position sensor input pin - D9 - Input pull-up pinMode(PIN_SENSOR_POSITION, INPUT_PULLUP); + // Touch sensor 1 input pin - D8 - Input no pull + pinMode(PIN_TOUCH_SENSOR1, INPUT); + + // Touch sensor 2 input pin - D5 - Input no pull + pinMode(PIN_TOUCH_SENSOR2, INPUT); + // LED output for visualisation pinMode(LED_BUILTIN, OUTPUT); @@ -88,7 +96,9 @@ void setup() { stmState = STM_INIT; stmDebounceState = STM_DEBOUNCE_IDLE_0; debounce_delay = 0; - input_now = 0; + input_position_sensor = LOW; + input_touch_sensor1 = LOW; + input_touch_sensor2 = LOW; // Connect ground for MP3 module digitalWrite(PIN_GROUND_SWITCH, HIGH); @@ -116,9 +126,9 @@ void loop() { #ifdef SENSOR_INCLINATION // Read instantaneous value of position sensor - input_now = digitalRead(PIN_SENSOR_POSITION); + input_position_sensor = digitalRead(PIN_SENSOR_POSITION); #else - input_now = 0; + input_position_sensor = LOW; #endif // Inclination debounce state machine @@ -126,7 +136,7 @@ void loop() { switch (stmDebounceState) { case STM_DEBOUNCE_IDLE_0: - if (input_now == 1) + if (input_position_sensor == HIGH) { stmDebounceState = STM_DEBOUNCE_DETECT_1; debounce_delay = millis(); @@ -134,7 +144,7 @@ void loop() { break; case STM_DEBOUNCE_IDLE_1: - if (input_now == 0) + if (input_position_sensor == LOW) { stmDebounceState = STM_DEBOUNCE_DETECT_0; debounce_delay = millis(); @@ -142,7 +152,7 @@ void loop() { break; case STM_DEBOUNCE_DETECT_0: - if (input_now == 1) + if (input_position_sensor == HIGH) { stmDebounceState = STM_DEBOUNCE_IDLE_1; } @@ -153,7 +163,7 @@ void loop() { break; case STM_DEBOUNCE_DETECT_1: - if (input_now == 0) + if (input_position_sensor == LOW) { stmDebounceState = STM_DEBOUNCE_IDLE_0; } @@ -176,6 +186,18 @@ void loop() { default: break; } + +#ifdef SENSOR_TOUCH_SENSOR1 + input_touch_sensor1 = digitalRead(PIN_TOUCH_SENSOR1); +#else + input_touch_sensor1 = LOW; +#endif + +#ifdef SENSOR_TOUCH_SENSOR2 + input_touch_sensor2 = digitalRead(PIN_TOUCH_SENSOR2); +#else + input_touch_sensor2 = LOW; +#endif // Main state machine //------------------- @@ -190,6 +212,11 @@ void loop() { { stmState = STM_PLAY; } + + if ((input_touch_sensor1 == HIGH) | (input_touch_sensor2 == HIGH)) + { + stmState = STM_PLAY; + } break; case STM_PLAY: -- GitLab