Old and New....

Hello All

Firstly, Old- as I’m late 50s, secondly new as I’ve only just joined!.
Whilst having electronics as a hobby for over 30 years ( on and off ) I thought I’d buy an Arduino ( clone) and Motor shield as I thought a microprocessor would do what I wanted and I have never been involved in this field.
I’ve been playing with it using the numerous example sketches on the net for the last week…but I’m still struggling to understand the basic code.
This what I was requiring to do:-

A DC motor ( say M1) is to be run one way for (n) seconds , be stopped by an external input to a pin, then run back again for the same (n) seconds after a time delay and stopped by the same input when it reaches the other end …the input is to stop an over run of the motored object.
I wanted this to only happen so many times…say for eg 20 times. Whilst this is going on I wanted a geared stepper motor on a time delay to start …say 30 minutes in from the programme start and run very slowly in one direction ( doing what it’s doing for about 15 mins) then when it finishes run back quickly to it’s start position…this stepper motor only needs to do this once per program run.
I’m sure this is all possible and is probably simple…but the code and especially any combining of different codes has made me put the lot away for the time being…can someone confirm that this can be done.

Thanks
clives

Welcome to the forum.

We get many newbies wanting us to supply a program to do many things. Our response usually is to ask what they have done towards their program.

Have you read the learning information available on the site? If you have, you should be able to write a basic program to do some of what you want. You should be able to read a button and drive the motor back and forth.

Then you can put that into a count loop to do it a number of times.

When you have these basics in a program, post the program and ask for assistance on any problems you have getting it to do as you wish and how to get the next stage working.

Weedpharma

Yes, it can be done. :wink:

It could be simpler (out of a mechanical view) to use two switches for the endstops.
(Using only one switch would force you having that on the moving part, wouldn't it?)

To get the flow of events/actions on a timed schedule,
I would suggest that you some kind of task scheduler.

For instance: GitHub - arkhipenko/TaskScheduler: Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers

You need to write down a list of the stages in your project - one on each line so that you can see the sequence of events clearly.

Have a look at Planning and Implementing a Program which tries to illustrate the process of developing a program. It also shows how timing can be managed with millis(). Personally I think that is much easier than using a scheduler library.

...R

Robin2:
Personally I think that is much easier than using a scheduler library.

If you want to implement more than trivial sequences,
you end up writing something like a task scheduler yourself.

Thanks for all replies.
I got lost with the forum board for a couple of days....couldn't find my original posting......not used to forums either....and never received any emails.
Bought a copy of Arduino for dummies ....not really sure that is going to help much though...as it's basically what is written on the web.
Managed to get motors working from examples in libraries on my motor shield...but tied up with being able to work a timer function. It's ok getting a motor ( both stepper and dc) to work to the sample sketches provided but I would like my arduino...when switched on, to work things to a timer command ..

something like :-
arduino switched on =start
after 5 mins run a DC motor 1 one way then reverse.
after another 12 mins run same motor 1
after another 18 mins run same motor 1
after etc etc...say for 20 times at ( but not always) different time periods.
Then say after an hour into the program run stepper motor1 forward then after so many minutes reverse....whilst the above is still going on.

From what I understand...all this could be put into voidsetup and nothing into voidloop ?.
I'm beginning to wonder if I should have used 555 timer chips to do what I want...I'm ok with them used them for decades......for the £11 it cost me for a clone board and a clone shield...I could have bought about 60 x555s ..but being serious I'm sure the microprocessor will be a better option ...once mastered.

Thanks
Clive

clives:
Thanks for all replies.

Sorry folks…I’ve only just going into the links provided now…thanks.

Clive

Whandall:
Yes, it can be done. :wink:

It could be simpler (out of a mechanical view) to use two switches for the endstops.
(Using only one switch would force you having that on the moving part, wouldn't it?)

To get the flow of events/actions on a timed schedule,
I would suggest that you some kind of task scheduler.

For instance: GitHub - arkhipenko/TaskScheduler: Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers

Thanks,but no need for hardware mechanical stops as I've run the various example sketches where a motor runs one way for 'anytime' and after a delay reverses for 'anytime' ...that isn't the issue for me.....these examples always show things running in void loops...until the end of time...it's a simple 'do a task at a programmed time' I think I need. I've put taskscheduler into the library ....I couldn't even get to grips with libraries until yesterday...took me a week to understand that!.

Cheers
Clive

these examples always show things running in void loops

Please note the name of that function is loop, not void loop.
So what you meant to say was this

From what I understand...all this could be put into setup function and nothing into the loop function ?.

Yes that is correct.

Grumpy_Mike:
Please note the name of that function is loop, not void loop.
So what you meant to say was this
Yes that is correct.

Grumpy_Mike:
Please note the name of that function is loop, not void loop.
So what you meant to say was this
Yes that is correct.

Hello

Yes…probably…just writing it as seen on the sketches.
Here’s a bit of code ( from library examples) I’ve been experimenting with all afternoon…just can’t get a stop command in for after x amount of time. I’ve tried using the ‘delay’ followed by stepper.setspeed (0)…but that doesn’t work…I’m lost after a few hours of trying. The only thing I’ve added so far is the delay before the motor runs…and the motor alterations…so it will run after a delay…and at a speed set …but…

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(64, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {
motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);
delay (10000);
stepper.setSpeed(255); //

}

void loop()
{
stepper.runSpeed();
}

Cheers
Clive

Can you post a link to where your example came from ?

Presumably if you stop the calls to stepper.runSpeed() the motor will stop ?

...R

Robin2:
Can you post a link to where your example came from ?

Presumably if you stop the calls to stepper.runSpeed() the motor will stop ?

...R

Hello

It's one of the examples in

Cheers
Clive

Robin2:
Presumably if you stop the calls to stepper.runSpeed() the motor will stop ?

…R

Hello Robin2

If I stop that then the motor doesn’t run at all. I thought it might be easier for me for this ‘simple’ project ( one stepper, 2x dc motors plus a couple of fading LEDs …going to worry about the LEDs after I’ve got the motors to work ) to just run it all in ‘setup’ …but I notice that this program …as it is …uses 18% of memory.
I can see why there are loop functions.

Cheers
Clive

I've tried using the 'delay' followed by stepper.setspeed (0)..but that doesn't work.

That is because the stepper.setspeed method doesn't actually do anything but set the speed it will go once you give it pulses.

Have you read how to use the AccelStepper library?
My guess is no because you haven't bothered to read how to use this forum.
How to use this forum
Because your post is breaking the rules about posting code.

Grumpy_Mike:
That is because the stepper.setspeed method doesn't actually do anything but set the speed it will go once you give it pulses.

Have you read how to use the AccelStepper library?
My guess is no because you haven't bothered to read how to use this forum.
How to use this forum
Because your post is breaking the rules about posting code.

Grumpy_Mike:
That is because the stepper.setspeed method doesn't actually do anything but set the speed it will go once you give it pulses.

Have you read how to use the AccelStepper library?
My guess is no because you haven't bothered to read how to use this forum.
How to use this forum
Because your post is breaking the rules about posting code.

Sorry hit wrong key. Also sorry about the Forum issues...I'm not used to them.....but as I've been Googling and and looking over Arduino messages for a week....and it hasn't really helped me that much ...I shall unsubscribe.
Thanks anyway.

Cheers
Clive

clives:
It's one of the examples in

GitHub - adafruit/AccelStepper: A small fork of AccelStepper v1.3 with AF_motor (Adafruit motor shield) support!

Which one?
I am not going to read 5 or 6 to find out.

...R

Robin2:
Which one?
I am not going to read 5 or 6 to find out.

...R

My mistake....it's the top of the list ...AFMotor_ConstantSpeed.

Clive

For the benefit of others, here is the code from Github

// And AccelStepper with AFMotor support (https://github.com/adafruit/AccelStepper)
// Public domain!

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(200, 1);


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void setup()
{  
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Stepper test!");
  
   stepper.setSpeed(50); 
}

void loop()
{  
   stepper.runSpeed();
}

I am not familiar with this use of AccelStepper - where it seems to rely on functions - but it seems to be one the options.

At this stage I have lost sight of what you are trying to achieve.

If you want the stepper to move for a given number of steps there are plenty of examples on the AccelStepper website.

If you want the stepper to move for a certain length of time you need to organize your code so that the function stepper.runSpeed(); is only called during the appropriate time period. For example

void loop() {
   if (millis() - startMillis < periodMillis) {
      stepper.runSpeed(); 
   }
}

…R

Robin2:
At this stage I have lost sight of what you are trying to achieve.

If you want the stepper to move for a given number of steps there are plenty of examples on the AccelStepper website.

Hello Robin2

Thanks for your replies and help..but I have already looked at the AccelStepper website and Arduino's etc....and I still think this is too much for me to understand ...just for working some motors. I will scrap the stepper motor idea....I would be better getting a geared dc motor instead...as the Aidadruit motor shield allows up to 4 dc motors ( when no stepper motor) and their dc motor example sketch for running forward/pausing /running backward works for me... i'm never going to be able to incorporate a stepper into the example sketch. I've put the 'simple timer' into my library ,which I found only a few hours ago... I will try to find a way of getting the timer to do these things .....in a time order, one after the other, just in 'setup' and no loops....well, 'try', until my sanity expires :-).

Regards
Clive