diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp index a4ca986..e59f656 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp @@ -91,13 +91,12 @@ update the voltMeterConstant variable in pP_config.h with the correct value // Headers, variables, and functions #include "LightChrono.h" -#include "pP_pins.h" -#include -#include #include "pP_function.h" #include "pP_i2c.hpp" +#include "pP_pins.h" #include "pP_serial.h" #include "pP_volatile.h" +#include void setup() { // Setup PWM on voltage follower (PD3) @@ -146,13 +145,6 @@ void setup() { void loop() { if (mainLoop.hasPassed(LOOP_DUR)) { mainLoop.restart(); - // Blink LED's on init - if (BlinkCount > 0) { - BlinkState = !BlinkState; - digitalWriteFast(ERR_LED, BlinkState); - // digitalWriteFast(TRG_OUT, BlinkState); - --BlinkCount; - } // Get Serial Input serialInput(); @@ -188,25 +180,21 @@ void loop() { // Check that the piezo disk is properly connected pzConCheck(); - // Blink LED's on init - if (BlinkCount > 0) { - BlinkState = !BlinkState; - digitalWriteFast(ERR_LED, BlinkState); - // digitalWriteFast(TRG_OUT, BlinkState); - --BlinkCount; - // } else { - // Check for error state - // checkError(); - } else { - digitalWriteFast(ERR_LED, 0); - } - // Print state if debug is on if (Debug > 0) { serialPrintState(); } - // Sets trigger output state to false after completing loop - // digitalWriteFast(TRG_OUT, HIGH); - sensorHReading = 0; + } + + // Blink LED + if (blinkLoop.hasPassed(BLINK_DURATION) && BlinkCount > 0) { + blinkLoop.restart(); + BlinkCount--; + digitalWriteFast(ERR_LED, BlinkCount % 2); + } + + if (lastTriggerTimestamp > 0 && millis() - lastTriggerTimestamp > TRG_DUR) { + digitalWriteFast(TRG_OUT, !LOGIC); + lastTriggerTimestamp = 0; } } diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h index ccc8a94..eade657 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h @@ -58,6 +58,8 @@ extern uint8_t pP_i2c_address; #define PP_VERSION "2.3.2" #endif // PP_VERSION fallback if python script fails +#define BLINK_DURATION 100 + void eraseEEPROM(); void setDefaultConfig(); void restoreConfig(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp index 288efe1..8ed9dd1 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp @@ -38,12 +38,12 @@ void doubleFlash() { /*------------------------------------------------*/ void pulse() { - digitalWriteFast(TRG_OUT, LOGIC); - sensorHReading = 1; - delay(TRG_DUR); - digitalWriteFast(TRG_OUT, !LOGIC); - Serial.println("Trig'd!"); - doubleFlash(); + if (lastTriggerTimestamp == 0) { + digitalWriteFast(TRG_OUT, LOGIC); + Serial.println("Trig'd!"); + doubleFlash(); + lastTriggerTimestamp = millis(); + } } /*------------------------------------------------*/ diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index 03e478c..0dc07d3 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -1,6 +1,7 @@ #include "pP_cmd.h" #include "pP_volatile.h" #include "string.h" +#include "Arduino.h" void parseData() { diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp index cfd90ba..3262771 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp @@ -3,7 +3,6 @@ #include "stdint.h" // these variables will change on their own. Do not edit ANYTHING below this line -volatile int sensorHReading = 0; // variable to store the value read from the sensor pin volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment volatile int ADJ_COMP = 0; // Variable for Comparator adjustment volatile int ERR_STATE = 0; @@ -28,8 +27,7 @@ int VFol = 0; // Error blink parameters -int BlinkState = 0; -int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after +int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state // Serial Input Parsing Variables char inputBuffer[buffSize]; @@ -38,4 +36,7 @@ bool serialIncoming = false; char serialMessageIn[buffSize] = {0}; long serialLong = 0; -LightChrono mainLoop; \ No newline at end of file +long lastTriggerTimestamp = 0; + +LightChrono mainLoop; +LightChrono blinkLoop; \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h index c1659cf..f6efb62 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h @@ -5,9 +5,8 @@ #include "stdint.h" // these variables will change on their own. Do not edit ANYTHING below this line -extern volatile int sensorHReading; // variable to store the value read from the sensor pin -extern volatile int ADJ_FOLLOW; // Variable for Follower adjustment -extern volatile int ADJ_COMP; // Variable for Comparator adjustment +extern volatile int ADJ_FOLLOW; // Variable for Follower adjustment +extern volatile int ADJ_COMP; // Variable for Comparator adjustment extern volatile int ERR_STATE; extern volatile int PZ_STATE; @@ -30,8 +29,7 @@ extern int VFol; // Error blink parameters -extern int BlinkState; -extern int BlinkCount; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after +extern int BlinkCount; // Multiply Blink count by 2 to handle toggle state, subtract one to make sure light is on after // Serial Input Parsing Variables #define buffSize 40 @@ -42,10 +40,13 @@ extern bool serialIncoming; extern char serialMessageIn[buffSize]; extern long serialLong; +extern long lastTriggerTimestamp; + //#define LOW 0 //#define HIGH 1 // Task scheduler instances extern LightChrono mainLoop; +extern LightChrono blinkLoop; #endif // PP_VOLATILE_H \ No newline at end of file