I started this project some weeks ago, posted about it here and got some feedback that seemed to say "go away, tidy up your sketch and think more about what you want it to do".
I have done exactly that but as I cannot find the original thread I am starting again.
The project is a slide driven back and forth by a stepper motor, there is a changeover microswitch at each end of the travel and a single pole changeover switch to restart in the opposite direction.
I am using 4 pins to sense and control the direction of travel, and have a working stepper motor setup.
Attached below a schematic of the wiring of the whole setup and a diagram of the switch connections with a logic grid showing which pins are high (black blob) through the phases of movement.
The switching side of things has been tested with LED's in all 4 pins and it follows the expected logic.
The motor runs in both directions using the restart switch.
I do not have the belt drive connected at present, this is a good thing and here is why!
The motor does not stop as expected when the carriage is moved to the end of the slide rails, my sketch is meant to look for a low signal on pin 8 or 9 whilst either 11 or 12 are high (other pin will be low) and stop the motor, until the switch is thrown and the motor restarts in the other direction.
// constants won't change. They're used here to set pin numbers:
const int ForwardPin = 12; // the number of the pin that when high sends slide forward
const int StopForwardPin = 8; // the number of the pin that stops motor when forward
const int BackwardPin = 11; // the number of the pin that when high sends slide backward
const int StopBackwardPin = 9; // the number of the pin that stops the motor when backward
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
// variables will change:
boolean ForwardState = 0; // variable for reading the micro-switch status
boolean StopForwardState = 0; // variable that stops motor when forward
boolean BackwardState = 0; // variable for reading the microswitch status
boolean StopBackwardState = 0; //variable that stops the motor when backward
int slidePosition = 2; // Tells progam what position the slide is in
/**
setup inputs for switches and outs for motor pins
serial begin to read the switches to test for errors
*/
void setup() {
// initialize the pin as an inputs:
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
// initialize the switch pins as inputs:
pinMode(ForwardPin, INPUT);
pinMode(StopForwardPin, INPUT);
pinMode(BackwardPin, INPUT);
pinMode(StopBackwardPin, INPUT);
Serial.begin(9600);
}
/**
this function turns motor foward
*/
void SlideForward() {
// turn motor foward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/*turns the motor 1 step*/
for (int i = 0; i < 1; i++)
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
/**
this function turns motor backwards
*/
void SlideBackward() {
digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
/*Turns the motor fast 1 step*/
for (int i = 0; i < 1; i++)
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
/**
Stop the motor form moving
*/
void stopSlideFromMoving() {
digitalWrite(smStepPin, LOW);
}
void loop() {
// read the state of the switches value:
ForwardState = digitalRead(ForwardPin);
StopForwardState = digitalRead(StopForwardPin);
BackwardState = digitalRead(BackwardPin);
StopBackwardState = digitalRead(StopBackwardPin);
// check which pin 11 or 12 is HIGH:
if (ForwardState == HIGH,
StopForwardState == HIGH,
BackwardState == LOW) {
SlideForward();
} else if (ForwardState == HIGH,
StopForwardState == LOW,
BackwardState == LOW) {
stopSlideFromMoving();
}
if (BackwardState == HIGH,
StopBackwardState == HIGH,
ForwardState == LOW) {
SlideBackward();
} else if (BackwardState == HIGH,
StopBackwardState == LOW,
ForwardState == LOW) {
stopSlideFromMoving();
}
}
Here is my sketch, can anyone point me in the right direction to get it running please?