Good evening, everyone. I am fairly new to the Arduino scene. I have started to code my Uno to use on my train layout. I have it where it will activate a step motor that will be used to raise a pair of arms for a railroad crossing using a proximity sensor. So far, I have success in making this work. The proximity sensor activates the step motor, which runs, then holds for 5 seconds, as coded. What I am wanting to do is to use that signal from the proximity sensor to activate a custom railroad crossing cross buck with lights. I also want the step motor to reverse direction to close the arms after the 5 second hold. I have already tried coding the motor with negative steps, but it will not turn. I'm trying to connect the lights using a stripped USB cable. When I connect the lights to 5V and Ground, they work fine, but they are always on. I want a way to turn them on with the motor. Thanks
// Arduino stepper motor control code
#include <Stepper.h>
// Include the header file
// change this to the number of steps on your motor
#define STEPS 32
// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);
int val = 0;
int LED = 7;
int LDR = A0;
void setup()
{
Serial.begin(9600);
stepper.setSpeed(500);
pinMode(LED, OUTPUT);
pinMode(LDR, INPUT);
}
void loop()
{
int LDRValue = analogRead(LDR);
Serial.print("sensor = ");
if (LDRValue <=100)
{
digitalWrite(LED, HIGH);
val = Serial.parseInt();
stepper.step(1000);
delay(5000);
Serial.println(val);
//for debugging
}
else
{
digitalWrite(LED, HIGH);
}
}
Last night I tried using “ stepper.step(-1000);” after the delay, but it didn’t do anything but vibrate the motor. The first stepper command works great. The motor turns, then does the delay.
Okey, You've investigated that.
Personally I use Accelstepper and have no experience of the Stepper library.
It sounds strange. Lots of people use stepper.h. Search for Arduino + stepper.h and see what that gives You. There must be a lot. THere is.
I’ve been using the ULN2003 that came with my step motor as the driver. I really want to keep the proximity sensors as my input. I’d like to run 2 outputs without having to use a breadboard.
Step motor
Crossing lights
Any advice on getting the step motor to run in reverse direction after the cycle? I still can’t figure that out.