I have this IF statement
else if (digitalRead(triggerPin) == HIGH && startActivated == true) {
Serial.println('\n');
Serial.println("DROPPING HEAD, PRESSURE 1 ON");
Monitor();
currentState = PRESSURE1;
releaseHead();
enablePressure1();
}
break;
case PRESSURE1:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
}
else if (digitalRead(triggerPin) == LOW && pressure1Done == false && pressure2Done == false) {
Serial.println('\n');
Serial.println("STARTING TIMER 1");
Monitor();
startT1();
delay(100);
pressure1Done = true;
currentState = PRESSURE2;
}
break;
It is triggering and executing the method StartT1() Even though triggerPin is HIGH.
The previous case enables the priming pressure, and then once a microswitch is pressed (triggerPin goes LOW) StartT1() is called which starts an external timer.
However the timer is being started before the trigger pin is pulled LOW by the microswitch.
I have spent hours and I just cant work it out.
Here's my serial monitor output,
CYCLE STARTING
Trigger State: 1
Start State: 1
Home State: 0
Active State: 0
Timer 1 State: 0
Timer 2 State: 0
DROPPING HEAD, PRESSURE 1 ON
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0
STARTING TIMER 1
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0
PRESSURE 2 ON, STARTING TIMER 2
Trigger State: 0
Start State: 1
Home State: 1
Active State: 1
Timer 1 State: 1
Timer 2 State: 0
RASISING HEAD
Trigger State: 0
Start State: 1
Home State: 1
Active State: 1
Timer 1 State: 1
Timer 2 State: 1
And here's my full code
// Define states
enum State {
IDLE,
STOP,
DROPHEAD,
PRESSURE1,
PRESSURE2,
GOHOME,
CYCLE_COMPLETE
};
// Define input pins
const int startPin = D12;
const int StopPin = D11;
const int triggerPin = D10;
const int homePin = D9;
const int t1OutPin = D8;
const int t2OutPin = D7;
// Define output pins
const int t1StartPin = D5;
const int t2StartPin = D1;
const int dSol1Pin = D2;
const int dSol2Pin = D3;
const int uSol1Pin = D4;
const int runningPin = D0;
// Define initial states
State currentState = IDLE;
bool startActivated = false;
bool pressure1Done = false;
bool pressure2Done = false;
int startState;
int homeState;
int triggerState;
//Timer stuff
unsigned long T1startTime;
unsigned long T2startTime;
int T1oldState;
int T2oldState;
void setup() {
// Set input pins
pinMode(startPin, INPUT_PULLUP);
pinMode(StopPin, INPUT_PULLUP);
pinMode(triggerPin, INPUT_PULLUP);
pinMode(homePin, INPUT_PULLUP);
pinMode(t1OutPin, INPUT_PULLUP);
pinMode(t2OutPin, INPUT_PULLUP);
// Set output pins
pinMode(t1StartPin, OUTPUT);
pinMode(t2StartPin, OUTPUT);
pinMode(dSol1Pin, OUTPUT);
pinMode(dSol2Pin, OUTPUT);
pinMode(uSol1Pin, OUTPUT);
pinMode(runningPin, OUTPUT);
// Initialize outputs - Outputs are sinking so LOW is ON, HIGH is OFF.
digitalWrite(t1StartPin, HIGH);
digitalWrite(t2StartPin, HIGH);
digitalWrite(dSol1Pin, HIGH);
digitalWrite(dSol2Pin, HIGH);
digitalWrite(uSol1Pin, LOW);
digitalWrite(runningPin, HIGH);
startState = digitalRead(startPin);
homeState = digitalRead(homePin);
triggerState = digitalRead(triggerPin);
Serial.begin(9600);
}
void loop() {
switch (currentState) {
case IDLE:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
} else if (digitalRead(homePin) == LOW && digitalRead(startPin) == LOW && startActivated == false) {
Serial.println('\n');
Serial.println("CYCLE STARTING");
Monitor();
currentState = DROPHEAD;
startActivated = true;
}
break;
case STOP:
if (digitalRead(StopPin) == HIGH) {
Serial.println('\n');
Serial.println("STOP RELEASED RETURNING TO IDLE");
currentState = IDLE;
resetProgram();
}
break;
case DROPHEAD:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
}
else if (digitalRead(startPin) == HIGH) {
Serial.println('\n');
Serial.println("START NOT PRESSED, RETURNING TO IDLE");
currentState = IDLE;
startActivated = false;
}
else if (digitalRead(triggerPin) == HIGH && startActivated == true) {
Serial.println('\n');
Serial.println("DROPPING HEAD, PRESSURE 1 ON");
Monitor();
currentState = PRESSURE1;
releaseHead();
enablePressure1();
}
break;
case PRESSURE1:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
}
else if (digitalRead(triggerPin) == LOW && pressure1Done == false && pressure2Done == false) {
Serial.println('\n');
Serial.println("STARTING TIMER 1");
Monitor();
startT1();
delay(100);
pressure1Done = true;
currentState = PRESSURE2;
}
break;
case PRESSURE2:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
}
else if (digitalRead(triggerPin) == LOW && digitalRead(t1OutPin) == HIGH && pressure1Done == true && pressure2Done == false) {
Serial.println('\n');
Serial.println("PRESSURE 2 ON, STARTING TIMER 2");
Monitor();
enablePressure2();
startT2();
delay(100);
pressure2Done = true;
currentState = GOHOME;
}
break;
case GOHOME:
if (digitalRead(StopPin) == LOW) {
Serial.println('\n');
Serial.println("STOP PRESSED");
currentState = STOP;
disableOutputs();
}
else if (digitalRead(triggerPin) == LOW && digitalRead(t2OutPin) == HIGH && pressure1Done == true && pressure2Done == true) {
Serial.println('\n');
Serial.println("RASISING HEAD");
Monitor();
StopT1();
StopT2();
clampHead();
currentState = CYCLE_COMPLETE;
}
break;
case CYCLE_COMPLETE:
if (digitalRead(homePin) == LOW) {
Serial.println('\n');
Serial.println("CYCLE COMPLETED SUCCESSFULLY RETURNING TO IDLE");
currentState = IDLE;
resetProgram();
} else if (digitalRead(startPin) == LOW) {
Serial.println('\n');
Serial.println("START BUTTONS HELD - CYCLE COMPLETED SUCCESSFULLY RETURNING TO IDLE");
currentState = IDLE;
resetProgram();
}
break;
}
}
void releaseHead() {
digitalWrite(uSol1Pin, HIGH);
}
void clampHead() {
digitalWrite(uSol1Pin, LOW);
digitalWrite(dSol1Pin, HIGH);
digitalWrite(dSol2Pin, HIGH);
}
void enablePressure1() {
digitalWrite(dSol1Pin, LOW);
}
void enablePressure2() {
digitalWrite(dSol2Pin, LOW);
}
void startT1() {
digitalWrite(t1StartPin, LOW);
}
void StopT1() {
digitalWrite(t1StartPin, HIGH);
}
void startT2() {
digitalWrite(t2StartPin, LOW);
}
void StopT2() {
digitalWrite(t2StartPin, HIGH);
}
void Monitor() {
startState = digitalRead(startPin);
homeState = digitalRead(homePin);
triggerState = digitalRead(triggerPin);
Serial.println((String)"Trigger State: " + triggerState);
Serial.println((String)"Start State: " + startState);
Serial.println((String)"Home State: " + homeState);
Serial.println((String)"Active State: " + startActivated);
Serial.println((String)"Timer 1 State: " + pressure1Done);
Serial.println((String)"Timer 2 State: " + pressure2Done);
}
void disableOutputs() {
digitalWrite(t1StartPin, HIGH);
digitalWrite(t2StartPin, HIGH);
digitalWrite(dSol1Pin, HIGH);
digitalWrite(dSol2Pin, HIGH);
digitalWrite(uSol1Pin, HIGH);
digitalWrite(runningPin, HIGH);
}
void enableReturn() {
digitalWrite(t1StartPin, HIGH);
digitalWrite(t2StartPin, HIGH);
digitalWrite(dSol1Pin, HIGH);
digitalWrite(dSol2Pin, HIGH);
digitalWrite(uSol1Pin, LOW);
digitalWrite(runningPin, HIGH);
}
void resetProgram() {
startActivated = false;
pressure1Done = false;
pressure2Done = false;
currentState = IDLE;
enableReturn();
}