Motor reverse ideas

Brand new, go easy. For a Christmas light project i built a prop to move back/forth along homemade tracks. I purchased Arduino Uno and a motor control board, 12v windshield wiper motor, 12v car battery and watched a youtube video to code the board. It works great. Moves in each direction a specific time and pauses before moving again. I'd like to rely on a trigger or switch once the prop gets to the end of the track, instead of a specific running time. Is there an easy way to accomplish this? Mechanically connecting a trigger/switch isn't an issue for me, but learning a new code and how to electronically connect it to the Arduino...intimidates me. It was a steep learning curve already.

If you use a "button" (or "debounce") library, all the hard coding is done by the author of the library.

All you need to do this is (1) install the library (click the link... and we can help), (2) try an example, then (3) copy-paste from an example sketch (also written by the author) into your sketch.

There are many button/debounce libraries. I did a simple search on "arduino button library" and this was the first link: GitHub - madleech/Button: Button is a tiny library to make reading buttons very simple. It handles debouncing automatically, and monitoring of state. · GitHub

At the bottom of the README page is an example. The example shows (1) identifying the button (or buttons), (2) what "trigger" you want ... pressed, released, toggled (press and release).

AND... as you add this to your project, ask any question.

+1.

The madleech/Button libraty is indeed very simple, easy to use and... it does not suck.

That's my highest endorsement for a button library; I can say that many button libraries fail to hit all three criterions.

The only thing I would point out is that the debounce parameter default is quite large, viz:

	Button(uint8_t pin, uint16_t debounce_ms = 100);

The debounce time does not affect response time, it only limits the speed at which the library will recognize separate button presses. For a limit switch, this shouldn't matter too much, even at 100 milliseconds.

To change it, just add the your debounce time to the constructor:

Button button3(4, 20);  // a button called button3 on pin 4 with a 20 ms debounce

The code to move until, pause, move the other way until and repeat is fairly simple. If that is literally all you need to accomplish, coding it should not need any really advanced language features or programming skilz.

a7

The example of the README page makes sense, i think i understand what the code is doing. Do you have a sketch, or can you tell me how to pin the motor to the arduino, and the button/trigger to the arduino? I'm also using a motor control board.

See Post #3. Or (and) see the Arduino page: https://docs.arduino.cc/built-in-examples/digital/Button/ (also, described in one paragraph in the README for the previous button library)

Please. Do not wire "a motor" to "an arduino." Use a motor driver. Please.

Start with your hand drawing of your project. Add all the specifications you can find about the motor, the power, the weight of the object, and everything I missed. That will get a lot of very good answers.

Literally all i want to do is move a prop appx 50' in each direction. Back and forth, for a couple of hours each night during Christmas season. With a small pause at each end.

This will be using code called a "state machine" (or a library for it) that will allow different devices (switches, driver-power) to be added and removed from reading their states. For example, you start in "state zero", and once you trip one switch (state one), you can not keep reading that switch as a "stop" switch until you have reversed motor driver direction (state two) and waited until a timer has finished (state three) and enabled the motor driver (state zero, but in the negative direction).

There are electro-mechanical methods available (no code, no Arduino). Limit switches, resetable timer relays, double-pole-double-throw relays... and probably things I missed.


This is my exact setup and code. The prop i'm moving is probably 20 pds.

You had me at:

image

Sending out the batsignal: @hallowed31

LOL, i deleted that from my code! Not moving no spiders!

That's exactly what a spider would say. (@hallowed31 is a all-halloween-project maker)

This sounds like something model railroaders use all the time... perhaps @Railroader can add insight and advice.

There is also @MicroBahner who does everything motors, timing and buttons.

Hope to hear from them. My current setup works, but i don't want to rely on time. I want it to push a button or trip a trigger.

It could.

Or it could be simple code that just literally has the steps laid out. For this you don't even need true button handling, either in your code or from a library.

repeat until New Year's Eve
   while it's going one way
      wait for it to get there
      pause
      reverse directions 

  while it's going the other way
      wait for it to get there
      pause
      reverse directions 

Basically you are only looking for the button you are heading towards.

Even simpler:

run until you hit a switch
pause
reverse 

@sawyerbrownlive - up your game here. Try posting the code you have been working with in code tags.

You can use the <CODE/> button you see in the message composition window toolbar, and paste your code over where it is telling you to do:

type or paste code here

It will let us read, copy and run your code, and it will keep things on the code from being interpreted as if they were your prose,

a7

Very true, and very maintainable... As long as Christmas isn't ending in fire and New Years can be happy.

I'm at the office, the code is on my home PC. The picture i posted above is what i did. I used that video to learn a tiny bit about arduino and coding. I changed the times that she had in her video, but everything else is the same. And yea, Run until i hit a switch, pause, reverse, repeat. That's all i want. And a simple schematic of how to wire/pin the arduino-motor controller-switches.

Do you have an idea yet how the travel limit will be sensed?

Some options: mechanical limit switch, inductive (metal sensing) proximity, infrared, thru-beam (one sender, one receiver).

Mechanical limit switch most likely.

The limit switches can be connected between an Arduino input pin and the arduino ground.

In place of the delay() functions, simply loop until the limit switch is activated.

const int limitSwitch1pin = 4;
const int limitSwitch2pin = 5;

void setup() {
  ...
  pinMode(limitSwitch1pin, INPUT_PULLUP);
  pinMode(limitSwitch2pin, INPUT_PULLUP);
  ...
}

void loop() {
  ...
  // replacing delay(5000);
  while (digitalRead(limitSwitch1pin) == HIGH);
  ...
  // replacing delay(3000);
  while (digitalRead(limitSwitch2pin) == HIGH);
  ...
}

What distance will one switch have to be away from the other switch and the microprocessor they connect to?

TBH that sounds like more of a challenge than the code.

a7

The cart is 3' long. So if i put a switch on each end then they are 3' apart.