Im setting up an old machine at work for filling bottles. It's a piston filler and must cycle 4 times to complete a gallon fill. Right now it is foot switch activated and has no facilities for running on a conveyor (entry and exit gates, signalling the conveyor). I have, at this point in my project, setup the programming to know when the operator wants to run (start button), pause, or emergency stop. From there the program will operate the air piston (keep giving it air), count how many times the piston cycles via micro switch, and then reload the gate.
Right now the times for the conveyor/entrygate/exit gate are all estimations and will be changing when i get a chance to set everything up on line and have a control box made etc.
Basically, if the counts happen too fast (faster than it will in real life, under normal circumstances) the timing portion for the reloading program happens too fast and everything ends up in a loop. The machine usually takes 10seconds to cycle (increment to 4 counts), my program runs correct around 6 seconds. However, less than that it gets stuck in various loops. Unfortunately, i have a graveyard of tried phrases to try and get my time markers to reset or actuate properly. I started off by using blink without delay as my base... As you can see, i started departing from that template with the conveyor section, but for the rest of it i am still in the copy/paste sort of setup.
I am using an UNO and compiling with atmel studio. I am posting the problem section of code and attaching the sketch since it has too many lines to easily post here.
void reload() //is trigger when the micro switch has been triggered enough times
{
//structure to start a process:
//if pause button hasnt been hit AND we're supposed to be in the reload program AND the process isn't already running AND the count has been reached, then run
//structure for end sequence
//basically, i had to do a lot of gum and ductaping to try and keep things from running when the pause or stop buttons were hit
unsigned long conveyorMillis = millis();
Serial.print("Conveyor time: ");
Serial.println(conveyorMillis-prevConveyorMillis);
if ((pauseButState==0) && (reloadState==1) && (conveyorState==LOW)) // && (conveyorMillis - prevConveyorMillis > conveyorInterval)
{
conveyorState=HIGH;
//prevConveyorMillis=conveyorMillis;
}
else if ((conveyorState==HIGH) && (pauseButState==0) && (conveyorMillis - prevConveyorMillis > conveyorInterval) && conveyorState==0)
{
conveyorState=LOW;
prevConveyorMillis=conveyorMillis;
conveyorComplete=1;
}
unsigned long exitMillis = millis();
Serial.print("exit time: ");
Serial.println(exitMillis-prevXGmillis);
if ((pauseButState==0) && (reloadState==1) && (exitGateState==LOW) && (exitMillis - prevXGmillis > XGinterval))
{
Serial.println("exit gate");
exitGateState=HIGH;
prevXGmillis = exitMillis;
}
else if ((exitGateState==HIGH) && (pauseButState==0) && (exitMillis - prevXGmillis > XGinterval) )
{
exitGateState=LOW;
prevXGmillis = exitMillis; //(conveyorInterval-XGinterval+EGinterval)
exitGateComplete=1;
}
unsigned long entryMillis = millis();
if ((exitGateComplete==1) && (entryGateState==LOW) && (entryMillis - prevEGmillis > EGinterval) && (pauseButState==0) && (reloadState==1))
{
entryGateState=HIGH;
// Serial.println ("gate OPEN");
prevEGmillis = entryMillis;
}
//the following closes the entry gate and resets all of the states
else if ((entryGateState==HIGH) && (entryMillis - prevEGmillis > EGinterval) && (pauseButState == 0) && (conveyorComplete==1))
{
reloadState = 0;
entryGateState=LOW;
prevEGmillis = entryMillis;
startButState=1;
pistonSolState=1;
exitGateComplete=0;
conveyorComplete=0;
pistonRun();
prevConveyorMillis=conveyorMillis;
//prevXGmillis = exitMillis;
}
else if (stopButState==1) //sets everything to stop if the stop button is hit
{
entryGateState=LOW;
exitGateState=LOW;
conveyorState=LOW;
reloadState=0;
prevEGmillis = entryMillis;
}
else if ((pauseButState == 1))
{
prevConveyorMillis=conveyorMillis;
prevEGmillis=entryMillis;
prevXGmillis=exitMillis;
conveyorState=0;
exitGateState=0;
entryGateState=0;
reloadState=0;
startButState=0;
}
digitalWrite (exitGate,exitGateState);
digitalWrite(conveyorSol,conveyorState);
digitalWrite (entryGate,entryGateState);
}
you seem to have missed the lesson on how to use flags to block code so the timers are just running away and doing there own thing. you also have some funny code
conveyorState == HIGH and conveyorState == 0 in the same "if"
do you think that this line will wait for 3 seconds after exitGateState goes low ?
well the program didn't reset prevXGmillis so the timer was satisfied 2 seconds before it ever got to this line. After this "if" is run then the timer reset.
make life easy for us
make a list of what the relays/solinoids do and when they do it
seconds event
1 conveyorState run
2
3
4 entryGate run
5
6 conveyorState stop
7
8 entryGate stop
exitGate run
9
10
11 exitGate stop
runpiston loop
pause means stop and hold cycle
stop means reset to safe
e-stop is not code its hardwired in panel
with out details its hard to tell if you are trying to move things in sequence or they all move together
As for the contradictory if statement... that's embarrassing. That was supposed to be "conveyorComplete", which gets reset back to 0 after the entry gate closes. I was trying to ensure that if the entry gate closed, the conveyor would not start again (as it was sometimes due to the time). That was one of the last things i did before things really starting going to hell.
Sequence is, and im surprised i didn't write it out: Fill bottles, turn on and conveyor and open exit gate, close exit gate, open entry gate, close entry gate and shut off conveyor (on time or when photo-eye has counted proper number of bottles (still needs to be written)). The code right now assumes there are bottles under the fill heads to begin with. That will eventually be fixed.
So, pending actual times being used:
Seconds event
0 conveyor on
0 exit gate open (to evacuate the first bottles)
1
2
3 exit gate close
3 entry gate open (note this doesnt get called until the "exit gate complete = 1")
4
5
6 entry gate close, conveyor off
run piston loop
pause mean s finish the fill, but dont reload, or stop reload if in progress.
stop means stop the fill or reload and reset to safe.
this isn't code. see if you can understand what the code does. if you can understand the basic way the code is written you should be able to see the flow
// put your main code here, to run repeatedly:
//Seconds event
//0 conveyor on
//0 exit gate open (to evacuate the first bottles)
//1
//2
//3 exit gate close
//3 entry gate open (note this doesnt get called until the "exit gate complete = 1")
//4
//5
//6 entry gate close, conveyor off
//run piston loop
//may need these to be global
int conveyorStartDelay = 1; //5 second start delay
int conveyorStopDelay = 60; //5 second stop delay
int exitGateStartDelay = 1; //3second start delay
int exitGateStopDelay = 30; //3second stop delay
int entryGateRun = 30; //3 seconds after entryGateState goes high and conveyorComplete is 1
conveyorStopDelay=conveyorStopDelay + conveyorStopDelay;//same counter so correcting maths
exitGateStopDelay=exitGateStopDelay + exitGateStartDelay;//same counter so correcting maths
unsigned long conveyorMillis = millis();
if (pauseButState == 0) {//else later in program
if (stopButState == 0) {//else later in program
//stop timers if paused or stopped
//reset to 0 maybe in the stop code?
if (conveyorMillis - prevConveyorMillis > 100L) {
//prevConveyorMillis global unsigned long
//make these global int
exitGateTimer++//every 10th of a second +1
conveyortimer++//every 10th of a second +1
entryGateTimer++//every 10th of a second +1
prevConveyorMillis = conveyorMillis;
}
//dont know if you are looping so next lines just stop counters overflowing
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyortimer = constrain(conveyortimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
if ((conveyorState == LOW) && (conveyortimer > conveyorStartDelay)) {
conveyorState == HIGH;
}
if ((conveyorState == LOW) && (conveyortimer > conveyorStopDelay)) {
conveyorState == LOW;
conveyorComplete = 1;
}
if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay)) {
exitGateState == HIGH;
}
if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay)) {
exitGateState == LOW;
exitGateComplete = 1;
}
if ((exitGateComplete == 1) && (entryGateState == LOW) && (reloadState == 1)) {
entryGateState = HIGH;
entryGateTimer = 0;//set counter ready for next part
}
if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
{ reloadState = 0;
entryGateState = LOW;
startButState = 1;
pistonSolState = 1;
exitGateComplete = 0;
conveyorComplete = 0;
pistonRun();
exitGateTimer = 0; //set timers back to 0
conveyortimer = 0; //set timers back to 0
entryGateTimer = 0; //set timers back to 0
}
} else //stop button is 1
{ //needs a plan as this looks like pause code
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
//stop code should include
// exitGateTimer=0;
// conveyortimer=0;
// entryGateTimer=0;
}
} else//pause button is 1
{ //needs a plan as this looks like stop code not pause
prevConveyorMillis = conveyorMillis;
prevEGmillis = entryMillis;
prevXGmillis = exitMillis;
conveyorState = 0;
exitGateState = 0;
entryGateState = 0;
reloadState = 0;
startButState = 0;
}
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}
Will work it out tomorrow. Thank you so much for the guidance.
As for the pause bit - yes once it gets down into the reload() side of the program pause essentially acts as a stop. That was both intentional and a small part laziness. For the operation of the machine it will be inconsequential if, during reload, you hit pause and everything stops.
with out seeing the machine its impossible to real understand the actions. Im sure if you can understand the flow of code then you will be able to rewrite it or modify your code to make the machine work.
"Sequence is, and im surprised i didn't write it out"
if you start with a good written plan your code will look like the plan when you have finished.
Well, it was mapped in my head... using relative numbers... Planning would definitely go a long way for sure, im sure.
Unfortunately, the code is getting stuck. It;s entering into Millis, but it's not getting down into the start procedure. Evidently, it's not looping. Well, it's not looping now. It did earlier, though...
void reload() //is trigger when the micro switch has been triggered enough times
{
Serial.println("reload");
conveyorStopDelay=conveyorStopDelay + conveyorStopDelay;//same counter so correcting maths
exitGateStopDelay=exitGateStopDelay + exitGateStartDelay;//same counter so correcting maths
unsigned long conveyorMillis = millis();
if (pauseButState==0)
{
if (stopButState==0)
{
if (conveyorMillis - prevConveyorMillis > 100L)
{
Serial.println("inside millis");
exitGateTimer++;
conveyorTimer++;
entryGateTimer++;
prevConveyorMillis=conveyorMillis;
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyorTimer = constrain(conveyorTimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
} //comented back in
}//commented back in
}//commented back in
if ((conveyorState==LOW) && (conveyorTimer>conveyorStartDelay))
{
Serial.println("start conveyor");
conveyorState=HIGH;
}
if ((conveyorState==HIGH) && (conveyorTimer>conveyorStopDelay))
{
Serial.println("stop conveyor");
conveyorState=LOW;
conveyorComplete=1;
}
if ((exitGateState==LOW) && (exitGateTimer>exitGateStartDelay))
{
exitGateState=HIGH;
}
if ((exitGateState==HIGH) && (exitGateTimer>exitGateStopDelay))
{
exitGateState=LOW;
exitGateComplete=1;
}
if ((exitGateComplete==1) && (entryGateState==LOW))
{
entryGateState=HIGH;
entryGateTimer=0;
}
if ((entryGateState==HIGH) && (entryGateTimer>entryGateRun) && (conveyorComplete==1))
{
reloadState=0;
entryGateState=LOW;
startButState=1;
pistonSolState=1;
exitGateComplete=0;
conveyorComplete=0;
exitGateTimer=0;
conveyorTimer=0;
entryGateTimer=0;
pistonRun();
// } // added in last ditch
// } // added in last ditch
//} //added in last ditch
} //added in last ditch
else if (stopButState==1)
{
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
exitGateComplete=0;
conveyorComplete=0;
exitGateTimer=0;
conveyorTimer=0;
entryGateTimer=0;
}
else if (pauseButState==1)
{
prevConveyorMillis = conveyorMillis;
conveyorState=0;
exitGateState=0;
entryGateState=0;
reloadState=0;
startButState=0;
exitGateComplete=0;
conveyorComplete=0;
exitGateTimer=0;
conveyorTimer=0;
entryGateTimer=0;
}
Serial.print("digital write conveyor ");
Serial.println(conveyorState);
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}
ok copy this code to a blank ide then don't bother with reading it just scroll up and down and see how the code is spaced on the left hand side. Notice that the stop "if" lines up with the stop "else"
to make the code line up press ctrl+t and it will auto format. If its all over the place then you skipped or added a switch in the wrong place
p.s if you post code please post all of it as we can not compile to test it
Full code is included as an attachment to my last post.
edit: so to answer one of your earlier questions, no this isnt really looping. It's called upon in the body of the "fill/pistonrun()" program . I am going to make it loop, but add the first condition to be the reloadState==1
edit2: well. now it loops.. but it is not running the conveyor and it's getting stuck at the exit gate
boolean reloadComplete = 0;
const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW; //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 2000; //how long to keep the entry gate open
int entryGateRun = 30;
int entryGateTimer = 0;
const int exitGate = 8;
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 3000;
int exitGateStartDelay = 1;
int exitGateStopDelay = 30;
int exitGateTimer = 0;
const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
int conveyorStartDelay = 1;
int conveyorStopDelay = 60;
int conveyorTimer = 0;
const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;
const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;
const int pauseBut = 4; //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState= 0;
const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;
const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean microState = 0;
boolean lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting
boolean reloadState = 0; //if TRUE then run reload program
void setup() {
Serial.begin (115200);
pinMode (entryGate, OUTPUT);
pinMode(exitGate,OUTPUT);
pinMode(pistonSol, OUTPUT);
pinMode(conveyorSol,OUTPUT);
pinMode (startBut, INPUT_PULLUP);
pinMode(stopBut,INPUT_PULLUP);
pinMode (microSwitch, INPUT_PULLUP);
pinMode(pauseBut,INPUT_PULLUP);
Serial.println ("Piston Filler 01");
}
void loop() {
//sets the state for the stop button from which other functions follow
if (digitalRead(stopBut)==LOW)
{
stopButState=1;
//debounce/delay is intentional
}
//starts the process. At a future date this will start with the reload process to get the bottles under the machine.
if ((digitalRead(startBut)==LOW) && (reloadState ==0) && (startButState==0))
{
startButState=1;
pauseButState=0; //zeroes out the pause button if it was set earlier
pistonSolState=HIGH; //opens the solenoid to start filling bottles
stopButState=0; //zeroes out the stop button if it was hit earlier
Serial.println ("startBut LOW");
delay(150); //debouncer
}
if (startButState==1)
{
pistonRun();
}
if (digitalRead(pauseBut)==LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
{
pauseButState=1;
}
reload();
}
void pistonRun()
{
if ((digitalRead(microSwitch)==LOW) && (startButState==1)) //conditional statement so that the count only incremets when in run mode
{
microCount++; //counts how many time the micro switch is hit by the piston
Serial.print("M.Cnt ");
Serial.println(microCount);
delay(300);
}
if (microCount==pistonCycleCnt) //runs the reload program if piston has cycled enough times
{
reloadState = 1;
pistonSolState=0;
}
if (stopButState==1) // stops the piston from cycling and resets the count so when it starts, its back at 0
{
pistonSolState=0;
microCount=0;
}
digitalWrite(pistonSol,pistonSolState); //sets the piston solenoid to run or not
if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is separate from the act of counting in case i needed to add additional conditionals
{
reload();
microCount = 0;
startButState=0;
}
}
void reload() //is trigger when the micro switch has been triggered enough times
{
Serial.println("reload");
conveyorStopDelay = conveyorStopDelay + conveyorStopDelay; //same counter so correcting maths
exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths
unsigned long conveyorMillis = millis();
if (reloadState==1)
{
if (pauseButState == 0)
{
if (stopButState == 0)
{
if (conveyorMillis - prevConveyorMillis > 100L)
{
Serial.println("inside millis");
exitGateTimer++;
conveyorTimer++;
entryGateTimer++;
prevConveyorMillis = conveyorMillis;
}
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyorTimer = constrain(conveyorTimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
if ((conveyorState == LOW) && (conveyorTimer > conveyorStartDelay))
{
Serial.println("start conveyor");
conveyorState = HIGH;
}
if ((conveyorState == HIGH) && (conveyorTimer > conveyorStopDelay))
{
Serial.println("stop conveyor");
conveyorState = LOW;
conveyorComplete = 1;
}
if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay))
{
exitGateState = HIGH;
}
if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay))
{
exitGateState = LOW;
exitGateComplete = 1;
}
if ((exitGateComplete == 1) && (entryGateState == LOW))
{
entryGateState = HIGH;
entryGateTimer = 0;
}
if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
{
reloadState = 0;
entryGateState = LOW;
startButState = 1;
pistonSolState = 1;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
pistonRun();
} //added in last ditch
} else if (stopButState == 1)
{
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
}
} else if (pauseButState == 1)
{
prevConveyorMillis = conveyorMillis;
conveyorState = 0;
exitGateState = 0;
entryGateState = 0;
reloadState = 0;
startButState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
Serial.print("digital write conveyor ");
Serial.println(conveyorState);
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}
usually it doesn't take a whole program in the body, but i guess i didnt hit the line threshold this time:
Another thing that would be nice in your code is for you to leave gaps and make notes as pointers.
// the next "else" is linked to the "if" stop button
the compiler removes anything starting with // so you are not saving space. Big gaps between blocks of code also helps when you have been looking at code for a while it all kind of blurs together.
huh. it actually just finished running but the serial monitor doesnt show the conveyor having ever gone to HIGH, it looks like it just kept spitting out 0 (i caught the light change out of the corner of my eye).
edit: noted on the formatting. Will add those in for my sake and everyone elses if i post more code. I tried to add the pointers, at one point. Well, i remember doing it... but they're not there now
not entirely sure what's going on at this point. The conveyor and exit gate are turning on together, stay on for an undetermined amount of time (conveyorTime tops out at 255 and then just keeps repeating) and eventually those two shutoff and the entry gate turns on for a moment, then off, then it goes back to piston run.
For a minute i was having issues with the exit gate and conveyor turning back on for a moment when the exit gate was complete, but moving those timer rests to the conditional that reads the call for the reloadstate==1 solved that.
Thanks again for the help, i feel like i can i see the light - but the way this is going its probably a train!
boolean reloadComplete = 0; //flag for conditional statements
const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW; //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 2000; //how long to keep the entry gate open
int entryGateRun = 30; //how long to keep gate open for
int entryGateTimer = 0; //initial timer number
const int exitGate = 8; //
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 3000;
int exitGateStartDelay = 1;
int exitGateStopDelay = 30;
int exitGateTimer = 0;
const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
int conveyorStartDelay = 1;
int conveyorStopDelay = 60;
int conveyorTimer = 0;
const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;
const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;
const int pauseBut = 4; //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState= 0;
const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;
const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean microState = 0;
boolean lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting
boolean reloadState = 0; //if TRUE then run reload program
void setup() {
Serial.begin (115200);
pinMode (entryGate, OUTPUT);
pinMode(exitGate,OUTPUT);
pinMode(pistonSol, OUTPUT);
pinMode(conveyorSol,OUTPUT);
pinMode (startBut, INPUT_PULLUP);
pinMode(stopBut,INPUT_PULLUP);
pinMode (microSwitch, INPUT_PULLUP);
pinMode(pauseBut,INPUT_PULLUP);
Serial.println ("Piston Filler 01");
}
void loop() {
//sets the state for the stop button from which other functions follow
if (digitalRead(stopBut)==LOW)
{
stopButState=1;
//debounce/delay is intentional
}
//starts the process. At a future date this will start with the reload process to get the bottles under the machine.
if ((digitalRead(startBut)==LOW) && (reloadState ==0) && (startButState==0))
{
startButState=1;
pauseButState=0; //zeroes out the pause button if it was set earlier
pistonSolState=HIGH; //opens the solenoid to start filling bottles
stopButState=0; //zeroes out the stop button if it was hit earlier
Serial.println ("startBut LOW");
delay(150); //debouncer
}
if (startButState==1)
{
pistonRun();
}
if (digitalRead(pauseBut)==LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
{
pauseButState=1;
}
reload(); //added to the loop to for the counter functions, however it will not be called until reloadState==1
}
void pistonRun()
{
if ((digitalRead(microSwitch)==LOW) && (startButState==1)) //conditional statement so that the count only incremets when in run mode
{
microCount++; //counts how many time the micro switch is hit by the piston
Serial.print("M.Cnt ");
Serial.println(microCount);
delay(300);
}
if (microCount==pistonCycleCnt) //runs the reload program if piston has cycled enough times
{
reloadState = 1;
pistonSolState=0;
}
if (stopButState==1) // stops the piston from cycling and resets the count so when it starts, its back at 0
{
pistonSolState=0; //stops the piston from cycling again
microCount=0; //reset the count so it's ready to go for the next run
}
digitalWrite(pistonSol,pistonSolState); //sets the piston solenoid to run or not
if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is separate from the act of counting in case i needed to add additional conditionals
{
exitGateTimer = 0; //moved from the entry gate section of reload to here
conveyorTimer = 0; //""
entryGateTimer = 0; //""
reload();
microCount = 0;
startButState=0;
}
}
void reload() //is trigger when the micro switch has been triggered enough times
{
//Serial.println("reload");
//conveyorStopDelay = conveyorStopDelay + conveyorStartDelay; //same counter so correcting maths
//exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths
unsigned long conveyorMillis = millis();
if (reloadState==1) //since this is looping, this is the main phrase to call upon the relays to throw. called in pistonRun()
{
if (pauseButState == 0)
{
if (stopButState == 0)
{
if (conveyorMillis - prevConveyorMillis > 100L)
{
//Serial.println("inside millis");
exitGateTimer++;
conveyorTimer++;
entryGateTimer++;
prevConveyorMillis = conveyorMillis;
}
conveyorStopDelay = conveyorStopDelay + conveyorStartDelay; //same counter so correcting maths
exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyorTimer = constrain(conveyorTimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
Serial.print("conveyor timer ");
Serial.println(conveyorTimer);
if ((conveyorState == LOW) && (conveyorTimer > conveyorStartDelay))
{
//Serial.println("start conveyor");
conveyorState = HIGH;
}
if ((conveyorState == HIGH) && (conveyorTimer > conveyorStopDelay))
{
//Serial.println("stop conveyor");
conveyorState = LOW;
conveyorComplete = 1;
}
if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay))
{
exitGateState = HIGH;
}
if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay))
{
exitGateState = LOW;
exitGateComplete = 1;
}
if ((exitGateComplete == 1) && (entryGateState == LOW))
{
entryGateState = HIGH;
entryGateTimer = 0;
}
if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
{
reloadState = 0;
entryGateState = LOW;
startButState = 1;
pistonSolState = 1;
exitGateComplete = 0;
conveyorComplete = 0;
//exitGateTimer = 0;
//conveyorTimer = 0;
//entryGateTimer = 0;
pistonRun();
} //added in last ditch
}
}
else if (stopButState == 1)
{
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
//}
//}
else if (pauseButState == 1)
{
prevConveyorMillis = conveyorMillis;
conveyorState = 0;
exitGateState = 0;
entryGateState = 0;
reloadState = 0;
startButState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
//Serial.print("digital write conveyor ");
//Serial.println(conveyorState);
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}
}
you are really struggling with the concept of using { }
we may have to rewrite it and remove the else's as you are struggling
im showing 12% of memory used so im going to posted the code with correct switchs then rewirte that section to remove them
look at the text in the program
boolean reloadComplete = 0; //flag for conditional statements
const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW; //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 2000; //how long to keep the entry gate open
int entryGateRun = 30; //how long to keep gate open for
int entryGateTimer = 0; //initial timer number
const int exitGate = 8; //
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 3000;
int exitGateStartDelay = 1;
int exitGateStopDelay = 30;
int exitGateTimer = 0;
const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
int conveyorStartDelay = 1;
int conveyorStopDelay = 60;
int conveyorTimer = 0;
const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;
const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;
const int pauseBut = 4; //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState = 0;
const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;
const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean microState = 0;
boolean lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting
boolean reloadState = 0; //if TRUE then run reload program
void setup() {
Serial.begin (115200);
pinMode (entryGate, OUTPUT);
pinMode(exitGate, OUTPUT);
pinMode(pistonSol, OUTPUT);
pinMode(conveyorSol, OUTPUT);
pinMode (startBut, INPUT_PULLUP);
pinMode(stopBut, INPUT_PULLUP);
pinMode (microSwitch, INPUT_PULLUP);
pinMode(pauseBut, INPUT_PULLUP);
Serial.println ("Piston Filler 01");
}
void loop() {
//sets the state for the stop button from which other functions follow
if (digitalRead(stopBut) == LOW)
{
stopButState = 1;
//debounce/delay is intentional
}
//starts the process. At a future date this will start with the reload process to get the bottles under the machine.
if ((digitalRead(startBut) == LOW) && (reloadState == 0) && (startButState == 0))
{
startButState = 1;
pauseButState = 0; //zeroes out the pause button if it was set earlier
pistonSolState = HIGH; //opens the solenoid to start filling bottles
stopButState = 0; //zeroes out the stop button if it was hit earlier
Serial.println ("startBut LOW");
delay(150); //debouncer
}
if (startButState == 1)
{
pistonRun();
}
if (digitalRead(pauseBut) == LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
{
pauseButState = 1;
}
reload(); //added to the loop to for the counter functions, however it will not be called until reloadState==1
}
void pistonRun()
{
if ((digitalRead(microSwitch) == LOW) && (startButState == 1)) //conditional statement so that the count only incremets when in run mode
{
microCount++; //counts how many time the micro switch is hit by the piston
Serial.print("M.Cnt ");
Serial.println(microCount);
delay(300);
}
if (microCount == pistonCycleCnt) //runs the reload program if piston has cycled enough times
{
reloadState = 1;
pistonSolState = 0;
}
if (stopButState == 1) // stops the piston from cycling and resets the count so when it starts, its back at 0
{
pistonSolState = 0; //stops the piston from cycling again
microCount = 0; //reset the count so it's ready to go for the next run
}
digitalWrite(pistonSol, pistonSolState); //sets the piston solenoid to run or not
if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is separate from the act of counting in case i needed to add additional conditionals
{
exitGateTimer = 0; //moved from the entry gate section of reload to here
conveyorTimer = 0; //""
entryGateTimer = 0; //""
reload();
microCount = 0;
startButState = 0;
}
}
void reload() //is trigger when the micro switch has been triggered enough times
{
//Serial.println("reload");
unsigned long conveyorMillis = millis();
if (reloadState == 1) //since this is looping, this is the main phrase to call upon the relays to throw. called in pistonRun()
{ //open reload switch closed is near end of this loop
if (pauseButState == 0)
{ // i need to use else so that the last else in this loop
if (stopButState == 0)
{ //i need to use else so that second to last in this loop
if (conveyorMillis - prevConveyorMillis > 100L)
{
//Serial.println("inside millis");
exitGateTimer++;
conveyorTimer++;
entryGateTimer++;
prevConveyorMillis = conveyorMillis;
}
conveyorStopDelay = conveyorStopDelay + conveyorStartDelay; //same counter so correcting maths
exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyorTimer = constrain(conveyorTimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
Serial.print("conveyor timer ");
Serial.println(conveyorTimer);
if ((conveyorState == LOW) && (conveyorTimer > conveyorStartDelay))
{
//Serial.println("start conveyor");
conveyorState = HIGH;
}
if ((conveyorState == HIGH) && (conveyorTimer > conveyorStopDelay))
{
//Serial.println("stop conveyor");
conveyorState = LOW;
conveyorComplete = 1;
}
if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay))
{
exitGateState = HIGH;
}
if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay))
{
exitGateState = LOW;
exitGateComplete = 1;
}
if ((exitGateComplete == 1) && (entryGateState == LOW))
{
entryGateState = HIGH;
entryGateTimer = 0;
}
if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
{
reloadState = 0;
entryGateState = LOW;
startButState = 1;
pistonSolState = 1;
exitGateComplete = 0;
conveyorComplete = 0;
//exitGateTimer = 0;
//conveyorTimer = 0;
//entryGateTimer = 0;
pistonRun();
}
//following code closes the 3 if arguement in the beginning of the
//reload loop in the reverse order
//stopButState
//pausebutstate
//reloadstate
} else {//stopButState == 1
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}//this switch closes the else after stopbutstate
} else {//pauseButState ==1
prevConveyorMillis = conveyorMillis;
conveyorState = 0;
exitGateState = 0;
entryGateState = 0;
reloadState = 0;
startButState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}//this switch closes the else after pausebutstate
//Serial.print("digital write conveyor ");
//Serial.println(conveyorState);
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}//this switch closes the reload state
}//this is the end of the reload loop
boolean reloadComplete = 0; //flag for conditional statements
const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW; //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 2000; //how long to keep the entry gate open
int entryGateRun = 30; //how long to keep gate open for
int entryGateTimer = 0; //initial timer number
const int exitGate = 8; //
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 3000;
int exitGateStartDelay = 1;
int exitGateStopDelay = 30;
int exitGateTimer = 0;
const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
int conveyorStartDelay = 1;
int conveyorStopDelay = 60;
int conveyorTimer = 0;
const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;
const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;
const int pauseBut = 4; //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState = 0;
const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;
const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean microState = 0;
boolean lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting
boolean reloadState = 0; //if TRUE then run reload program
void setup() {
Serial.begin (115200);
pinMode (entryGate, OUTPUT);
pinMode(exitGate, OUTPUT);
pinMode(pistonSol, OUTPUT);
pinMode(conveyorSol, OUTPUT);
pinMode (startBut, INPUT_PULLUP);
pinMode(stopBut, INPUT_PULLUP);
pinMode (microSwitch, INPUT_PULLUP);
pinMode(pauseBut, INPUT_PULLUP);
Serial.println ("Piston Filler 01");
}
void loop() {
//sets the state for the stop button from which other functions follow
if (digitalRead(stopBut) == LOW)
{
stopButState = 1;
//debounce/delay is intentional
}
//starts the process. At a future date this will start with the reload process to get the bottles under the machine.
if ((digitalRead(startBut) == LOW) && (reloadState == 0) && (startButState == 0))
{
startButState = 1;
pauseButState = 0; //zeroes out the pause button if it was set earlier
pistonSolState = HIGH; //opens the solenoid to start filling bottles
stopButState = 0; //zeroes out the stop button if it was hit earlier
Serial.println ("startBut LOW");
delay(150); //debouncer
}
if (startButState == 1)
{
pistonRun();
}
if (digitalRead(pauseBut) == LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
{
pauseButState = 1;
}
reload(); //added to the loop to for the counter functions, however it will not be called until reloadState==1
}
void pistonRun()
{
if ((digitalRead(microSwitch) == LOW) && (startButState == 1)) //conditional statement so that the count only incremets when in run mode
{
microCount++; //counts how many time the micro switch is hit by the piston
Serial.print("M.Cnt ");
Serial.println(microCount);
delay(300);
}
if (microCount == pistonCycleCnt) //runs the reload program if piston has cycled enough times
{
reloadState = 1;
pistonSolState = 0;
}
if (stopButState == 1) // stops the piston from cycling and resets the count so when it starts, its back at 0
{
pistonSolState = 0; //stops the piston from cycling again
microCount = 0; //reset the count so it's ready to go for the next run
}
digitalWrite(pistonSol, pistonSolState); //sets the piston solenoid to run or not
if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is separate from the act of counting in case i needed to add additional conditionals
{
exitGateTimer = 0; //moved from the entry gate section of reload to here
conveyorTimer = 0; //""
entryGateTimer = 0; //""
reload();
microCount = 0;
startButState = 0;
}
}
void reload() //is trigger when the micro switch has been triggered enough times
{
//Serial.println("reload");
unsigned long conveyorMillis = millis();
if ((reloadState == 1) && (pauseButState == 0) && (stopButState == 0)) {
//since this is looping, this is the main phrase to call upon the relays to throw. called in pistonRun()
if (conveyorMillis - prevConveyorMillis > 100L)
{
//Serial.println("inside millis");
exitGateTimer++;
conveyorTimer++;
entryGateTimer++;
prevConveyorMillis = conveyorMillis;
}
conveyorStopDelay = conveyorStopDelay + conveyorStartDelay; //same counter so correcting maths
exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths
exitGateTimer = constrain(exitGateTimer, 0, 255);
conveyorTimer = constrain(conveyorTimer, 0, 255);
entryGateTimer = constrain(entryGateTimer, 0, 255);
Serial.print("conveyor timer ");
Serial.println(conveyorTimer);
if ((conveyorState == LOW) && (conveyorTimer > conveyorStartDelay))
{
//Serial.println("start conveyor");
conveyorState = HIGH;
}
if ((conveyorState == HIGH) && (conveyorTimer > conveyorStopDelay))
{
//Serial.println("stop conveyor");
conveyorState = LOW;
conveyorComplete = 1;
}
if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay))
{
exitGateState = HIGH;
}
if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay))
{
exitGateState = LOW;
exitGateComplete = 1;
}
if ((exitGateComplete == 1) && (entryGateState == LOW))
{
entryGateState = HIGH;
entryGateTimer = 0;
}
if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
{
reloadState = 0;
entryGateState = LOW;
startButState = 1;
pistonSolState = 1;
exitGateComplete = 0;
conveyorComplete = 0;
//exitGateTimer = 0;
//conveyorTimer = 0;
//entryGateTimer = 0;
pistonRun();
}
} //this switch closes reloadState == 1 and pauseButState == 0 and stopButState == 0
if (stopButState == 1) {
entryGateState = LOW;
exitGateState = LOW;
conveyorState = LOW;
reloadState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
if (pauseButState == 1) {
prevConveyorMillis = conveyorMillis;
conveyorState = 0;
exitGateState = 0;
entryGateState = 0;
reloadState = 0;
startButState = 0;
exitGateComplete = 0;
conveyorComplete = 0;
exitGateTimer = 0;
conveyorTimer = 0;
entryGateTimer = 0;
}
//Serial.print("digital write conveyor ");
//Serial.println(conveyorState);
digitalWrite (exitGate, exitGateState);
digitalWrite(conveyorSol, conveyorState);
digitalWrite (entryGate, entryGateState);
}//this is the end of the reload loop
Oh well, and i do feel bad about this, in the time that you added your replies i had gone back to the beginning and tried to get it to work my way again... I was adding this post and i got the "warning new posts have been added". I will of course be looking closely at your code and comparing them to avoid the mistakes i made on the earlier go-arounds.
original post:
so i decided to have another go at my original idea and i got it working. The fundamental fix came when i started using conveyorMillis (now uniTimeMillis) to control the times for all functions. Then it was a matter of making sure the things that were supposed to be looped, looped, and the thing s that weren't (eg. setting the variable time markers) didn't loop. Now, now matter how fast or slow the cycle is, the timers all accrue properly. I think i've also got everything laid out in such a way that if i need to add a dampening effect, i can without too much difficulty. By forcing the conveyor to keep running until the entry gate finished i have also ensured that the entry gate will not close and the conveyor will not stop until the eye has counted two bottles (Still not written). Unfortunately, at one point when i first restarted the project, i had some beatutiful notes... then it crashed for some reason and i went to work experimenting instead of noting. IF you or someone gets curious about the code i will go back and add my final copy with the notes when i get around to writing them.
boolean reloadComplete = 0;
const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW; //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 1500; //how long to keep the entry gate open
unsigned long entryMillis = 0;
boolean entryGateComplete = 0;
const int exitGate = 8;
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 2000;
unsigned long exitMillis = 0;
const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
unsigned long uniTimeMillis=0;
const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;
const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;
const int pauseBut = 4; //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState= 0;
const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;
const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean microState = 0;
boolean lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting
boolean reloadState = 0; //if TRUE then run reload program
void setup() {
Serial.begin (115200);
pinMode (entryGate, OUTPUT);
pinMode(exitGate,OUTPUT);
pinMode(pistonSol, OUTPUT);
pinMode(conveyorSol,OUTPUT);
pinMode (startBut, INPUT_PULLUP);
pinMode(stopBut,INPUT_PULLUP);
pinMode (microSwitch, INPUT_PULLUP);
pinMode(pauseBut,INPUT_PULLUP);
Serial.println ("Piston Filler 01");
}
void loop() {
//sets the state for the stop button from which other functions follow
if (digitalRead(stopBut)==LOW)
{
stopButState=1;
//debounce/delay is intentional
}
//starts the process. At a future date this will start with the reload process to get the bottles under the machine.
if ((digitalRead(startBut)==LOW) && (reloadState ==0) && (startButState==0))
{
startButState=1;
pauseButState=0; //zeroes out the pause button if it was set earlier
pistonSolState=HIGH; //opens the solenoid to start filling bottles
stopButState=0; //zeroes out the stop button if it was hit earlier
Serial.println ("startBut LOW");
delay(150); //debouncer
}
if (startButState==1)
{
pistonRun();
}
if (digitalRead(pauseBut)==LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
{
pauseButState=1;
}
}
void pistonRun()
{
if ((digitalRead(microSwitch)==LOW) && (startButState==1)) //conditional statement so that the count only incremets when in run mode
{
microCount++; //counts how many time the micro switch is hit by the piston
Serial.print("M.Cnt ");
Serial.println(microCount);
delay(300);
}
if (microCount==pistonCycleCnt) //runs the reload program if piston has cycled enough times
{
reloadState = 1;
pistonSolState=0;
}
if (stopButState==1) // stops the piston from cycling and resets the count so when it starts, its back at 0
{
pistonSolState=0;
microCount=0;
}
digitalWrite(pistonSol,pistonSolState); //sets the piston solenoid to run or not
if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is seperate from the act of counting in case i needed to add additional conditionals
{
reload();
microCount = 0;
startButState==0;
}
}
void reload() //is trigger when the micro switch has been triggered enough times
{
if ((pauseButState==0)&&(stopButState==0)&&(reloadState==1))
{
uniTimeMillis=millis();
if ((conveyorState==LOW) && (conveyorComplete==0))
{
conveyorState=HIGH;
prevConveyorMillis=uniTimeMillis;
prevXGmillis=uniTimeMillis;
prevEGmillis = uniTimeMillis+XGinterval;
conveyorComplete=0;
Serial.println("conveyor on");
}
if ((conveyorState==HIGH)&&(uniTimeMillis-prevConveyorMillis>conveyorInterval) && (conveyorComplete!=1) && (entryGateComplete==1))
{
conveyorComplete=1;
conveyorState=LOW;
}
if ((exitGateState==LOW) && (exitGateComplete==0));
{
exitGateState=HIGH;
Serial.println(uniTimeMillis-prevXGmillis);
}
if ((exitGateState==HIGH) && (uniTimeMillis - prevXGmillis > XGinterval))
{
exitGateState=LOW;
exitGateComplete=1;
}
if ((entryGateState==LOW) && (exitGateComplete==1))
{
entryGateState=HIGH;
}
if ((entryGateState==HIGH) && (uniTimeMillis - prevEGmillis > EGinterval)) //last added conveyor state complete
{
entryGateState=LOW;
entryGateComplete=1;
}
if ((conveyorComplete==1) && (exitGateComplete==1) && (entryGateComplete==1))
{
startButState=1;
reloadState=0;
conveyorState=LOW;
conveyorComplete=0;
exitGateState=LOW;
exitGateComplete=0;
entryGateState=LOW;
entryGateComplete=0;
pistonSolState=HIGH;
}
}
else if (stopButState==1) //sets everything to stop if the stop button is hit
{
entryGateState=LOW;
exitGateState=LOW;
conveyorState=LOW;
reloadState=0;
startButState=0;
}
else if ((pauseButState == 1)) //allows the piston to keep stroking but either aborts or stops the reload process
{ // then it resets everything so its ready to go for the next run.
conveyorState=0;
exitGateState=0;
entryGateState=0;
reloadState=0;
startButState=0;
}
digitalWrite(conveyorSol,conveyorState);
digitalWrite(exitGate,exitGateState);
digitalWrite(entryGate,entryGateState);
}