diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino index 07803dc..8f45c97 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino @@ -34,8 +34,8 @@ To change trigger active duration: TRG_D [integer for milliseconds] To change gain factor: GAIN_F [integer for gain state - see note*] To change ADC hysteresis value: HYST [integer] -To change sensor input pullup vRef low threshold: VADJ [float value] -To change comparator trigger high threshold: VCOMP [float value] +To change sensor input pullup vRef low threshold: VFOL [integer in millivolts] +To change comparator trigger high threshold: VCOMP [integer in millivolts] These commands should be wrapped in this format: @@ -43,7 +43,7 @@ These commands should be wrapped in this format: Examples: <~ set gain factor to index 3 (6x) - <~ set the vref floor to 2.35V + <~ set the vref floor to 2.35V *Note for Gain Factor: The gain STATE is representative of these values: @@ -64,6 +64,7 @@ The gain STATE is representative of these values: //#define VERBOSE true // Headers, variables, and functions +include include #include "LightChrono.h" #include "pP_pins.h" @@ -104,6 +105,10 @@ void setup() { delay(2); // Wait for vref to settle Serial.println("Initializing Pyr0-Piezo Sensor..."); + + restoreConfig(); + + adjustGain(); } /*------------------------------------------------*/ @@ -123,35 +128,46 @@ void loop() { serialInput(); // Set any new parameters from serial input - updateParams(); + if (serialIncoming) { + updateParams(); + } // Set the amplification gain factor adjustGain(); // Check voltage of first and second stages and compare against thresholds - adjustVin(); + readVin(); VComp = analogRead(VCOMP_SENSE_PIN); VFol = analogRead(V_FOLLOW_PIN); // Voltage Follower adjustment + VLast = VOld - Vin; if (VLast > Hyst || VLast < -Hyst) { adjustFollow(); - } // Voltage Comparator adjustment - if (VLast > Hyst || VLast < -Hyst) { adjustComp(); + // Alert the user that auto-calibration is ongoing + ERR_STATE = 1; + } else { + ERR_STATE = 0; } - // Alert the user that auto-calibration is ongoing - calibrateAlert(); - - // Check for error state - checkError(); - - // Reply with status - serialReply(); + // Blink LED's on init + if (BlinkCount > 0) { + BlinkState = !BlinkState; + digitalWrite(ERR_LED, BlinkState); + digitalWrite(TRG_OUT, BlinkState); + --BlinkCount; + } else { + // Check for error state + checkError(); + } + // Print state if debug is on + if (Debug > 0) { + serialPrintState(); + } // Sets trigger output state to false after completing loop //digitalWrite(TRG_OUT, HIGH); sensorHReading = 0; diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_config.h index 74c5f78..deb3028 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_config.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_config.h @@ -1,41 +1,63 @@ +#ifndef PP_CONFIG_H +#define PP_CONFIG_H + // Configurable settings: +#define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x +#define GAIN_FACTOR_ADDRESS 0 #if !(defined(GAIN_FACTOR)) - int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x + extern int GAIN_FACTOR; #endif -#ifndef senseThrs - #define senseThrs 1450 +#define FOLLOWER_THRESHOLD_DEFAULT 1450 // Voltage follower default voltage in mV +#define FOLLOWER_THRESHOLD_ADDRESS 4 +#if !(defined(followerThrs)) + extern int followerThrs; #endif -#ifndef compThrs - #define compThrs 2850 +#define COMP_THRESHOLD_DEFAULT 2850 // Comparatore Vref default voltage in mV +#define COMP_THRESHOLD_ADDRESS 8 +#if !(defined(compThrs)) + extern int compThrs; #endif #ifndef InitCount - #define InitCount 6 // Number of times to blink the LED on start + #define InitCount 6 // Number of times to blink the LED on start #endif +#define LOOP_DUR_DEFAULT 50 // duration of time between ADC checks and other loop functions +#define LOOP_DUR_ADDRESS 12 #if !(defined(LOOP_DUR)) extern int LOOP_DUR; #endif +#define TRG_DUR_DEFAULT 20 // duration of the Z-axis pulse sent, in ms +#define TRG_DUR_ADDRESS 16 #if !(defined(TRG_DUR)) - int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms + extern int TRG_DUR; #endif +#define HYST_DEFAULT 20 +#define HYST_ADDRESS 20 #if !(defined(Hyst)) - int Hyst = 20; // Hysteresis value for ADC measurements + extern int Hyst; // Hysteresis value for ADC measurements +#endif + +#if !(defined(Debug)) + extern int Debug; #endif #if !(defined(voldMeterConstant)) - long voltMeterConstant = 1125300L; // For fine tuning input voltage sense + extern long voltMeterConstant = 1125300L; // For fine tuning input voltage sense #endif #ifdef I2C_INPUT #if !(defined(pP_i2c_address)) - byte pP_i2c_address = 0xa0; // I2C Bus Address + extern byte pP_i2c_address = 0xa0; // I2C Bus Address #endif #endif - +void restoreConfig(); +void resetConfig(); + +#endif \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_volatile.h index 0faf218..1ed74a9 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_volatile.h @@ -11,15 +11,15 @@ int VLast = 0; // Convert threshold values based on the input voltage long followerLong = followerThrs * 1023L; +long compLong = compThrs * 1023L; +long followerInt; long compInt; // Voltage Comparator Adjustment parameters int VComp = 0; // Voltage Follower Adjustment parameters -int VAdj = 0; -int diffAdjL = VAdj - senseInt; -int diffAdjH = senseInt - VAdj; +int VFol = 0; // Error blink parameters @@ -29,10 +29,8 @@ int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle // Serial Input Parsing Variables #define buffSize 40 char inputBuffer[buffSize]; -#define startMarker '<' -#define endMarker '>' +#define endMarker '\n' byte bytesRecvd = 0; -bool readInProgress = false; bool serialIncoming = false; char serialMessageIn[buffSize] = {0}; int serialInt = 0;