My apologies about the code. Here it is:
/* Cast Stone Tamper
by Lisa M. Fida, 2022
Designed specifically to tamp cast stone mixture into rubber molds
*/
// global variables
int intSafetyState; // variable for reading safety door status
int intStopState; // variable for reading stopbutton status
int intLastStopState = HIGH; // the previous reading from the input pin (INPUT_PULLUP makes button read HIGH when open)
int intStartState; // variable for reading startbutton status
int intLastStartState = HIGH; // the previosu reading from the input pin (INPUT_PULLUP makes button read HIGH when open)
int intLinActMinState; // variable for reading linear actuator minimun status
bool boolSafety = false;
bool boolStop = false;
bool boolStart = false;
bool boolLinActMin = false;
long currentMillis = 0;
long previousMillis = 0;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
bool boolGantryUp = false;
bool boolGantryDown = false;
bool boolTampBoxOpen = false;
bool boolTampBoxClose = false;
const int intTampBoxPulseDelay = 1000;
//digital pin locations
const int swtAirValvePin = 2;
const int relLinAct1Pin = 3;
const int relLinAct2Pin = 4;
const int stepTampBoxDirPin = 5;
const int stepTampBoxStepPin = 6;
const int swtLinActMinPin = 7;
const int btnStopPin = 8; // red momentary button (green)
const int btnStartPin = 9; // green mementary buttons (blue)
const int swtSafetyDoorPin = 10;
const int stepTampBoxEnablePin = 11;
//analog pin locations
const int swtTampBoxLimitPin = A1; // in parallel (use analog input)
const int swtUpperLowerLimitPin = A2; // in parallel (use analog input)
const int swtPosTBLAPin = A3; // in parallel (use analog input)
void setup() {
// initialize serial monitor
Serial.begin(9600);
// initialize digital pins as input/output
pinMode(swtAirValvePin, OUTPUT);
pinMode(relLinAct1Pin, OUTPUT);
pinMode(relLinAct2Pin, OUTPUT);
pinMode(stepTampBoxDirPin, OUTPUT);
pinMode(stepTampBoxStepPin, OUTPUT);
pinMode(swtLinActMinPin, INPUT_PULLUP); // reads LOW when pressed
pinMode(btnStopPin, INPUT_PULLUP); // reads LOW when pressed
pinMode(btnStartPin, INPUT_PULLUP); // reads LOW when pressed
pinMode(swtSafetyDoorPin, INPUT_PULLUP); // reads LOW when pressed
pinMode(stepTampBoxEnablePin, OUTPUT);
// initialize analog pins as input/output
pinMode(swtTampBoxLimitPin, INPUT_PULLUP);
pinMode(swtUpperLowerLimitPin, INPUT_PULLUP);
pinMode(swtPosTBLAPin, INPUT_PULLUP);
// set Linear Actuator motor off
digitalWrite(relLinAct1Pin, LOW);
digitalWrite(relLinAct2Pin, LOW);
// set Tamp Box stepper motor off
digitalWrite(stepTampBoxEnablePin, HIGH); // HIGH turns the motor off
}
void loop() {
//Read operation switch to see which mode to enter
// This is an anolog input so we are looking for a range of values
int PosTBLAreading = analogRead(swtPosTBLAPin);
if (PosTBLAreading >= 210 && PosTBLAreading <= 250) { // Linear Actuator Positioning Mode
PositionGantry();
}
else if (PosTBLAreading >= 110 && PosTBLAreading <= 155) {
PositionTB();
}
else {
TampCycle();
}
}
void PositionGantry() {
CheckGantryLimitState();
CheckLinActMinState();
// Lower the gantry as long as the start buttons are pressed and no limits reached
if ((digitalRead(btnStartPin) == LOW) && (!boolGantryDown) && (!boolLinActMin)) {
digitalWrite(relLinAct1Pin, LOW);
digitalWrite(relLinAct2Pin, HIGH);
boolStart = true;
while ((boolStart) && (!boolGantryDown) && (!boolLinActMin)) {
if (digitalRead(btnStartPin) == HIGH) { // No longer being pressed
boolStart = false;
}
CheckGantryLimitState();
CheckLinActMinState();
}
GantryStop();
}
// Raise the gantry as long as the stop button is pressed and no limits reached
else if ((digitalRead(btnStopPin) == LOW) && (!boolGantryUp)) {
digitalWrite(relLinAct1Pin, HIGH);
digitalWrite(relLinAct2Pin, LOW);
boolStop = true;
while ((boolStop) && (!boolGantryUp)) {
if (digitalRead(btnStopPin) == HIGH) { // No longer being pressed
boolStop = false;
}
CheckGantryLimitState();
}
GantryStop();
}
else {
GantryStop();
}
}
void PositionTB() {
CheckTampBoxLimitState();
if ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
digitalWrite(stepTampBoxEnablePin, LOW);
digitalWrite(stepTampBoxDirPin, LOW);
}
while ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
digitalWrite(stepTampBoxStepPin, HIGH);
delayMicroseconds(intTampBoxPulseDelay);
digitalWrite(stepTampBoxStepPin, LOW);
delayMicroseconds(intTampBoxPulseDelay);
CheckTampBoxLimitState();
// Serial.print("boolTampBoxClose = ");
// Serial.println(boolTampBoxClose);
}
if ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) {
digitalWrite(stepTampBoxEnablePin, LOW);
digitalWrite(stepTampBoxDirPin, HIGH);
}
while ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) {
digitalWrite(stepTampBoxStepPin, HIGH);
delayMicroseconds(intTampBoxPulseDelay);
digitalWrite(stepTampBoxStepPin, LOW);
delayMicroseconds(intTampBoxPulseDelay);
CheckTampBoxLimitState();
// Serial.print("boolTampBoxOpen = ");
// Serial.println(boolTampBoxOpen);
}
digitalWrite(stepTampBoxEnablePin, HIGH);
}
void TampCycle() {
CheckStartState();
CheckStopState();
if ((boolStart) && (!boolStop)) {
TampBoxClose(); // close the Tamp Box
GantryDown(); // lower Linear Actuator to its minimum
if ((!boolStop) && (boolSafety)) {
AirValve(1); // open Air Valve
}
while ((!boolGantryDown) && (!boolStop) && (boolSafety)) {
CheckGantryLimitState(); // while air is open keep checking to see if Gantry has reached the bottom limit switch
CheckStopState();
CheckSafetyState();
}
AirValve(0); // close Air Valve
GantryUp(); // raise Gantry
TampBoxOpen(); // open the Tamp Box
if ((!boolStop) && (boolSafety)) {
boolStart = false;
}
}
}
void CheckSafetyState() {
intSafetyState = digitalRead(swtSafetyDoorPin); // HIGH for an open door
if (intSafetyState == HIGH) { // Door is open
boolSafety = false;
}
else { // Door is closed
boolSafety = true;
}
}
void CheckStopState() {
// read the state of the Stop/Reset button into a local variable: (set to INPUT_PULLUP)
// Open button reads HIGH, so we want to look for a LOW state
// for the rest of the code to work.
int stopreading = digitalRead(btnStopPin); // Open button reads HIGH, so we want the opposite for the rest of the code to work.
// check to see if you just pressed the button
// (i.e. the input went from HIGH to LOW), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (stopreading != intLastStopState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the stopreading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (stopreading != intStopState) {
intStopState = stopreading;
// only continue if the new button state is LOW
if (intStopState == LOW) {
boolStop = true;
AirValve(0); // close the air valve
}
}
}
// save the stopreading. Next time through the loop, it'll be the lastButtonState:
intLastStopState = stopreading;
}
void CheckStartState() {
// read the state of the Stop/Reset button into a local variable: (set to INPUT_PULLUP)
// Open button reads HIGH, so we want to look for a LOW state
// for the rest of the code to work.
int startreading = digitalRead(btnStartPin); // Open button reads HIGH, so we want the opposite for the rest of the code to work.
// check to see if you just pressed the button
// (i.e. the input went from HIGH to LOW), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (startreading != intLastStartState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the startreading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (startreading != intStartState) {
intStartState = startreading;
// only continue if the new button state is LOW
if (intStartState == LOW) {
boolStart = true;
}
}
}
// save the startreading. Next time through the loop, it'll be the lastButtonState:
intLastStartState = startreading;
}
void GantryDown() {
CheckGantryLimitState();
CheckLinActMinState();
CheckStopState();
CheckSafetyState();
if ((!boolGantryDown) && (!boolLinActMin) && (!boolStop) && (boolSafety)) {
digitalWrite(relLinAct1Pin, LOW);
digitalWrite(relLinAct2Pin, HIGH);
while ((!boolGantryDown) && (!boolLinActMin) && (!boolStop) && (boolSafety)) {
CheckGantryLimitState();
CheckLinActMinState();
CheckSafetyState();
CheckStopState();
}
GantryStop();
}
}
void GantryUp() {
CheckGantryLimitState();
CheckStopState();
CheckSafetyState();
if ((!boolGantryUp) && (!boolStop) && (boolSafety)) {
digitalWrite(relLinAct1Pin, HIGH);
digitalWrite(relLinAct2Pin, LOW);
while ((!boolGantryUp) && (!boolStop) && (boolSafety)) {
CheckGantryLimitState();
CheckStopState();
CheckSafetyState();
}
GantryStop();
}
}
void CheckLinActMinState() {
intLinActMinState = digitalRead(swtLinActMinPin); // HIGH for an open door
if (intLinActMinState == LOW) { // The minimum has been reached
boolLinActMin = true;
}
else { // The minimum has not been reached
boolLinActMin = false;
}
}
void CheckGantryLimitState() {
// This is an anolog input so we are looking for a range of values
int GantryLimreading = analogRead(swtUpperLowerLimitPin);
if (GantryLimreading >= 10 && GantryLimreading <= 35) { // the upper limit has been reached
boolGantryUp = true;
boolGantryDown = false;
}
else if (GantryLimreading >= 150 && GantryLimreading <= 285) { // the lower limit has been reached
boolGantryUp = false;
boolGantryDown = true;
}
else {
boolGantryDown = false;
boolGantryUp = false;
}
}
void GantryStop() {
digitalWrite(relLinAct1Pin, LOW);
digitalWrite(relLinAct2Pin, LOW);
}
void TampBoxClose() {
CheckTampBoxLimitState();
CheckStopState();
CheckSafetyState();
if ((!boolTampBoxClose) && (!boolStop) && (boolSafety)) {
digitalWrite(stepTampBoxEnablePin, LOW);
digitalWrite(stepTampBoxDirPin, LOW);
}
while ((!boolTampBoxClose) && (!boolStop) && (boolSafety)) {
digitalWrite(stepTampBoxStepPin, HIGH);
delayMicroseconds(intTampBoxPulseDelay);
digitalWrite(stepTampBoxStepPin, LOW);
delayMicroseconds(intTampBoxPulseDelay);
CheckTampBoxLimitState();
CheckStopState();
CheckSafetyState();
}
digitalWrite(stepTampBoxEnablePin, HIGH);
}
void TampBoxOpen() {
CheckTampBoxLimitState();
CheckStopState();
CheckSafetyState();
if ((!boolTampBoxOpen) && (!boolStop) && (boolSafety)) {
digitalWrite(stepTampBoxEnablePin, LOW);
digitalWrite(stepTampBoxDirPin, HIGH);
}
while ((!boolTampBoxOpen) && (!boolStop) && (boolSafety)) {
digitalWrite(stepTampBoxStepPin, HIGH);
delayMicroseconds(intTampBoxPulseDelay);
digitalWrite(stepTampBoxStepPin, LOW);
delayMicroseconds(intTampBoxPulseDelay);
CheckTampBoxLimitState();
CheckStopState();
CheckSafetyState();
}
digitalWrite(stepTampBoxEnablePin, HIGH);
}
void CheckTampBoxLimitState() {
// This is an anolog input so we are looking for a range of values
int tbreading = analogRead(swtTampBoxLimitPin);
if (tbreading >= 150 && tbreading <= 400) {
boolTampBoxOpen = true;
boolTampBoxClose = false;
}
else if (tbreading >= 10 && tbreading <= 40) {
boolTampBoxOpen = false;
boolTampBoxClose = true;
}
else {
boolTampBoxOpen = false;
boolTampBoxClose = false;
}
}
void AirValve(int intOpenClose) {
if (intOpenClose == 0) {
digitalWrite(swtAirValvePin, LOW); // closes air valve
}
else {
digitalWrite(swtAirValvePin, HIGH); // opens air valve
}
}