Housekeeping
This commit is contained in:
		| @@ -111,8 +111,8 @@ bool XGZP::read(float * reading) { | |||||||
| 			if (count == 3) { // got data | 			if (count == 3) { // got data | ||||||
| 				XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read(); | 				XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read(); | ||||||
| 				XGZPC_Value = XGZPC_Value / 8.0; | 				XGZPC_Value = XGZPC_Value / 8.0; | ||||||
| 				Serial.print("Pa Value: "); | 				//Serial.print("Pa Value: "); | ||||||
| 				Serial.println(XGZPC_Value); | 				//Serial.println(XGZPC_Value); | ||||||
|  |  | ||||||
| 				XGZPC_Value = XGZPC_Value / 6895.0; | 				XGZPC_Value = XGZPC_Value / 6895.0; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,8 +7,8 @@ | |||||||
| #define SOLENOID_INFLATE 1 | #define SOLENOID_INFLATE 1 | ||||||
| #define SOLENOID_DEFLATE 2 | #define SOLENOID_DEFLATE 2 | ||||||
|  |  | ||||||
| #define RELAY1_PIN 7 | #define RELAY1_PIN 11 | ||||||
| #define RELAY2_PIN 11 | #define RELAY2_PIN 7 | ||||||
|  |  | ||||||
| #define PRESSURE_SENSOR_PIN A0 | #define PRESSURE_SENSOR_PIN A0 | ||||||
|  |  | ||||||
|   | |||||||
| @@ -36,7 +36,7 @@ enum buttonStates { | |||||||
| 	NUM_BUTTONSTATES | 	NUM_BUTTONSTATES | ||||||
| }; | }; | ||||||
|  |  | ||||||
| enum screenStates { | enum machineStates { | ||||||
| 	BOOT_UP, | 	BOOT_UP, | ||||||
| 	PRESSURE, | 	PRESSURE, | ||||||
| 	SET_POINT, | 	SET_POINT, | ||||||
| @@ -71,7 +71,7 @@ enum buttonStates upButton = OPEN; | |||||||
| enum buttonStates enterButton = OPEN; | enum buttonStates enterButton = OPEN; | ||||||
| enum buttonStates downButton = OPEN; | enum buttonStates downButton = OPEN; | ||||||
|  |  | ||||||
| enum screenStates screenState = BOOT_UP; | enum machineStates machineState = BOOT_UP; | ||||||
| double pressureValue = 0.0; | double pressureValue = 0.0; | ||||||
| int pressureSetPoint = 0; | int pressureSetPoint = 0; | ||||||
| double initialPressure = 0.0; | double initialPressure = 0.0; | ||||||
| @@ -108,9 +108,9 @@ void setup() | |||||||
|  |  | ||||||
| #ifdef SIMULATE | #ifdef SIMULATE | ||||||
| 	initSimulation(); | 	initSimulation(); | ||||||
| #endif | #else | ||||||
|  |  | ||||||
| 	initHardware(); | 	initHardware(); | ||||||
|  | #endif | ||||||
|  |  | ||||||
| 	resamplePressure(pressureValue); | 	resamplePressure(pressureValue); | ||||||
| } | } | ||||||
| @@ -118,7 +118,7 @@ void setup() | |||||||
| void loop() { | void loop() { | ||||||
| 	measurePressure(pressureValue); | 	measurePressure(pressureValue); | ||||||
| 	pollButtons(); | 	pollButtons(); | ||||||
| 	runUI(); | 	runStateMachine(); | ||||||
| 	logData(); | 	logData(); | ||||||
|  |  | ||||||
| #ifdef SIMULATE | #ifdef SIMULATE | ||||||
| @@ -130,7 +130,9 @@ void logData() { | |||||||
| 	static unsigned long lastLogTime = millis(); | 	static unsigned long lastLogTime = millis(); | ||||||
|  |  | ||||||
| 	if (millis() > lastLogTime + 100) { | 	if (millis() > lastLogTime + 100) { | ||||||
| 		Serial.print("pressure: "); | 		Serial.print("millis: "); | ||||||
|  | 		Serial.print(millis()); | ||||||
|  | 		Serial.print(", pressure: "); | ||||||
| 		Serial.print(pressureValue); | 		Serial.print(pressureValue); | ||||||
| #ifdef SIMULATE | #ifdef SIMULATE | ||||||
| 		Serial.print(", simulated: "); | 		Serial.print(", simulated: "); | ||||||
| @@ -143,15 +145,15 @@ void logData() { | |||||||
| 		Serial.print(", setpoint: "); | 		Serial.print(", setpoint: "); | ||||||
| 		Serial.print(pressureSetPoint); | 		Serial.print(pressureSetPoint); | ||||||
| 		Serial.print(", state: "); | 		Serial.print(", state: "); | ||||||
| 		Serial.print(stateLabels[screenState]); | 		Serial.print(stateLabels[machineState]); | ||||||
| 		Serial.println(""); | 		Serial.println(""); | ||||||
|  |  | ||||||
| 		lastLogTime = millis(); | 		lastLogTime = millis(); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void runUI() { | void runStateMachine() { | ||||||
| 	static enum screenStates nextState = BOOT_UP; | 	static enum machineStates nextState = BOOT_UP; | ||||||
|  |  | ||||||
| 	static unsigned long timer = millis(); | 	static unsigned long timer = millis(); | ||||||
|  |  | ||||||
| @@ -165,10 +167,10 @@ void runUI() { | |||||||
|  |  | ||||||
| 	oled.clearDisplay(); | 	oled.clearDisplay(); | ||||||
|  |  | ||||||
| 	switch (screenState) { | 	switch (machineState) { | ||||||
| 		case BOOT_UP: | 		case BOOT_UP: | ||||||
| 			if (millis() >= timer + 2000) { | 			if (millis() >= timer + 2000) { | ||||||
| 				screenState = PRESSURE; | 				machineState = PRESSURE; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			oled.setCursor(0,0); | 			oled.setCursor(0,0); | ||||||
| @@ -184,25 +186,25 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case PRESSURE: | 		case PRESSURE: | ||||||
| 			if (enterButton == PRESSED) { | 			if (enterButton == PRESSED) { | ||||||
| 				screenState = SAY_HOLD; | 				machineState = SAY_HOLD; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (enterButton == HELD) { | 			} else if (enterButton == HELD) { | ||||||
| 				;  // settings? | 				;  // settings? | ||||||
| 			} else if (upButton == PRESSED) { | 			} else if (upButton == PRESSED) { | ||||||
| 				screenState = SET_POINT; | 				machineState = SET_POINT; | ||||||
| 				pressureSetPoint = debouncedPressureValue+1; | 				pressureSetPoint = debouncedPressureValue+1; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (downButton == PRESSED) { | 			} else if (downButton == PRESSED) { | ||||||
| 				screenState = SET_POINT; | 				machineState = SET_POINT; | ||||||
| 				pressureSetPoint = debouncedPressureValue-1; | 				pressureSetPoint = debouncedPressureValue-1; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (upButton == HELD) { | 			} else if (upButton == HELD) { | ||||||
| 				screenState = SET_POINT; | 				machineState = SET_POINT; | ||||||
| 				pressureSetPoint = debouncedPressureValue+1; | 				pressureSetPoint = debouncedPressureValue+1; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (downButton == HELD) { | 			} else if (downButton == HELD) { | ||||||
| 				screenState = SET_POINT; | 				machineState = SET_POINT; | ||||||
| 				pressureSetPoint = debouncedPressureValue-1; | 				pressureSetPoint = debouncedPressureValue-1; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
| @@ -217,12 +219,12 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case SET_POINT: | 		case SET_POINT: | ||||||
| 			if (enterButton == PRESSED) { | 			if (enterButton == PRESSED) { | ||||||
| 				screenState = SAY_HOLD; | 				machineState = SAY_HOLD; | ||||||
| 				nextState = SET_POINT; | 				nextState = SET_POINT; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (enterButton == HELD) { | 			} else if (enterButton == HELD) { | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				screenState = INIT_RUN; | 				machineState = INIT_RUN; | ||||||
| 			} else if (upButton == PRESSED) { | 			} else if (upButton == PRESSED) { | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				pressureSetPoint++; | 				pressureSetPoint++; | ||||||
| @@ -238,7 +240,7 @@ void runUI() { | |||||||
| 				pressureSetPoint--; | 				pressureSetPoint--; | ||||||
| 				delay(75); | 				delay(75); | ||||||
| 			} else if (millis() >= timer + TIMEOUT_TIME) { | 			} else if (millis() >= timer + TIMEOUT_TIME) { | ||||||
| 				screenState = SAY_TIMEOUT; | 				machineState = SAY_TIMEOUT; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
| @@ -258,7 +260,7 @@ void runUI() { | |||||||
| 			initialPressure = pressureValue; | 			initialPressure = pressureValue; | ||||||
| 			startTime = millis(); | 			startTime = millis(); | ||||||
| 			timer = millis(); | 			timer = millis(); | ||||||
| 			screenState = BEGIN_RUN; | 			machineState = BEGIN_RUN; | ||||||
|  |  | ||||||
| 			oled.setCursor(0,0); | 			oled.setCursor(0,0); | ||||||
| 			oled.setTextSize(1); | 			oled.setTextSize(1); | ||||||
| @@ -276,7 +278,7 @@ void runUI() { | |||||||
| 				isInflating = false; | 				isInflating = false; | ||||||
| 			} else { | 			} else { | ||||||
| 				setSoleniod(SOLENOID_STOP); | 				setSoleniod(SOLENOID_STOP); | ||||||
| 				screenState = SAY_DONE; | 				machineState = SAY_DONE; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -288,7 +290,7 @@ void runUI() { | |||||||
| 			if (millis() >= timer + 5000) { | 			if (millis() >= timer + 5000) { | ||||||
| 				//runningPressure = pressureValue; | 				//runningPressure = pressureValue; | ||||||
| 				setSoleniod(SOLENOID_STOP); | 				setSoleniod(SOLENOID_STOP); | ||||||
| 				screenState = MEASURING; | 				machineState = MEASURING; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				stopTime = millis(); | 				stopTime = millis(); | ||||||
| 			} | 			} | ||||||
| @@ -296,6 +298,12 @@ void runUI() { | |||||||
| 			break; | 			break; | ||||||
|  |  | ||||||
| 		case MEASURING: | 		case MEASURING: | ||||||
|  | 			if (millis() < timer + 500) { | ||||||
|  | 				// wait for solenoids to settle before averaging | ||||||
|  | 				resamplePressure(pressureValue); | ||||||
|  |  | ||||||
|  | 			} | ||||||
|  |  | ||||||
| 			if (millis() >= timer + 3000) { | 			if (millis() >= timer + 3000) { | ||||||
| 				sampledPressure = pressureValue; | 				sampledPressure = pressureValue; | ||||||
|  |  | ||||||
| @@ -316,24 +324,19 @@ void runUI() { | |||||||
| 				initialPressure = sampledPressure; | 				initialPressure = sampledPressure; | ||||||
|  |  | ||||||
| 				if (isInflating && (int) sampledPressure >= pressureSetPoint) { | 				if (isInflating && (int) sampledPressure >= pressureSetPoint) { | ||||||
| 					screenState = SAY_DONE; | 					machineState = SAY_DONE; | ||||||
| 					nextState = PRESSURE; | 					nextState = PRESSURE; | ||||||
| 				} else if (isDeflating && (int) sampledPressure <= pressureSetPoint) { | 				} else if (isDeflating && (int) sampledPressure <= pressureSetPoint) { | ||||||
| 					screenState = SAY_DONE; | 					machineState = SAY_DONE; | ||||||
| 					nextState = PRESSURE; | 					nextState = PRESSURE; | ||||||
| 				} else { | 				} else { | ||||||
| 					screenState = RUNNING; | 					machineState = RUNNING; | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				startTime = millis(); | 				startTime = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (millis() < timer + 500) { |  | ||||||
| 				// wait for solenoids to settle before averaging |  | ||||||
| 				resamplePressure(pressureValue); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			setSoleniod(SOLENOID_STOP); | 			setSoleniod(SOLENOID_STOP); | ||||||
|  |  | ||||||
| 			oled.setCursor(0,0); | 			oled.setCursor(0,0); | ||||||
| @@ -348,27 +351,27 @@ void runUI() { | |||||||
| 		case RUNNING: | 		case RUNNING: | ||||||
| 			if (enterButton == PRESSED) { | 			if (enterButton == PRESSED) { | ||||||
| 				setSoleniod(SOLENOID_STOP); | 				setSoleniod(SOLENOID_STOP); | ||||||
| 				screenState = SAY_CANCEL; | 				machineState = SAY_CANCEL; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (upButton == PRESSED) { | 			} else if (upButton == PRESSED) { | ||||||
| 				setSoleniod(SOLENOID_STOP); | 				setSoleniod(SOLENOID_STOP); | ||||||
| 				screenState = SAY_CANCEL; | 				machineState = SAY_CANCEL; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} else if (downButton == PRESSED) { | 			} else if (downButton == PRESSED) { | ||||||
| 				setSoleniod(SOLENOID_STOP); | 				setSoleniod(SOLENOID_STOP); | ||||||
| 				screenState = SAY_CANCEL; | 				machineState = SAY_CANCEL; | ||||||
| 				nextState = PRESSURE; | 				nextState = PRESSURE; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (isInflating && millis() >= timer + 20000) { | 			if (isInflating && millis() >= timer + 20000) { | ||||||
| 				screenState = MEASURING; | 				machineState = MEASURING; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				stopTime = millis(); | 				stopTime = millis(); | ||||||
| 			} else if (isDeflating && millis() >= timer + 10000) { | 			} else if (isDeflating && millis() >= timer + 10000) { | ||||||
| 				screenState = MEASURING; | 				machineState = MEASURING; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 				stopTime = millis(); | 				stopTime = millis(); | ||||||
| 			} | 			} | ||||||
| @@ -403,7 +406,7 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case SAY_DONE: | 		case SAY_DONE: | ||||||
| 			if (millis() >= timer + 3000) { | 			if (millis() >= timer + 3000) { | ||||||
| 				screenState = nextState; | 				machineState = nextState; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -417,7 +420,7 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case SAY_CANCEL: | 		case SAY_CANCEL: | ||||||
| 			if (millis() >= timer + 1000) { | 			if (millis() >= timer + 1000) { | ||||||
| 				screenState = nextState; | 				machineState = nextState; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -431,7 +434,7 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case SAY_HOLD: | 		case SAY_HOLD: | ||||||
| 			if (millis() >= timer + 500) { | 			if (millis() >= timer + 500) { | ||||||
| 				screenState = nextState; | 				machineState = nextState; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -445,7 +448,7 @@ void runUI() { | |||||||
|  |  | ||||||
| 		case SAY_TIMEOUT: | 		case SAY_TIMEOUT: | ||||||
| 			if (millis() >= timer + 1000) { | 			if (millis() >= timer + 1000) { | ||||||
| 				screenState = nextState; | 				machineState = nextState; | ||||||
| 				timer = millis(); | 				timer = millis(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user