Uno Step Motor/Lights Help!

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);

}

}



1 Like

Hello vkng070

Use a servo instead.
That would be more easy to operate.

Have a nice day and enjoy coding in C++.

Check the stepper library for how to change direction.

The file shows using a negative integer would reverse direction, but all it does is make the step motor vibrate.

The code You posted is not trying to make the stepper step backwards, only accesses an LED.

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.

I have done flashing lights for train layouts too.

I used infrared to detect trains.

It is capable of switching points with a driver board to. :wink:

Reply #3 again...

What would I need to do in order to supply 5v to my lights, but only trigger it with the sensor?

The library is what told me to use the negative number on the step command to reverse the rotation, but it did not work.

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.

Arduino stepper.h – Yahoo Sökresultat

Well... I used a Atmega328p and two FC-51 Infared modules.
It has five inputs and 10 inputs(one per sensor).

The detection circuit would be doable in logic if you had flip-flops.

By the way one reason I didn't use an LDR is that you need a light source. :wink:

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.

  1. Step motor
  2. Crossing lights

Any advice on getting the step motor to run in reverse direction after the cycle? I still can’t figure that out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.