From 7999321f8c729364b9f8a20c6c466a62519c9fa9 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Tue, 19 Feb 2019 01:07:42 -0800 Subject: [PATCH] Added basic serial input to modify parameters - Partially addresses https://github.com/pyr0ball/pyr0piezo/issues/1 - Partially addresses https://github.com/pyr0ball/pyr0piezo/issues/2 - Added serial input parsing for V_Follow, V_Comp, and Gain_Factor - Example serial input: VFOLH2.35 VFOLL1.80 VCOMPH2.75 VCOMPL2.54 GFACT2 --- .../Pyr0_Piezo_Sensor_v2.0.0.ino | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.0.0/Pyr0_Piezo_Sensor_v2.0.0.ino b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.0.0/Pyr0_Piezo_Sensor_v2.0.0.ino index cee2057..2eab41c 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.0.0/Pyr0_Piezo_Sensor_v2.0.0.ino +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.0.0/Pyr0_Piezo_Sensor_v2.0.0.ino @@ -20,6 +20,9 @@ Schematics for this project can be found here: https://github.com/pyr0ball/pyr0piezo/tree/master/docs/Schematics + For Arduino IDE use MCUdude MiniCore: https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json + + created 2/18/2019 by Alan "pyr0ball" Weinstock @@ -82,6 +85,11 @@ int diffAdjH = senseHighInt - VAdj; int BlinkState = LOW; int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state +// Serial Input Parsing Variables +String readString, substring; +int loc; + + void setup() { pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT pinMode(ERR_LED, OUTPUT); @@ -211,6 +219,128 @@ void checkError () { /*------------------------------------------------*/ +void serialInput() { + + //expect a string like VFOLH2.35 VFOLL1.80 VCOMPH2.75 VCOMPL2.54 GFACT2 + + if (Serial.available()) { + char c = Serial.read(); //gets one byte from serial buffer + if (c == '\n') { //looks for end of data packet marker + //if (c == ',') { + Serial.println(readString); //prints string to serial port out + + + //Analyze each input + loc = readString.indexOf("VFOLH"); + //Serial.println(loc); + if (loc != -1) { + substring = readString.substring(loc+5, loc+9); + if (isFloat(substring)) { + char carray[substring.length() + 1]; // determine size of the array + substring.toCharArray(carray, sizeof(carray)); // puts substring into an array + senseHighThrs = atof(carray); + } + Serial.print("VFollowHigh: "); + Serial.println(substring); + } else { + Serial.print("VFollowHigh: "); + Serial.println("NA"); + } + + + loc = readString.indexOf("VFOLL"); + //Serial.println(loc); + if (loc != -1) { + substring = readString.substring(loc+5, loc+9); + if (isFloat(substring)) { + char carray[substring.length() + 1]; // determine size of the array + substring.toCharArray(carray, sizeof(carray)); // puts substring into an array + senseLowThrs = atof(carray); + } + Serial.print("VFollowLow: "); + Serial.println(substring); + } else { + Serial.print("VFollowLow: "); + Serial.println("NA"); + } + + loc = readString.indexOf("VCOMPH"); + //Serial.println(loc); + if (loc != -1) { + substring = readString.substring(loc+6, loc+10); + if (isFloat(substring)) { + char carray[substring.length() + 1]; // determine size of the array + substring.toCharArray(carray, sizeof(carray)); // puts substring into an array + compHighThrs = atof(carray); + } + Serial.print("VCompHigh: "); + Serial.println(substring); + } else { + Serial.print("VCompHigh: "); + Serial.println("NA"); + } + + loc = readString.indexOf("VCOMPL"); + //Serial.println(loc); + if (loc != -1) { + substring = readString.substring(loc+6, loc+10); + if (isFloat(substring)) { + char carray[substring.length() + 1]; // determine size of the array + substring.toCharArray(carray, sizeof(carray)); // puts substring into an array + compLowThrs = atof(carray); + } + Serial.print("VCompLow: "); + Serial.println(substring); + } else { + Serial.print("VCompLow: "); + Serial.println("NA"); + } + + loc = readString.indexOf("GFACT"); + //Serial.println(loc); + if (loc != -1) { + substring = readString.substring(loc+5, loc+9); + char carray[substring.length() + 1]; // determine size of the array + substring.toCharArray(carray, sizeof(carray)); // puts substring into an array + GAIN_FACTOR = atof(carray); + Serial.print("GainFactor: "); + Serial.println(substring); + } else { + Serial.print("GainFactor: "); + Serial.println("NA"); + } + + readString=""; //clears variable for new input + substring=""; + + } + else { + readString += c; //makes the string readString + } + } + +} + +// Routine for checking if string is a valid number +boolean isFloat(String tString) { + String tBuf; + boolean decPt = false; + + if(tString.charAt(0) == '+' || tString.charAt(0) == '-') tBuf = &tString[1]; + else tBuf = tString; + + for(int x=0;x '9') return false; + } + return true; +} + + void loop() { // Blink LED's on init