Random Control of Motor

Hello everyone, very first post. I am not in any way a programmer/coder/electrician so bear with me on this.

I have a 12v motor that I want to have randomly turn on and off from every 5-20 seconds in the same direction running for about 10 seconds then shutting off. Right now it’s connected to a power source and a button switch that when you press it the motor turns on, press it again the motor switches directions, press it a third time and it shuts off. It’s from an old back massager thing that I took apart.

I really have no idea where to begin on this and would really appreciate help. I am very mechanical but have nothing on the electrical side.

Thank you in advance

Often with projects like this one has to make an initial decision as to how they're going to approach it.

From where I'm sitting the two most promising are possibly:

  1. Find / tap-in-to / discover the part of the existing motor and circuit that does the actual switching of power to the motor and then simply get an Arduino to close the contacts on a relay at the prescribed intervals (probably the most eloquent, but would require more work), or

  2. Get an Arduino to close/open a relay that's connected to the existing switch once when you want to start it and then twice when you want to stop it (simple to do but has the potential for things to get out of sync - so although it would probably work, it's less "eloquent" in that you'd need to allow for someone / something introducing a random button press thus making it do all the wrong things when the Arduino sent it all the right commands).

There may well be other solutions (such as using a triac or relay to control the motor directly (at which point motor loads vs the specs of the controlling device would need to be taken into account).

Which approach has the most appeal?

From what you've said the actual code would be trivial to write;

  • generate a random number between 2 values ->

  • Use that to generate a delay that long ->

  • Start a 20 second timer ->

  • Turn the motor on ->

  • Wait for the timer to get to 20 seconds ->

  • Turn the motor off ->

  • Rinse and repeat.

Does that help?

void loop()
{
    delay(random(5000, 20000));  // 5 to 20 seconds
    digitalWrite(MotorPin, HIGH);
    delay(random(9000, 11000));  // About 10 seconds
    digitalWrite(MotorPin, LOW);
}
1 Like

I'm still in awe of how brutally simple Arduino code can be.

Ok you guys are like 5 steps ahead of me on this. I really appreciate the info and time you took to help me out. I’m still trying to figure out what I need to buy. If I go on Amazon can I just get an Arduino starter kit and do what I want to do or do I need something more specialized? Your info is great, I’m just a few steps from even being at the point of coding stuff

The starter kit (assuming that we're talking about the official one) is SUPERB; It's a tour-de-force in teaching so many things with what seem to be silly (but are actually very powerful) examples. For me, a defining hallmark of Arduino was "I thought it was going to be hard ... until I discovered just how easy most things are"; so rather than "give you a fish and feed you for a day" it very much "teaches you how to fish and allows you to feed yourself for a lifetime" ... so a bit more work, but an incredibly rewarding journey.

So regardless of whether it helps with your specific project, there's tremendous value in getting the starter kit anyway.

I think if you take a week or two to get the kit and work through the examples that interest you you'll come back here a changed person; you'll probably still have the training wheels on the bike - but at least you'll be riding the bike - and it'll help you relate no end to advice that all the other bike riders give; and there are some hellishly good bike riders here. There are also some hellishly cheap bikes that people can buy from some sources. At the end of the day if just comes down to gaining hard-earned experience; it's a wonderful journey if you enjoy that kind of thing (and obviously many of us do). Probably the biggest danger is that you start finding more and more things to do with Arduino and end up falling down the rabbit hole (I've already made an electric blanket controller, grip heater controller for my motorbike, got an Arduino to pop in an 8 digit random number at the cursor position at the press of a switch ... with more projects in the pipeline ... and I've only been into it for a few months!)