Creation of a Routine with a Button that triggers relays and 2 stepper motors. Arduino (UNO)

Hey guys,
I don't have the ability to program in arduino, however, I'm doing a project that will need an arduino (UNO or other) to perform some tasks.
Well, I studied a little how the arduino works and I will explain how my project routine will be.

(if not possible with this Arduino Uno) which one should I use?)

Please, if you can help me with the programming, I would be very grateful.

Routine:
I need that:

Press button = pin 2 (1st time)
Connect relay = pin 3

Press button = pin 2 (2nd time)
Turn off relay = pin 3

Press button = pin 2 (3rd time)
Connect relay = pin 4
Turn on stepper motor 1 = Pin 6
pin 7
pin 8
pin 9
For 5s clockwise 1/4 turn
After 10s,
Turn on stepper motor 2 = Pin 10
pin 11
pin 12
pin 13
For 5s clockwise 1/4 turn
After 5s,
Turn on stepper motor 2 = Pin 10
pin 11
pin 12
pin 13
For 5s counterclockwise 1/4 turn
After,
Connect relay = pin 3
for 5s
And pause the process.

Press button = pin 2 (4th time)
Connect relay = pin 4
Connect relay = pin 5

After 10s,
Turn on stepper motor 2 = Pin 10
pin 11
pin 12
pin 13
For 5s clockwise 1/4 turn
After 20s,
Turn off relay = pin 4
Turn off relay = pin 5
Turn on stepper motor 1 = Pin 10
pin 11
pin 12
pin 13
For 5s counterclockwise 1/4 turn
Turn on stepper motor 2 = Pin 10
pin 11
pin 12
pin 13
For 5s counterclockwise 1/4 turn

RESET
Press the button = Pin 2
for 3s

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Project Guidance category

The IDE (integrated Development Environment) is free for the downloading. With this you can write code and debut your Arduino as well as program it. There are a lot of tutorials etc on line top get you started. Start by getting a copy of the Arduino Cookbook, it will be the best tool you have for a long time. You will find most of the code you need in that in various sections.

hello gilshitz, I've been studying since the beginning of June. I'm having some difficulties. I need this schedule for the end of July and that's why I asked for help from the forum staff. although I have read some tutorials, I confess that anxiety to finish the project has been disturbing a lot. anyway, thanks a lot for helping.

How can you have 2 steppers on the same pins but controlled independently?

To count button presses consult the state change detection tutorial and my, companion tutorial, state change detection for active low inputs.

It would be easier to structure your program using a finite state machine.

For controlling stepper motors I like the MobaTools stepper library. Unlike the Stepper library the Mobatools movement functions do not block and the movement functions use acceleration enabling higher speeds. The MobaTools library is available for install via the IDE library manager.

To do timing, do not use the blocking delay() function. Use millis() or micros() to do timing ala the blink without delay example.
Some non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.

If you have written code that you want help with post the code. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

It would help if you could post a schematic or wiring diagram if you have one. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Hi fungo de solo (portugues?) Im brazilian

I will Do like you teach me.

many thanks!

Arizona, USA.

You are welcome. Any questions, just ask.

You’ve reached a point in your programming life, where you need to decide - “do I want to learn and do it myself”, or do I want to partner with someone else and possibly pay for skills I don’t have.

The project isn’t incredibly hard, it requires several disciplines to realise, not a good first project in a hurry.

You’ve got what you need above, but now’s the time to make some hard choices.

I assume

  Press button = pin 2

means there is a button on pin 2 to be used as an input.

  Connect relay = pin 3

means energize the relay driven by an output on pin 3

and that

 Turn on stepper motor…   For 5s clockwise 1/4 turn
 After 10s,

takes 15 seconds, 5 to rotate then 10 to just hang out resting.

Is that all correct?


In the step caused by the fourth press of a button on pin 2, relays 4 and 5 are to be connected, by which I assume you mean energized.

But relay 4 is already connected.

What is the roll of RESET long press on button at pin 2? You don't list any reset positions or connected/off relays. It looks like you need to press that to "reset" before pressing means the first prsss at the top of your recipe.

TBH coding for that long press is the hardest thing you are trying to accomplish. I suggest you drop the long press, whether you drop the extra button press required at this stage or not.

Whatever you program, it should be flexible enough to easily tolerate slight adjustments and corrections to your process.

When you haven't said, does the button do anything? Is there no requirement for any control over those parts that run on time without regard for any checking anything else? Like a STOP button or whatever.

Which you might say a few things about which. It may be that you are doing something we'd all recognize if it weren't just a low level set of arbitrary steps.

HTH

a7

Hi alto777,

thanks for your help . I will send you a video showing how the programming routine is.

I think you mean to share using google drive. I tried to look. I'd need an account, and it may need you to make those things publlic. In any case, ppl prefer to not go off site for information. Perhaps you could put the video on youtube and share a link. Or use your words. Videos are rarely as useful as those who go to the trouble to make them think they are going to be. In any case, for this project it really is just getting all your steps making sense.

Here's some code in the simulator. If you read it, you will see that it does not follow your steps, but does have all the elements you need to assemble. In the right order and so forth.

It is a very literal and def noob-style sketch. It may give you all you need. It may or may not be a good starting point for the (inevitable?) enhancements and feature creep that visits all projects sooner or later. For example - I'm adding a "Done" LED that comes on before you have to hit the button RESET. Which restarts it?

Try it here. I put 18 RPM as the speed, I got tired of the 5 seconds it takes to go 1/4 revolution at 3 RPM, but that's the number to change to for your rate.

Wokwi_badge crude motor relay control demo

// https://wokwi.com/projects/369635724513089537
// https://forum.arduino.cc/t/creation-of-a-routine-with-a-button-that-triggers-relays-and-2-stepper-motors-arduino-uno/1145630

#include <Stepper.h>

const int stepsPerRevolution = 200;  // adjust for your motor
const int PRESST = LOW;

// initialize two steppers
Stepper motor1(stepsPerRevolution, 6, 7, 8, 9);
Stepper motor2(stepsPerRevolution, 10, 11, 12, 13);

void setup() {
  Serial.begin(115200);
  Serial.println("Whirled Peas!\n");    // xprintf works like printf. no floats

  // set the speed at 3 rpm: (20 seconds / revolution)
  motor1.setSpeed(18);   // 18 takes 5/6 second. life too short!
  motor2.setSpeed(18);

  pinMode(2, INPUT_PULLUP);
}

void loop() {
  motor1.step(50);
  motor2.step(-50);

  waitOnButton();

  motor1.step(-50);
  motor2.step(50);

  waitOnButton();
  relayControl(3, HIGH);
  
  waitOnButton();
  relayControl(4, HIGH);

  waitOnButton();
  relayControl(3, LOW);
  delay(1000);
  relayControl(4, LOW);
  delay(1000);
  relayControl(5, HIGH);
  delay(1000);
  relayControl(5, LOW);  

  waitOnButton();
}

void waitOnButton()
{
  Serial.println("waiting on the button");
  do delay(15); while (digitalRead(2) != PRESST);
  while (digitalRead(2) == PRESST) delay(15);
  delay(15);
}

void relayControl(unsigned char thePin, unsigned char theValue)
{
  digitalWrite(thePin, theValue);
}

HTH

a7

Hello 777, thank you very much for the help. I applied it to my arduino uno, however, it didn't work, do I need to install some library?

It sounds like you mean it did not compile.

Say more. Post the first dozen lines of the error message you get.

My sketch includes the Stepper library. I'm guessing that is what you did not do - install the Stepper library in the IDE.

# include <Stepper.h>

Which the sketch uses. Anything you write for the UNO might need that library, although you can do all the motor stuff directly. If you want to make a side career out of it. :wink:

You can modify the simulation, you get your own copy when you do. You could experiment freely in the simulation environment, easier to get the logic all perfect with no worries about wires and power supplies and so forth.

HTH

a7

thank you 777 for your dedication. I tested it on the simulator and it worked. but when I compile it doesn't work, not even the relays. I'm going to break my head and try to get this resolved.

OK compiles and behaves differently than the simulation.

So now it is probably a hardware (wiring!) issue. Post a schematic, hand drawn take a picture and post it is fine, easiest really.

Be sure to show how you wired all the parts with attention to power - where it comes from and how it gets where it needs to go.

Note also I use a push button wired in the pull-up configuration and I use the internal pullup resistor. So that's why my switch is wired between the input and GND, and why the code expects the pin to read LOW when then button is presst.

It's prolly something stupid simple that you've overlooked.

a7

1 Like

I will do like you teach me and I show by image.
Thank a lot..

@sergiohenriq also created a duplicate topic in the PortuguĂŞs category, where there is active parallel discussion:

@sergiohenriq In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Thanks in advance for your cooperation.

Obrigado pela dica.

Não foi minha intenção.

Tomaria cuidado para nĂŁo faze-lo novamente