I am new to coding in general, and I am mainly self taught. I am using a bipolar stepper motor, with two limit switches and an external device (analytical instrument) that provides TTL outputs. Originally, I had working code where when a limit switch was activated, it could change the direction of the stepper motor. I quickly learned that the stepper motor rate isn’t precise enough to stay in time with the analytical instrument. I’m trying to have the two devices communicate (the analytical instrument has strict software), so the analytical instrument can keep time and tell the stepper motor when to change direction. The instrument can only provide TTL ouput, so I have wired a pin that can receive the signal. The signal from the instrument is normal edge, so it’s constant +5V and sends a 0v signal.
When I use an if loop based on whether the limit switches are active, this controls the direction of the stepper motor very well. Now, I’m trying to adapt the code so the limit switches are for safety, so they can stop the motor from turning if either one is activated, and instead use the TTL 0v from the instrument to control the stepper motor direction. The code compiles. It’s only when I uploaded the code below, the stepper motor doesn’t respond or turn at all. The code has been able to control the stepper motor before, it’s only when I added the bolded section that the stepper ceased to respond to the stop/start commands.
Any feedback is greatly appreciated!
//MOTOR SETUP
const int MotorSpeedFW = 14; // for control over injecting speed
const int MotorSpeedRW = 1; // for control over sampling speed
int speedFW = 2000;
int speedRW = 2000;
int MotorStartStop = 0;
int direction_set; // no initial value for motor direction FW=1 RW=0
//PIN SETUP
const int dirPin1 = 4; //DIG PIN 4
const int stepperPin1 = 5; //DIG PIN 5
const int dirPin2 = 7; //DIG PIN 7
const int stepperPin2 = 6; //DIG PIN 6
const int LimitSwitchTOP = 2; // neg. direction sensor limit pin
const int LimitSwitchBOTTOM = 3; // pos. direction sensor limit pin
const int TTLin = 8; // DIG PIN 8 for TTLout-TTLin on IC
// BLINK SETUP - Millis value when the data was last sent.
long LastSent;
long LastBlink = 0; // Time we last blinked the LED
int OnTime = 10; // Amount of time the LED remains on [milliseconds]
int OffTime = 100; // Amount of time the LED remains off [milliseconds]
const int LEDPin = 13; // Pin the LED is attached to
void setup()
{
Serial.begin(9600);
Serial.println(F("Blink 2.0"));
Serial.println(F("=========="));
//BLINK SETUP
LastSent = millis();
//MOTOR SETUP
pinMode(dirPin1, OUTPUT);
pinMode(stepperPin1, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(stepperPin2, OUTPUT);
pinMode(TTLin, INPUT);
pinMode(LimitSwitchTOP, INPUT);
pinMode(LimitSwitchBOTTOM, INPUT);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
}//END OF SETUP
//MOTOR FUNCTION
void stepFW () //STEP FW Function
{
if (direction_set == 1 ) {
digitalWrite(dirPin2,LOW);
digitalWrite(dirPin1,LOW);
for(int j=0;j<=MotorSpeedFW;j++){
digitalWrite(stepperPin1,LOW);
digitalWrite(stepperPin2,LOW);
delayMicroseconds(speedFW);
digitalWrite(stepperPin1,HIGH);
digitalWrite(stepperPin2,HIGH);
//delay(1);
}
}
}
void stepRW () //STEP RW Function
{
if (direction_set == 0 ) {
digitalWrite(dirPin2,HIGH);
digitalWrite(dirPin1,HIGH);
for(int j=0;j<=MotorSpeedRW;j++){
digitalWrite(stepperPin1,LOW);
digitalWrite(stepperPin2,LOW);
delayMicroseconds(speedRW);
digitalWrite(stepperPin1,HIGH);
digitalWrite(stepperPin2,HIGH);
delay(50);
}
}
}
void loop()
{
SerialCommandHandler.Process();
//SETUP MOTOR SPEED AND LIMIT SWITCHES
speedFW = (2000 - (2000*MotorSpeedFW/100)); //for control over sampling speed
speedRW = (2000 - (2000*MotorSpeedRW/100)); //for control over injecting speed
int toplimitSwitch = digitalRead(LimitSwitchTOP);
int bottomlimitSwitch = digitalRead(LimitSwitchBOTTOM);
int IC_signal = digitalRead(TTLin);
[b][b] if (toplimitSwitch == HIGH && bottomlimitSwitch == HIGH){
MotorStartStop = 0;
}
else {
Panel.SetText("lst1", "OFF");
Panel.SetText("lsd1", "OFF");
}
if (IC_signal == LOW){
switch (direction_set){
case 0:
direction_set = 1;
break;
case 1:
direction_set = 0;
break;
}
}
[/b]
//SETUP MOTOR
if ( MotorStartStop == 1 ) {
if (direction_set == 0 ) {
Panel.SetText("direction_set", "0");
Panel.SetText("direction_read", "Collecting Sample");
stepRW();
} else if (direction_set == 1 )
{
Panel.SetText("direction_set", "1");
Panel.SetText("direction_read", "Injecting Sample");
stepFW();
}
else {
Panel.SetText("direction_read", "Motor Stopped");
delayMicroseconds(1);
}
}
}//END OF PROGRAM