Coding Help

I want to gradually turn off 4 motors after a certain amount of time. I want the void loop to keep going to 5 minutes before gradually turning off the motors (they slow down). How do I do this? Thanks!

What kind of motors ? Little toy DC motors ? airplane turbine engines ? stepper motors ? ion thruster ?
What do you use for a driver ? A shield or a driver board ? What board ?
What is the project about ?

There are libraries for timing events, there are also libraries for stepper motor libraries with automatic ramp. You could also program it yourself.

Start by taking a look at the Blink Without Delay.

I am using a DC motor. I am new to arduino, so I am not sure what you mean by what 'driver' I used. I a going to attach it strait to the board without a shield if that's what you are asking.

I am using a DC motor.

Since we didn't figure you were driving an AC motor, that doesn't help at all. What voltage? What amperage?

I a going to attach it strait to the board without a shield if that's what you are asking.

It was, and no, you probably aren't.

Generally, a motor will draw too much current to be powered from the arduino directly and you will likely damage the arduino if you try. Take a look at this schematic from the playground: Arduino Playground - HomePage. It shows you how to wire up a solenoid, but the same thing applies to a DC motor.

It is a max of 3.7v, so I don't think it will damage the arduino, will it? I am going to be using PWM anyway. I don't think I am ever going to use the full 3.7v

It is a max of 3.7v, so I don't think it will damage the arduino, will it? I am going to be using PWM anyway. I don't think I am ever going to use the full 3.7v

So, you intend to apply 5V at next to no current to a motor that expects no more than 3.7V at significant current. OK. It's your motor and your Arduino. Feel free to abuse them both. When you get new ones, though, don't abuse them.

Won't PWM allow for a lower voltage?

No.
PWM is either Vcc or nothing.

Arduino pins can only supply ~40ma, which is probably insufficient to make your motor move.

Now I am confused, I though PWM will lower the voltage it is outputting with the duty cycle… And I thought it was possible to start a motor with an arduino. So, the voltage is too high (even with PWM?) and the amperage is too low??

Also, there are still a couple problems:

  1. I do not get how to use the “blinkwithoutdelay” function to allow for the code to run for some time and then slow down the motors.
  2. I want to stop the void loop() when I start to slow down the motors, how do I do that?
  3. How do I slow down the motors (is there a function to gradually slow down the motors?)

Here is the code if it helps at all:

#include <Ultrasonic.h>
#define trigPinfront 22
#define echoPinfront A0
#define trigPinleft 26
#define echoPinleft A1
#define trigPinright 30
#define echoPinright A2
#define trigPinup 34
#define echoPinup A3
#define trigPindown 38
#define echoPindown A4
#define frontleft 3
#define frontright 5
#define backleft 6
#define backright 9


void setup()
{
  Serial.begin (9600);
  pinMode(trigPinfront, OUTPUT);
  pinMode(echoPinfront, INPUT);
  pinMode(trigPinleft, OUTPUT);
  pinMode(echoPinleft, INPUT);
  pinMode(trigPinright, OUTPUT);
  pinMode(echoPinright, INPUT);
  pinMode(trigPinup, OUTPUT);
  pinMode(echoPinup, INPUT);
  pinMode(trigPindown, OUTPUT);
  pinMode(echoPindown, INPUT);
  delay(3000);
  analogWrite(frontleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backleft, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
}

void loop()
{
  int durationfront, distancefront;
  digitalWrite(trigPinfront, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPinfront, LOW);
  durationfront = pulseIn(echoPinfront, HIGH);
  distancefront = (durationfront/2)/29.1;
  Serial.print(distancefront);
  Serial.println("cm");
  if (distancefront < 240)
  {
  analogWrite(frontleft, 145);
  analogWrite(backleft, 145);
  delay(2000);
  analogWrite(frontleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backleft, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
  }
  
  int durationleft, distanceleft;
  digitalWrite(trigPinleft, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPinleft, LOW);
  durationleft = pulseIn(echoPinleft, HIGH);
  distanceleft = (durationleft/2)/29.1;
  Serial.print(distanceleft);
  Serial.println("cm");
    if (distanceleft < 15)
  {
  analogWrite(frontleft, 145);
  analogWrite(backleft, 145);
  delay(2000);
  analogWrite(frontleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backleft, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
  }
  
  int durationright, distanceright;
  digitalWrite(trigPinright, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPinright, LOW);
  durationright = pulseIn(echoPinright, HIGH);
  distanceright = (durationright/2)/29.1;
  Serial.print(distanceright);
  Serial.println("cm");
    if (distanceright < 15)
  {
  analogWrite(frontright, 145);
  analogWrite(backright, 145);
  delay(2000);
  analogWrite(frontleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backleft, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
  }
  
  int durationup, distanceup;
  digitalWrite(trigPinup, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPinup, LOW);
  durationup = pulseIn(echoPinup, HIGH);
  distanceup = (durationup/2)/29.1;
  Serial.print(distanceup);
  Serial.println("cm");
    if (distanceup < 15)
  {
  analogWrite(frontleft, 125);
  analogWrite(backleft, 125);
  analogWrite(frontright, 125);
  analogWrite(backright, 125145);
  delay(2000);
  analogWrite(frontleft, 135);
  analogWrite(backleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
  }
  
  int durationdown, distancedown;
  digitalWrite(trigPindown, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPindown, LOW);
  durationdown = pulseIn(echoPindown, HIGH);
  distancedown = (durationdown/2)/29.1;
  Serial.print(distancedown);
  Serial.println("cm");
   if (distancedown < 15) 
  {
  analogWrite(frontleft, 145);
  analogWrite(backleft, 145);
  analogWrite(frontright, 145);
  analogWrite(backright, 145);
  delay(2000);
  analogWrite(frontleft, 135);
  analogWrite(backleft, 135);
  analogWrite(frontright, 135);
  analogWrite(backright, 135);
  delay(2000);
  analogWrite(backright, 140);
  analogWrite(backleft, 140);
  }
}

electricviolin:
Won't PWM allow for a lower voltage?

You can but you need hardware also

electricviolin:
Won't PWM allow for a lower voltage?

It isn't about that - it's about how much current the motor will use which depends on load and voltage. PWM signals "look like" an average voltage to a DC motor, so in effect it is a lower voltage, yes, but that isn't the important part.

To drive a DC motor with an Arduino using PWM you need to create a simple amplifier circuit, so you can use the small current levels of the Arduino to control the large current levels of the motor. This is a very simple circuit which everybody should know by heart. It is explained in this tutorial...

That will "protect" the Arduino from the high current draw of the motor.

Okay, so I looked at the link and if I get these parts, will it be okay to use my setup (if I do what the tutorial said to do?). Also, the two questions I had before:

  1. I do not get how to use the "blinkwithoutdelay" function to allow for the code to run for some time and then slow down the motors.
  2. I want to stop the void loop() when I start to slow down the motors, how do I do that?

The third question I figured out.

What don't you "get" about blink without delay?
Have you played around with it?
It has about four significant expressions - what don't you understand about it?

electricviolin:
Okay, so I looked at the link and if I get these parts, will it be okay to use my setup (if I do what the tutorial said to do?).

Yes, the amplifier circuit is sufficient to drive a brushed motor based on the PWM signal from the Arduino. You are making a simple 'electronic speed controller' - look that up.

  1. I want to stop the void loop() when I start to slow down the motors, how do I do that?

No, you don't want to stop the loop() function - that's the point of showing "BlinkWithoutDelay" example code. In that example, you are shown how to manage the state of your devices while allowing the loop() function to run over and over again as fast as possible. With this program structure, you will use timing to manage your delays rather than stopping all the processing. This way you can manage multiple things by simply changing their state at appropriate times. Understanding the BlinkWithoutDelay example is fundamental to everything Arduino, so please if there's something you don't quite follow, try to ask good questions about those specifics, so we can help you.

I still have so many questions.

Which Arduino board do you use ?
How many motors to you have ? Which brand and type ? Where did you buy them ?

If you want the motors to be able to rotate in both directions, you need an H-bridge. A driver circuit for an H-brigde for 3.7V motors is not easy to find.

Is your project a robot, which needs to slow down if it an obstacle is detected ? What kind of robot ? Is there extra hardware with the robot ?

Is it a project for school ? and do you already have working code for it ? What is your part of the project ? Do others work on the project ?

You could set the speed (the pwm with analogWrite) every 10ms or every 20ms. And use a function that creates a ramp to speed up and slow down. The loop() would run every 10ms or 20ms and that function is called every time. The function would be partly a state-machine. A variable tells the function if speed up or slow down is active at the moment.

I want to stop the void loop after 3 minutes and then slow down four motors. I posted my code persiously. Csn someone please write that part of the code so this will happen? I am still new to arduino and coding in gerneral, so i dont get how this works.

electricviolin:
I want to stop the void loop after 3 minutes

No, you don't. You are trying to define the logic and the action at the same time, you need to separate that. One good exercise is to try to define the problem without using any computer or programming terms - define the problem as if you were going to have a person do it.

In this case, the problem is "Run the motors at one speed for three minutes, then slow them down"

Do you see how that makes the issue more clear? You can go further with this definition...

"While the Arduino master clock is under 180 seconds, run the motors at full speed. If the Arduino master clock is greater than 180 seconds, slow the motors down."

Do you see now, how you could implement this without "stopping" the loop? Each time through, you check what state you are in, and act accordingly.

Maybe this video will help. I've decided to make a video series about some of these fundamental concepts. Please let me know if this doesn't help you understand how to do things without delays.