Using millis with a L293D Motor Shield

At the risk of getting my head crewed off - is it possible to use the millis function with a L293D Motor Shield? The presets of the AF Motor library make it very confusing (to me at least) to mimic the code in the blink without delay code.
Thank you in advance.

Post the code that you tried. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Tell us what the code actually does and how that is different from what you want.

Which exact motor shield. Post a link to where you got it.

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

1 Like

Posting schematics would help a lot....

1 Like

Thank you for your quick reply. The goal is to run 4 motors simultaneously at different speeds/durations/directions. All the motors are DC but are from 4 different manufactures with different RPM speeds.

The shield is the L203D bout on Amazon

as you'll see, the code is incomplete with serious issues - currently I'm stuck with the
expected initializer before '.' token.

I'm trying to get one motor running before expanding the program to 4

Thank you for your time.
(I pasted the code here from the IDE ,highlighted the code then clicked on the </> button but it's not adding the tags. I also did the cut and paste "copy for forum" which didn't work either. I'm about to throw in the towel)


[code]
#include <AFMotor.h>
//

AF_DCMotor motor4 (4);
int motor4State = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
int motor4.setSpeed (254);
int motor4.run(RELEASE);
int motor4.run(FORWARD);
void setup() {
  AF_DCMotor motor4 (4);

  //turn on motors

}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    if (motor4.run(FORWARD))  {
      motor4.run(RELEASE);

    } else {
      motor4.run(FORWARD);


    }
[/code]
1 Like

Hi Railroader

I put my reply in groundFungus's post.

Thank you for your time and insight - is greatly appreciated
.
G

???

Hi Railroader,

Thank you for your quick reply. The goal is to run 4 motors simultaneously at different speeds/durations/directions. All the motors are DC but are from 4 different manufactures with different RPM speeds.

The shield is the L203D I bought on Amazon.

as you'll see, the code is woefully incomplete with serious issues - currently I'm stuck with the
expected initializer before '.' token.

I'm trying to get one motor running before expanding the program to 4

Thank you for your time and thoughts on this.

#include <AFMotor.h>
//

AF_DCMotor motor4 (4);
int motor4State = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
int motor4.setSpeed (254);
int motor4.run(RELEASE);
int motor4.run(FORWARD);
void setup() {
  AF_DCMotor motor4 (4);


}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (motor4.run(FORWARD))  {
      motor4.run(RELEASE);

    } else {
      motor4.run(FORWARD);


    }

There is code in your post, but is incomplete. It is missing at least a few closing braces.

The AFMotor.h documentation from GitHub - adafruit/Adafruit-Motor-Shield-library: Adafruit Motor shield V1 firmware with basic Microstepping support. Works with all Arduinos and the Mega advise moving on to Adafruit's V2 board and library

In any case, you can use millis() for high level control of the V1 board, with the library handling the lower level PWM and L293 control.

Does the https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/examples/MotorParty/MotorParty.ino work with your hardware?

Are you asking if it is possible to recode something like this example to use the BlinkWithoutDelay millis() scheme instead of delay()?

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Yes, it is possible. If this guess of mine is what you intend, I'd add in the extra setup stuff and then change the last line to something like:

    digitalWrite(ledPin, ledState);
    motor4.run(ledState? FORWARD : BACKWARD);
    motor3.run(ledState? BACKWARD : FORWARD);
    motor2.run(ledState? FORWARD : RELEASE);
    motor1.run(ledState? RELEASE : BACKWARD);

For different durations, you need to add different blocks like this:

if (currentMillis - previousMillis >= interval) {
  ...
}

... with individual previousMillis and interval variables.

That's the best way to go!

As You intend to use a stone age driver I ask You for schematics. Way to often powering is an issue. Then all thinking about code is useless.

Hi DaveX

Thank you for your non passive aggressive reply!!! Yes you are correct with your guess.

I was unaware that they had a V2 board - I'll look into getting one. In meantime I'll do as you suggest and update my code with your suggestions. as to:

Does the Adafruit-Motor-Shield-library/MotorParty.ino at master · adafruit/Adafruit-Motor-Shield-library · GitHub work with your hardware?-

I'll have to find out.

You've been a really big help to someone who is clearly over their head.

Greatly appreciated .
G

Here's some patched-up code, with a guess as to your intentions, in Wokwi:

Thank you sir - I'll look into the V2 shield. Funny how you don't know what you don't know until you do.

cheers.

Aw, it's a bit aggressive -- lots of the questions here are like "My breakfast didn't work" without enough details for people to give anything but a snarky answer. Folks ask for what you intended your breakfast to be and what recipe you tried.

When I use libraries, especially harder-to-find ones like AFMotor, I like to put a link to the library doc/code on the #include line (and in any questions) In this case, it says pretty clearly that it's obsolete.

Hi DX

I hope you didn't think I thought you were passive aggressive - quite the opposite in fact. The hard thing with starting out on learning code you take a few things for granted, case in point - the AFMotor library. How in the hell would a beginner think that it was obsolete. I'm having enough trouble trying to figure how to set up the code. Same with uploading code to the forum - I spent an hour going over everything a trillion times and my code never showed the tabs - I finally said screw it I'll sent it anyway. Only to find out that the tabs don't show up in the thread until you post your question.

I really appreciate your help and as you suggested, I'll be updating to V2.

On a final note - I hope I can reach out to you in the future if that’s s OK.

Cheers.
G

Arduino includes a lot of crufty, old, confusing, misleading info.

One thing to try if you are having problems with a library is looking at its info in Library Manager:

Clicking on "More info" says it's old and deprecated:

Your V1 board will still work, but it is harder to get support for older, deprecated hardware.

greatly appreciated -

cheeers

GC

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