Separated functions and definitions

- added header and cpp files
   - pin assignments
   - volatile variables
   - general functions
   - serial functions
   - i2c functions
This commit is contained in:
pyr0ball 2019-06-10 21:19:47 -07:00
parent b5883af901
commit 6567b50ddb
6 changed files with 397 additions and 377 deletions

View file

@ -56,8 +56,6 @@ The gain STATE is representative of these values:
4 = 11x 4 = 11x
*/ */
// Debug output toggle. Uncomment to enable // Debug output toggle. Uncomment to enable
#define DEBUG true #define DEBUG true
@ -65,9 +63,15 @@ The gain STATE is representative of these values:
//#define I2C true //#define I2C true
#ifdef I2C #ifdef I2C
#include <Wire.h> #include <Wire.h>
#include "pP_i2c.h"
#endif #endif
// Set variables for working parameters // Headers, variables, and functions
#include "pP_pins.h"
#include "pP_volatile.h"
#include "pP_functions.cpp"
// Configurable settings:
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
#define InitCount 6 // Number of times to blink the LED on start #define InitCount 6 // Number of times to blink the LED on start
int LOOP_DUR = 100; // duration of time between ADC checks and other loop functions int LOOP_DUR = 100; // duration of time between ADC checks and other loop functions
@ -78,381 +82,7 @@ int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms
int Hyst = 20; // Hysteresis value for ADC measurements int Hyst = 20; // Hysteresis value for ADC measurements
#define Vin 5 // input reference voltage #define Vin 5 // input reference voltage
// Analog Pin Assignments
#define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage
#define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage
// Digital Pin Assignments
#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7
//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin
//#define Z_TRG 0 // the piezo is connected to INT0 / digital pin 2
#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2
#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved
#define GADJ_R0 20 // Auto-adjust ladder pin assignments
#define GADJ_R1 21 // "
#define GADJ_R2 5 // "
#define GADJ_R3 6 // "
#define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment
#define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment
// 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;
// Convert float to integer for adjustment functions
int senseInt = (senseThrs / 5) * 1024; // Voltage Follower upper converted to adg interger
int compInt = (compThrs / 5) * 1024; // Upper threshold of Comparator before adjustment
// Voltage Comparator Adjustment parameters
//float VCompRef = 0.00; // variable to store the float value read from the comparator reference
int VComp = 0;
int diffCompL = VComp - compInt;
int diffCompH = compInt - VComp;
// Voltage Follower Adjustment parameters
//float vAdjRead = 0.00; // variable to store the value read from the follower
int VAdj = 0;
int diffAdjL = VAdj - senseInt;
int diffAdjH = senseInt - VAdj;
// Error blink parameters
int BlinkState = LOW;
int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state
// Serial Input Parsing Variables
#define buffSize 40
char inputBuffer[buffSize];
#define startMarker '<'
#define endMarker '>'
byte bytesRecvd = 0;
bool readInProgress = false;
bool serialIncoming = false;
char serialMessageIn[buffSize] = {0};
int serialInt = 0;
float serialFloat = 0.00;
void setup() {
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
pinMode(ERR_LED, OUTPUT);
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
pinMode(V_FOLLOW_PIN, INPUT);
pinMode(VCOMP_SENSE_PIN, INPUT);
pinMode(GADJ_R0, INPUT); // declare input to break pull to ground
pinMode(GADJ_R1, INPUT); // declare input to break pull to ground
pinMode(GADJ_R2, INPUT); // declare input to break pull to ground
pinMode(GADJ_R3, INPUT); // declare input to break pull to ground
Serial.begin(9600);
// Uncomment the following lines to use PCInt pins instead of hardware interrupt
//#include <PinChangeInterrupt.h>
//attachPCINT(digitalPinToPCINT(Z_TRG), pulse, FALLING);
// Uncomment the followoing line to use hardware interrupt pin
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
Serial.println("Initializing Pyr0-Piezo Sensor...");
}
/*------------------------------------------------*/
void pulse() {
digitalWrite(TRG_OUT, LOW);
sensorHReading = 1;
delay(TRG_DUR);
digitalWrite(TRG_OUT, HIGH);
}
/*------------------------------------------------*/
void adjustFollow() {
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
if (diffAdjL > 0.0) {
ADJ_FOLLOW += diffAdjL;
}
if (diffAdjH > 0.0) {
ADJ_FOLLOW -= diffAdjH;
}
// Analog output (PWM) of duty cycle
analogWrite(V_FOL_PWM, ADJ_FOLLOW);
}
/*------------------------------------------------*/
void adjustComp() {
if (diffCompL > 0.0) {
ADJ_COMP += diffCompL;
}
if (diffCompH > 0.0) {
ADJ_COMP -= diffCompH;
}
analogWrite(VCOMP_PWM, ADJ_COMP);
}
void calibrateAlert() {
if (diffAdjL > 0.0 || diffAdjH > 0.0 || diffCompL > 0.0 || diffCompH > 0.0) {
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain() {
if (GAIN_FACTOR == 0) {
pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 0) {
pinMode(GADJ_R3, OUTPUT);
digitalWrite(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 1) {
pinMode(GADJ_R2, OUTPUT);
digitalWrite(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 2) {
pinMode(GADJ_R1, OUTPUT);
digitalWrite(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 3) {
pinMode(GADJ_R0, OUTPUT);
digitalWrite(GADJ_R0, LOW);
ERR_STATE = 0;
}
}
/*------------------------------------------------*/
void checkError () {
if (ERR_STATE == 1) {
digitalWrite(ERR_LED, BlinkState);
BlinkState = !BlinkState;
}
else if (ERR_STATE == 0) {
BlinkState = LOW;
digitalWrite(ERR_LED, BlinkState);
}
}
/*------------------------------------------------*/
void serialInput() {
// receive data from Serial and save it into inputBuffer
if(Serial.available() > 0) {
// the order of these IF clauses is significant
identifyMarkers();
}
}
/*------------------------------------------------*/
#ifdef I2C
void i2cInput() {
// receive data from Serial and save it into inputBuffer
while(Wire.available()) {
identifyMarkers();
updateParams();
i2cReply();
}
}
#endif
/*------------------------------------------------*/
void identifyMarkers() {
char x = Serial.read();
// char y = Wire.read();
if (x == endMarker) {
readInProgress = false;
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
else if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
else if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
#ifdef I2C
if (y == endMarker) {
readInProgress = false;
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if(readInProgress) {
inputBuffer[bytesRecvd] = y;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (y == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
#endif
}
/*------------------------------------------------*/
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
serialInt = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
serialFloat = atof(strtokIndx); // convert this part to a float
}
/*------------------------------------------------*/
void updateParams() {
if (strcmp(serialMessageIn, "TRG_D") == 0) {
updateTrigDuration();
}
else if (strcmp(serialMessageIn, "GAIN_F") == 0) {
updateGainFactor();
}
else if (strcmp(serialMessageIn, "VCOMP") == 0) {
updateVComp();
}
else if (strcmp(serialMessageIn, "VADJ") == 0) {
updateVAdj();
}
}
/*------------------------------------------------*/
void updateTrigDuration() {
if (serialInt >= 0) {
TRG_DUR = serialInt;
}
}
/*------------------------------------------------*/
void updateGainFactor() {
if (serialInt >= 0) {
GAIN_FACTOR = serialInt;
}
}
/*------------------------------------------------*/
void updateVComp() {
if (serialInt >= 0) {
compInt = (serialFloat / 5) * 1024;
//senseInt = compInt; // syncing these params til #24 is fixed
}
}
/*------------------------------------------------*/
void updateVAdj() {
if (serialInt >= 0) {
senseInt = (serialFloat / 5) * 1024;
//compInt = senseInt; // syncing these params til #24 is fixed
}
}
/*------------------------------------------------*/
void updateHysteresis() {
if (serialInt >= 0) {
Hyst = serialInt;
}
}
/*------------------------------------------------*/
void serialReply() {
if (serialIncoming) {
serialIncoming = false;
#ifdef DEBUG
Serial.print("Comp Reference:");
Serial.print(VComp);
Serial.print(" ");
Serial.print("Comparator State:");
Serial.print(ADJ_COMP);
Serial.print(" ");
Serial.println(compInt);
Serial.print("Diff");
Serial.print(" ");
Serial.print(diffCompL);
Serial.print(" ");
Serial.println(diffCompH);
Serial.print("Amp Sense:");
Serial.print(VAdj);
Serial.print(" ");
Serial.print("Follower State:");
Serial.print(ADJ_FOLLOW);
Serial.print(" ");
Serial.println(senseInt);
Serial.print("Diff");
Serial.print(" ");
Serial.print(diffAdjL);
Serial.print(" ");
Serial.println(diffAdjH);
#endif
Serial.print("Delay:");
Serial.println(TRG_DUR);
Serial.print("Error State:");
Serial.println(ERR_STATE);
Serial.println("------------------");
}
}
/*------------------------------------------------*/
#ifdef I2C
void i2cReply() {
if (serialIncoming) {
Wire.write("OK");
}
}
#endif
/*------------------------------------------------*/ /*------------------------------------------------*/
void loop() { void loop() {

View file

@ -0,0 +1,242 @@
void setup() {
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
pinMode(ERR_LED, OUTPUT);
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
pinMode(V_FOLLOW_PIN, INPUT);
pinMode(VCOMP_SENSE_PIN, INPUT);
pinMode(GADJ_R0, INPUT); // declare input to break pull to ground
pinMode(GADJ_R1, INPUT); // declare input to break pull to ground
pinMode(GADJ_R2, INPUT); // declare input to break pull to ground
pinMode(GADJ_R3, INPUT); // declare input to break pull to ground
Serial.begin(9600);
// Uncomment the following lines to use PCInt pins instead of hardware interrupt
//#include <PinChangeInterrupt.h>
//attachPCINT(digitalPinToPCINT(Z_TRG), pulse, FALLING);
// Uncomment the followoing line to use hardware interrupt pin
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
Serial.println("Initializing Pyr0-Piezo Sensor...");
}
/*------------------------------------------------*/
void pulse() {
digitalWrite(TRG_OUT, LOW);
sensorHReading = 1;
delay(TRG_DUR);
digitalWrite(TRG_OUT, HIGH);
}
/*------------------------------------------------*/
void adjustFollow() {
/* Compares diffs of threshold vs read value
if positive, adjusts the follower to within
the range set above*/
if (diffAdjL > 0.0) {
ADJ_FOLLOW += diffAdjL;
}
if (diffAdjH > 0.0) {
ADJ_FOLLOW -= diffAdjH;
}
// Analog output (PWM) of duty cycle
analogWrite(V_FOL_PWM, ADJ_FOLLOW);
}
/*------------------------------------------------*/
void adjustComp() {
if (diffCompL > 0.0) {
ADJ_COMP += diffCompL;
}
if (diffCompH > 0.0) {
ADJ_COMP -= diffCompH;
}
analogWrite(VCOMP_PWM, ADJ_COMP);
}
void calibrateAlert() {
if (diffAdjL > 0.0 || diffAdjH > 0.0 || diffCompL > 0.0 || diffCompH > 0.0) {
ERR_STATE = 1;
}
}
/*------------------------------------------------*/
void adjustGain() {
if (GAIN_FACTOR == 0) {
pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 0) {
pinMode(GADJ_R3, OUTPUT);
digitalWrite(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 1) {
pinMode(GADJ_R2, OUTPUT);
digitalWrite(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 2) {
pinMode(GADJ_R1, OUTPUT);
digitalWrite(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT);
ERR_STATE = 0;
}
else if (GAIN_FACTOR > 3) {
pinMode(GADJ_R0, OUTPUT);
digitalWrite(GADJ_R0, LOW);
ERR_STATE = 0;
}
}
/*------------------------------------------------*/
void checkError () {
if (ERR_STATE == 1) {
digitalWrite(ERR_LED, BlinkState);
BlinkState = !BlinkState;
}
else if (ERR_STATE == 0) {
BlinkState = LOW;
digitalWrite(ERR_LED, BlinkState);
}
}
/*------------------------------------------------*/
void identifyMarkers() {
char x = Serial.read();
// char y = Wire.read();
if (x == endMarker) {
readInProgress = false;
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
else if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
else if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
#ifdef I2C
if (y == endMarker) {
readInProgress = false;
serialIncoming = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if(readInProgress) {
inputBuffer[bytesRecvd] = y;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (y == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
#endif
}
/*------------------------------------------------*/
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
serialInt = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
serialFloat = atof(strtokIndx); // convert this part to a float
}
/*------------------------------------------------*/
void updateParams() {
if (strcmp(serialMessageIn, "TRG_D") == 0) {
updateTrigDuration();
}
else if (strcmp(serialMessageIn, "GAIN_F") == 0) {
updateGainFactor();
}
else if (strcmp(serialMessageIn, "VCOMP") == 0) {
updateVComp();
}
else if (strcmp(serialMessageIn, "VADJ") == 0) {
updateVAdj();
}
}
/*------------------------------------------------*/
void updateTrigDuration() {
if (serialInt >= 0) {
TRG_DUR = serialInt;
}
}
/*------------------------------------------------*/
void updateGainFactor() {
if (serialInt >= 0) {
GAIN_FACTOR = serialInt;
}
}
/*------------------------------------------------*/
void updateVComp() {
if (serialInt >= 0) {
compInt = (serialFloat / 5) * 1024;
//senseInt = compInt; // syncing these params til #24 is fixed
}
}
/*------------------------------------------------*/
void updateVAdj() {
if (serialInt >= 0) {
senseInt = (serialFloat / 5) * 1024;
//compInt = senseInt; // syncing these params til #24 is fixed
}
}
/*------------------------------------------------*/
void updateHysteresis() {
if (serialInt >= 0) {
Hyst = serialInt;
}
}
/*------------------------------------------------*/

View file

@ -0,0 +1,23 @@
/*------------------------------------------------*/
#ifdef I2C
void i2cInput() {
// receive data from Serial and save it into inputBuffer
while(Wire.available()) {
identifyMarkers();
updateParams();
i2cReply();
}
}
#endif
/*------------------------------------------------*/
#ifdef I2C
void i2cReply() {
if (serialIncoming) {
Wire.write("OK");
}
}
#endif

View file

@ -0,0 +1,32 @@
/* pyr0-piezo pins configuration file
Default pins (based on Rev.2.x.xPCB layout)
* PD2 INT0 (Piezo In 'D2')
* D7 PCINT23 (Trigger OUT 'D7')
* PC0 ADC0 (Voltage Reference Check 'A0')
* PC1 ADC1 (Sensitivity Adjustment Check 'A1')
* PD4 PCINT20 (Error feedback LED 'D4')
* PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20')
* PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21')
* PD5 T1 (Voltage Adjustment Resistor 2 'D5')
* PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6')
* PB1 OC1A (Comparator VRef PWM Out 'D9')
* PD3 OC2B (Voltage Follower VRef PWM Out 'D3')
*/
// Analog Pin Assignments
#define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage
#define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage
// Digital Pin Assignments
#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7
//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin
#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2
#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved
#define GADJ_R0 20 // Auto-adjust ladder pin assignments
#define GADJ_R1 21 // "
#define GADJ_R2 5 // "
#define GADJ_R3 6 // "
#define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment
#define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment

View file

@ -0,0 +1,56 @@
/*------------------------------------------------*/
void serialInput() {
// receive data from Serial and save it into inputBuffer
if(Serial.available() > 0) {
// the order of these IF clauses is significant
identifyMarkers();
}
}
/*------------------------------------------------*/
void serialReply() {
if (serialIncoming) {
serialIncoming = false;
#ifdef DEBUG
Serial.print("Comp Reference:");
Serial.print(VComp);
Serial.print(" ");
Serial.print("Comparator State:");
Serial.print(ADJ_COMP);
Serial.print(" ");
Serial.println(compInt);
Serial.print("Diff");
Serial.print(" ");
Serial.print(diffCompL);
Serial.print(" ");
Serial.println(diffCompH);
Serial.print("Amp Sense:");
Serial.print(VAdj);
Serial.print(" ");
Serial.print("Follower State:");
Serial.print(ADJ_FOLLOW);
Serial.print(" ");
Serial.println(senseInt);
Serial.print("Diff");
Serial.print(" ");
Serial.print(diffAdjL);
Serial.print(" ");
Serial.println(diffAdjH);
#endif
Serial.print("Delay:");
Serial.println(TRG_DUR);
Serial.print("Error State:");
Serial.println(ERR_STATE);
Serial.println("------------------");
}
}

View file

@ -0,0 +1,37 @@
// 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;
// Convert float to integer for adjustment functions
int senseInt = (senseThrs / 5) * 1024; // Voltage Follower upper converted to adg interger
int compInt = (compThrs / 5) * 1024; // Upper threshold of Comparator before adjustment
// Voltage Comparator Adjustment parameters
//float VCompRef = 0.00; // variable to store the float value read from the comparator reference
int VComp = 0;
int diffCompL = VComp - compInt;
int diffCompH = compInt - VComp;
// Voltage Follower Adjustment parameters
//float vAdjRead = 0.00; // variable to store the value read from the follower
int VAdj = 0;
int diffAdjL = VAdj - senseInt;
int diffAdjH = senseInt - VAdj;
// Error blink parameters
int BlinkState = LOW;
int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state
// Serial Input Parsing Variables
#define buffSize 40
char inputBuffer[buffSize];
#define startMarker '<'
#define endMarker '>'
byte bytesRecvd = 0;
bool readInProgress = false;
bool serialIncoming = false;
char serialMessageIn[buffSize] = {0};
int serialInt = 0;
float serialFloat = 0.00;