Running multiple DC motors with Arduino DUE.

Hi everyone. We’re a couple of students looking for help to program our Arduino DUE.
This is an image of our current coding:

int motorPin = 9;
void setup() {
pinMode(motorPin, OUTPUT);
}
void loop() {
for(int motorValue = 0 ; motorValue <= 125; motorValue +=5)
analogWrite(motorPin, motorValue);
delay(300);

for(int motorValue = 125 ; motorValue <= 180; motorValue +=5)
analogWrite(motorPin, motorValue);
delay(150);

for(int motorValue = 180 ; motorValue <= 255; motorValue +=5)
analogWrite(motorPin, motorValue);
delay(100);

for(int motorValue = 255 ; motorValue >= 0; motorValue -=15)
analogWrite(motorPin, motorValue);
delay(5);
}
This is just a test to see if the coding is right, and not the right intervals and values.

Our idea was to add an extra void setup and loop but on another pin which then could power the second motor. The second motor should run on other motorValue’s and delays and also run at the same time as the first one. We tried to copy the same coding as above and paste it right under, but with other values and pins but when we run the verify / compile button it comes up with a lot of errors.

So the questions we seek answers for is:

1:Do you guys have any suggestion as to how we can run 2-3 DC motors on one Arduino DUE?
2:What coding should we use to run several DC motors at the same time (But not with the same input value and delay?)

-Sincerely desperate students.

Running multiple DC motors with Arduino DUE.

looking for help to program our Arduino Uno.

First step is to figure out which board you really have.

Our idea was to add an extra void setup and loop but on another pin which then could power the second motor. The second motor should run on other motorValue's and delays and also run at the same time as the first one.

You can only have one setup() and one loop() function. You can do any number of things in each function.

1:Do you guys have any suggestion as to how we can run 2-3 DC motors on one Arduino DUE?

You can't POWER any motors from the Arduino. You need to use a motor shield to POWER the motors, which the Arduino can then control.

Unless you mean servos... You still can't power the servos from the Arduino, but you can control them.

2:What coding should we use to run several DC motors at the same time (But not with the same input value and delay?)

"at the same time" and "delay" just do not belong in the same sentence.

You need to look at the blink without delay example to learn how to not use delay(). Then you need to completely start over. You can not use a for loop. You must, on each pass through loop(), see if it is time to do each thing that might need doing. If it is, do it, and record what the next thing to do might be and when to do it (relative to the last time something was done).

Our idea was to add an extra void setup and loop but on another pin which then could power the second motor

Wrong idea entirely. setup() and loop() are functions and you can only have one of each in a program.

So, what to do ?
First ditch the delay()s. You need to look into using millis() for timing as in the BlinkWithoutDelay example.
Save the time an event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.

Using millis() will allow loop() to continue to run rather than being stalled by delay() so you can add a second (or more) motor(s) each with their own timing.

This may help Several things at the same time

Okay, first one was a typing failure. We have an Arduino DUE to work with.

As I been able to read from other websites etc. we cant have several motors implemented in just one setup and one loop function, so I guess we have to do something else.

Also, we plan on having a supply station to power out motors and then have the Arduino to change what input the motors get.

And what we intend to do, is to control several DC motors to run simultaneously but with different motorValues so that they dont run with the same speed.

We will try to look at the blink function but if you have any ideas as how the coding might look like, it would be greatly appriciated.

As I been able to read from other websites etc. we cant have several motors implemented in just one setup and one loop function

Bullpoop.

so I guess we have to do something else.

Yes, but adding more setup() and loop() functions is not it. Nor is adding multithreading or interrupts.

You do need to engage your brain. How would YOU move the change the speed of the motors at different intervals?

And what we intend to do, is to control several DC motors to run simultaneously but with different motorValues so that they dont run with the same speed.

That does not explain why you want to change the speeds at different rates.

but if you have any ideas as how the coding might look like, it would be greatly appriciated.

Well, it will have one setup() function, one loop() function, and one call to millis(), plus some other stuff.

if you have any ideas as how the coding might look like, it would be greatly appriciated.

Start with something like this

start of loop()
  get current time from millis()
  
  if motor 1 speed period has elapsed
    change the speed of motor 1
    save the time of motor 1 speed change
  end if
  
  if motor 2 speed period has elapsed
    change the speed of motor 2
    save the time of motor 2 speed change
  end if

  if motor 3 speed period has elapsed
    change the speed of motor 3
    save the time of motor 3 speed change
  end if
end of loop()

When you have got it working then change it to use arrays of values to avoid the repetitive code

Well, this is the first time we have ever tried working with Arduino - and coding for that matter - so this is entirely new to us. But anyway, thanks for the quick responses, we will look into the blink without delay and try to make it fit to our project.