Converting to defines and else ifs

Defines save flash and progmem and elseifs increase execution speed on code repeated on every loop.

Side note, something funky happened with the spacing.  Fixed it, but it's slightly different than what it was before.
This commit is contained in:
Foodbandlt 2019-04-22 03:25:52 -04:00
parent 5ff215c0da
commit e16c35cd32

View file

@ -60,7 +60,7 @@ The gain STATE is representative of these values:
// Set variables for working parameters // Set variables for working parameters
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
int InitCount = 6; // Number of times to blink the LED on start #define InitCount 6 // Number of times to blink the LED on start
int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms
#define senseThrs 2.15 #define senseThrs 2.15
//float senseHighThrs = 2.35; // Upper threshold of Voltage Follower before adjustment //float senseHighThrs = 2.35; // Upper threshold of Voltage Follower before adjustment
@ -68,25 +68,25 @@ int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms
#define compThrs 2.75 #define compThrs 2.75
//float compHighThrs = 2.75; // Upper threshold of Comparator before adjustment //float compHighThrs = 2.75; // Upper threshold of Comparator before adjustment
//float compLowThrs = 2.54; // Lower threshold of Comparator before adjustment //float compLowThrs = 2.54; // Lower threshold of Comparator before adjustment
int Hyst = 20; // Hysteresis value for ADC measurements #define Hyst 20 // Hysteresis value for ADC measurements
int Vin = 5; // input reference voltage #define Vin 5 // input reference voltage
// Analog Pin Assignments // Analog Pin Assignments
int V_FOLLOW_PIN = A0; // Sense pin to check Voltage Follower stage #define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage
int VCOMP_SENSE_PIN = A1; // Sense pin to check comparator stage voltage #define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage
// Digital Pin Assignments // Digital Pin Assignments
const int TRG_OUT = 7; // LED and Z-Min trigger output connected to digital pin 7 #define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7
//const int TRG_OUT = 13; // For testing on Atmega328/2560, Output is moved to onboard LED pin //#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 0 // the piezo is connected to INT0 / digital pin 2
const byte Z_TRG = 2; // the piezo is connected to INT0 / digital pin 2 #define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2
int ERR_LED = 4; // LED will blink if optimal voltage range cannot be achieved #define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved
const int GADJ_R0 = 20; // Auto-adjust ladder pin assignments #define GADJ_R0 20 // Auto-adjust ladder pin assignments
const int GADJ_R1 = 21; // " #define GADJ_R1 21 // "
const int GADJ_R2 = 5; // " #define GADJ_R2 5 // "
const int GADJ_R3 = 6; // " #define GADJ_R3 6 // "
int V_FOL_PWM = 3; // PWM analog output pin for voltage follower adjustment #define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment
int VCOMP_PWM = 9; // PWM analog output pin for comparator 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 // 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 sensorHReading = 0; // variable to store the value read from the sensor pin
@ -124,10 +124,10 @@ int BlinkState = LOW;
int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state
// Serial Input Parsing Variables // Serial Input Parsing Variables
const byte buffSize = 40; #define buffSize 40
char inputBuffer[buffSize]; char inputBuffer[buffSize];
const char startMarker = '<'; #define startMarker '<'
const char endMarker = '>'; #define endMarker '>'
byte bytesRecvd = 0; byte bytesRecvd = 0;
bool readInProgress = false; bool readInProgress = false;
bool serialIncoming = false; bool serialIncoming = false;
@ -172,10 +172,10 @@ void adjustFollow() {
if positive, adjusts the follower to within if positive, adjusts the follower to within
the range set above*/ the range set above*/
if (diffAdjL > 0.0) { if (diffAdjL > 0.0) {
ADJ_FOLLOW += (diffAdjL / 4) ; ADJ_FOLLOW += (diffAdjL / 4) ;
} }
if (diffAdjH > 0.0) { if (diffAdjH > 0.0) {
ADJ_FOLLOW -= (diffAdjH / 4); ADJ_FOLLOW -= (diffAdjH / 4);
} }
// Analog output (PWM) of duty cycle // Analog output (PWM) of duty cycle
@ -186,11 +186,11 @@ void adjustFollow() {
void adjustComp() { void adjustComp() {
if (diffCompL > 0.0) { if (diffCompL > 0.0) {
ADJ_COMP += (diffCompL / 4); ADJ_COMP += (diffCompL / 4);
} }
if (diffCompH > 0.0) { if (diffCompH > 0.0) {
ADJ_COMP -= (diffCompH / 4); ADJ_COMP -= (diffCompH / 4);
} }
analogWrite(VCOMP_PWM, ADJ_COMP); analogWrite(VCOMP_PWM, ADJ_COMP);
@ -200,44 +200,41 @@ void adjustComp() {
void adjustGain() { void adjustGain() {
if (GAIN_FACTOR < 0) { if (GAIN_FACTOR < 0 || GAIN_FACTOR > 4) {
ERR_STATE = 1; ERR_STATE = 1;
} }
if (GAIN_FACTOR == 0) { else if (GAIN_FACTOR == 0) {
pinMode(GADJ_R3, INPUT); pinMode(GADJ_R3, INPUT);
pinMode(GADJ_R2, INPUT); pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT); pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT); pinMode(GADJ_R0, INPUT);
ERR_STATE = 0; ERR_STATE = 0;
} }
if (GAIN_FACTOR > 0) { else if (GAIN_FACTOR > 0) {
pinMode(GADJ_R3, OUTPUT); pinMode(GADJ_R3, OUTPUT);
digitalWrite(GADJ_R3, LOW); digitalWrite(GADJ_R3, LOW);
pinMode(GADJ_R2, INPUT); pinMode(GADJ_R2, INPUT);
pinMode(GADJ_R1, INPUT); pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT); pinMode(GADJ_R0, INPUT);
ERR_STATE = 0; ERR_STATE = 0;
} }
if (GAIN_FACTOR > 1) { else if (GAIN_FACTOR > 1) {
pinMode(GADJ_R2, OUTPUT); pinMode(GADJ_R2, OUTPUT);
digitalWrite(GADJ_R2, LOW); digitalWrite(GADJ_R2, LOW);
pinMode(GADJ_R1, INPUT); pinMode(GADJ_R1, INPUT);
pinMode(GADJ_R0, INPUT); pinMode(GADJ_R0, INPUT);
ERR_STATE = 0; ERR_STATE = 0;
} }
if (GAIN_FACTOR > 2) { else if (GAIN_FACTOR > 2) {
pinMode(GADJ_R1, OUTPUT); pinMode(GADJ_R1, OUTPUT);
digitalWrite(GADJ_R1, LOW); digitalWrite(GADJ_R1, LOW);
pinMode(GADJ_R0, INPUT); pinMode(GADJ_R0, INPUT);
ERR_STATE = 0; ERR_STATE = 0;
} }
if (GAIN_FACTOR > 3) { else if (GAIN_FACTOR > 3) {
pinMode(GADJ_R0, OUTPUT); pinMode(GADJ_R0, OUTPUT);
digitalWrite(GADJ_R0, LOW); digitalWrite(GADJ_R0, LOW);
ERR_STATE = 0; ERR_STATE = 0;
}
if (GAIN_FACTOR > 4) {
ERR_STATE = 1;
} }
} }
@ -245,12 +242,12 @@ void adjustGain() {
void checkError () { void checkError () {
if (ERR_STATE == 1) { if (ERR_STATE == 1) {
digitalWrite(ERR_LED, BlinkState); digitalWrite(ERR_LED, BlinkState);
BlinkState = !BlinkState; BlinkState = !BlinkState;
} }
if (ERR_STATE == 0) { else if (ERR_STATE == 0) {
BlinkState = LOW; BlinkState = LOW;
digitalWrite(ERR_LED, BlinkState); digitalWrite(ERR_LED, BlinkState);
} }
} }
@ -258,12 +255,12 @@ void checkError () {
void serialInput() { void serialInput() {
// receive data from Serial and save it into inputBuffer // receive data from Serial and save it into inputBuffer
if(Serial.available() > 0) { if(Serial.available() > 0) {
// the order of these IF clauses is significant // the order of these IF clauses is significant
identifyMarkers(); identifyMarkers();
} }
} }
@ -272,61 +269,61 @@ void serialInput() {
/* void i2cInput() { /* void i2cInput() {
// receive data from Serial and save it into inputBuffer // receive data from Serial and save it into inputBuffer
while(Wire.available()) { while(Wire.available()) {
identifyMarkers(); identifyMarkers();
updateParams(); updateParams();
i2cReply(); i2cReply();
} }
} }
*/ */
/*------------------------------------------------*/ /*------------------------------------------------*/
void identifyMarkers() { void identifyMarkers() {
char x = Serial.read(); char x = Serial.read();
// char y = Wire.read(); // char y = Wire.read();
if (x == endMarker) { if (x == endMarker) {
readInProgress = false; readInProgress = false;
serialIncoming = true; serialIncoming = true;
inputBuffer[bytesRecvd] = 0; inputBuffer[bytesRecvd] = 0;
parseData(); parseData();
} }
if(readInProgress) { else if(readInProgress) {
inputBuffer[bytesRecvd] = x; inputBuffer[bytesRecvd] = x;
bytesRecvd ++; bytesRecvd ++;
if (bytesRecvd == buffSize) { if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1; bytesRecvd = buffSize - 1;
} }
} }
if (x == startMarker) { else if (x == startMarker) {
bytesRecvd = 0; bytesRecvd = 0;
readInProgress = true; readInProgress = true;
} }
/* if (y == endMarker) { /* if (y == endMarker) {
readInProgress = false; readInProgress = false;
serialIncoming = true; serialIncoming = true;
inputBuffer[bytesRecvd] = 0; inputBuffer[bytesRecvd] = 0;
parseData(); parseData();
} }
if(readInProgress) { if(readInProgress) {
inputBuffer[bytesRecvd] = y; inputBuffer[bytesRecvd] = y;
bytesRecvd ++; bytesRecvd ++;
if (bytesRecvd == buffSize) { if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1; bytesRecvd = buffSize - 1;
} }
} }
if (y == startMarker) { if (y == startMarker) {
bytesRecvd = 0; bytesRecvd = 0;
readInProgress = true; readInProgress = true;
} }
*/ */
} }
@ -334,8 +331,8 @@ void identifyMarkers() {
void parseData() { void parseData() {
// split the data into its parts // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,","); // get the first part - the string strtokIndx = strtok(inputBuffer,","); // get the first part - the string
@ -352,111 +349,111 @@ void parseData() {
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateParams() { void updateParams() {
if (strcmp(serialMessageIn, "TRG_D") == 0) { if (strcmp(serialMessageIn, "TRG_D") == 0) {
updateTrigDuration(); updateTrigDuration();
} }
if (strcmp(serialMessageIn, "GAIN_F") == 0) { else if (strcmp(serialMessageIn, "GAIN_F") == 0) {
updateGainFactor(); updateGainFactor();
} }
if (strcmp(serialMessageIn, "VCOMP") == 0) { else if (strcmp(serialMessageIn, "VCOMP") == 0) {
updateVComp(); updateVComp();
} }
//if (strcmp(serialMessageIn, "VCOMPH") == 0) { //else if (strcmp(serialMessageIn, "VCOMPH") == 0) {
// updateVCompH(); // updateVCompH();
//} //}
//if (strcmp(serialMessageIn, "VCOMPL") == 0) { //else if (strcmp(serialMessageIn, "VCOMPL") == 0) {
// updateVCompL(); // updateVCompL();
//} //}
if (strcmp(serialMessageIn, "VADJ") == 0) { else if (strcmp(serialMessageIn, "VADJ") == 0) {
updateVAdj(); updateVAdj();
} }
//if (strcmp(serialMessageIn, "VADJH") == 0) { //else if (strcmp(serialMessageIn, "VADJH") == 0) {
// updateVAdjH(); // updateVAdjH();
//} //}
//if (strcmp(serialMessageIn, "VADJL") == 0) { //else if (strcmp(serialMessageIn, "VADJL") == 0) {
// updateVAdjL(); // updateVAdjL();
//} //}
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateTrigDuration() { void updateTrigDuration() {
if (serialInt >= 0) { if (serialInt >= 0) {
TRG_DUR = serialInt; TRG_DUR = serialInt;
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateGainFactor() { void updateGainFactor() {
if (serialInt >= 0) { if (serialInt >= 0) {
GAIN_FACTOR = serialInt; GAIN_FACTOR = serialInt;
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateVComp() { void updateVComp() {
if (serialInt >= 0) { if (serialInt >= 0) {
compInt = (serialFloat / 5) * 1024; compInt = (serialFloat / 5) * 1024;
} }
} }
/*------------------------------------------------* /*------------------------------------------------*
void updateVCompH() { void updateVCompH() {
if (serialInt >= 0) { if (serialInt >= 0) {
compHighThrs = ((float)serialFloat); compHighThrs = ((float)serialFloat);
} }
} }
*------------------------------------------------* *------------------------------------------------*
void updateVCompL() { void updateVCompL() {
if (serialInt >= 0) { if (serialInt >= 0) {
compLowThrs = ((float)serialFloat); compLowThrs = ((float)serialFloat);
} }
} }
*------------------------------------------------*/ *------------------------------------------------*/
void updateVAdj() { void updateVAdj() {
if (serialInt >= 0) { if (serialInt >= 0) {
senseInt = (serialFloat / 5) * 1024; senseInt = (serialFloat / 5) * 1024;
} }
} }
/*------------------------------------------------* /*------------------------------------------------*
void updateVAdjH() { void updateVAdjH() {
if (serialInt >= 0) { if (serialInt >= 0) {
senseHighThrs = ((float)serialFloat); senseHighThrs = ((float)serialFloat);
} }
} }
*------------------------------------------------* *------------------------------------------------*
void updateVAdjL() { void updateVAdjL() {
if (serialInt >= 0) { if (serialInt >= 0) {
senseLowThrs = ((float)serialFloat); senseLowThrs = ((float)serialFloat);
} }
} }
*------------------------------------------------*/ *------------------------------------------------*/
void serialReply() { void serialReply() {
if (serialIncoming) { if (serialIncoming) {
serialIncoming = false; serialIncoming = false;
Serial.print("PZ Status:"); Serial.print("PZ Status:");
Serial.println(sensorHReading); Serial.println(sensorHReading);
Serial.print("Voltage Reference:"); Serial.print("Voltage Reference:");
Serial.print(VComp); Serial.print(VComp);
Serial.print(" "); Serial.print(" ");
//Serial.println(VCompRef,2); //Serial.println(VCompRef,2);
Serial.print("Amp Sense:"); Serial.print("Amp Sense:");
Serial.print(VAdj); Serial.print(VAdj);
Serial.print(" "); Serial.print(" ");
//Serial.println(vAdjRead,2); //Serial.println(vAdjRead,2);
Serial.print("Comparator State:"); Serial.print("Comparator State:");
Serial.println(ADJ_COMP); Serial.println(ADJ_COMP);
Serial.print("Follower State:"); Serial.print("Follower State:");
Serial.println(ADJ_FOLLOW); Serial.println(ADJ_FOLLOW);
Serial.print("Delay:"); Serial.print("Delay:");
Serial.println(TRG_DUR); Serial.println(TRG_DUR);
Serial.print("Error State:"); Serial.print("Error State:");
Serial.println(ERR_STATE); Serial.println(ERR_STATE);
Serial.println("------------------"); Serial.println("------------------");
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
@ -473,11 +470,11 @@ void loop() {
// Blink LED's on init // Blink LED's on init
if (BlinkCount > 0) { if (BlinkCount > 0) {
BlinkState = !BlinkState; BlinkState = !BlinkState;
digitalWrite(ERR_LED, BlinkState); digitalWrite(ERR_LED, BlinkState);
digitalWrite(TRG_OUT, BlinkState); digitalWrite(TRG_OUT, BlinkState);
delay(TRG_DUR); delay(TRG_DUR);
--BlinkCount; --BlinkCount;
} }
// Get Serial Input // Get Serial Input