Hello,
I have built a machine that has a linear belt driven by a stepper motor, it has limit switches at both ends, and two buttons that drive an object on the belt towards or away from each of the limit switches. If written the following code to essentially disable the home or run buttons depending on if the limit switches are HIGH or LOW. This code works exactly as I would expect it to.
/*
Revision Notes
1.2 - First working demonstration code
1.3 - Added linear actuator aka finger to hardware
1.4 - Added code to control finger
*/
#include <Stepper.h>
const int homeButtonPin = 5;
const int runButtonPin = 6;
const int homeLimitSwitch = 2;
const int endLimitSwitch = 3;
const int ledPin = 13;
const int stepsPerRevolution = 600;
const int fingerUp = 1; //rev 1.3
const int fingerDown = 0; //rev 1.3
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int homeButtonState = 0;
int runButtonState = 0;
int homeLimitSwitchState = 0;
int endLimitSwitchState = 0;
int lastCheck = 1;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(homeButtonPin, INPUT);
pinMode(runButtonPin, INPUT);
pinMode(homeLimitSwitch, INPUT);
pinMode(endLimitSwitch, INPUT);
pinMode(fingerUp, OUTPUT); //rev 1.3
pinMode(fingerDown, OUTPUT); //rev 1.3
}
void loop() {
// read the state of the pushbutton value:
homeButtonState = digitalRead(homeButtonPin);
runButtonState = digitalRead(runButtonPin);
homeLimitSwitchState = digitalRead(homeLimitSwitch);
endLimitSwitchState = digitalRead(endLimitSwitch);
int speedPot = analogRead(A5); // Added 11:00 1/2/2022
int speedSetting = map(speedPot, 0 , 1023, 100, 600); //rev 1.3 - 600 was 1000
myStepper.setSpeed(speedSetting); // Added 11:00 1/2/2022
if (homeButtonState == HIGH && homeLimitSwitchState == LOW)
{
myStepper.step(-stepsPerRevolution / 100); //1.4 reinstated /100
digitalWrite(ledPin, HIGH);
}
if (homeLimitSwitchState == HIGH)
{
myStepper.step(0);
digitalWrite(ledPin, HIGH);
}
if (((homeLimitSwitchState == HIGH || homeLimitSwitchState == LOW) && endLimitSwitchState == HIGH) && runButtonState == HIGH)
{
//This is where the code should be added
myStepper.step(stepsPerRevolution / 100); //1.4 reinstated /100
}
if (endLimitSwitchState == HIGH && runButtonState == HIGH);
{
myStepper.step(0);
}
}
I am adding another feature to the code that is driven by the same runButton. This code moves a linear actuator (aka finger) for a set amount of time, holds that position while runButton is held else it retracts the linear actuator. This code also works exactly like I would expect it to.
int fingerDown = 0;
int fingerUp = 1;
int runButtonPin = 6;
int runButtonState = 0;
int lastCheck = 1;
void setup()
{
pinMode(fingerDown, OUTPUT);
pinMode(fingerUp, OUTPUT);
}
void loop()
{
runButtonState = digitalRead(runButtonPin);
if (runButtonState == HIGH)
{
if (lastCheck == 1)
{
analogWrite(fingerUp, 0);
analogWrite(fingerDown, 255);
delay(600); //Time the motor runs forward
analogWrite(fingerUp, 0);
analogWrite(fingerDown, 0);
}
lastCheck = 0;
}
else
{
lastCheck = 1;
analogWrite(fingerDown, 0);
analogWrite(fingerUp, 255);
}
}
My challenge is this, when I try to add the "nested" IF/ELSE of the second code to the first code I get wildly unexpected results. Any help on how to have the second code work with the first would be greatly appreciated. The first code has a comment for where I would like to add it.
Please let me know what additional details I could provide to be more helpful.