Need a simple Arduino program

Hi, I am a newbie here so hopefully I am posting this in the right place. Sorry if not.

I need what I think is a simple program. I need to control a 12V DC 20 rpm motor as follows:

Turn motor on at max speed
Turn motor off after 750ms
Wait 3,000 ms

Repeat the above for 450 seconds (450,000ms) then pause for 400 seconds (400,000ms)

Then keep repeating the above, ie turn motor on/off/wait and repeat for 450 secs and pause 400 sec.

I am wondering if someone can help or direct me in the right direction.

Thanks!

Welcome to the forum

The forum is not a code writing service unless you want to pay for it. If so, there is a section of the forum in which you can post

What is your programming and electronics experience and what have you tried so far ?

I have some coding experience years ago with languages no one uses anymore but not with Arduino. I've found programs online that start the motor at max speed but none that do the loop repeatedly and then pause.

I'll look for the paid forum. Thank you.

Welcome
Just hit the flag icon and ask a moderator to move it, your needs are reasonably trivial and you could do it yourself, that is what this forum is for, but you would need to put some time and effort in to learn the concepts. If it is a one off and you don’t care about learning then the paid section is for you. If you want to spend a week or so getting to know arduino and C then ask specific questions posting your code etc as per the forum guidelines

Thanks will do. This is just a program I need for a prototype to turn a motor pending the motherboard being developed and my time would be more useful on other aspects of the project.

Moved to the Jobs and Paid Consultancy

I don't think I need an L298N as I am only driving the motor clockwise and only at the max speed.

Why not post the simplest one, as a starting point? It will save someone a lot of typing. There are a lot of bitten tongues here, it's a super easy sketch.

You still need a driver of some kind. No Arduino output will directy drive a motor. I would recommend a logic level MOSFET with a flyback diode.

You're absolutely correct - you need something much more modern and efficient.

1 Like
void loop()
{
  // Cycle for 450 seconds
  // 450000 / 3750 = 120
  for (int i=0; i < 120; i++)  // Repeat 120 times
  {
    MotorOn();
    delay(750ul);  // run for 750 milliseconds
    MotorOff();
    dely(3000ul); // pause for 3 seconds
  }
  delay(400000ul); // pause for 400 seconds
}
1 Like

Thanks. I've never programmed Arduino (my background is a hundred years ago in Fortran, Basic & PL/1, lol) but decided to do some review of the Arduino programming language this morning and try to tackle it myself. I came up with this but added your 'for' command to replace a 'for while' that I had. I changed the delay to 833ms, repeats to 150, left the pause at 400 secs, but then added that the sequence run for 10 hours.

I don't see your motoron() of motoroff() commands existing anywhere in arduino code after doing an online search. Are they the name of a function you created but did not list?

Any advice anyone can give to this newbie about my code would be appreciated.

//L293D
const int motorPin1 = 5;  // Pin 14 of L293D
const int motorPin2 = 6;  // Pin 10 of L293D
unsigned long startmillis;

void setup() {
  //Set pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  //initial start time
  startmillis = millis();
}
//Run program for 7200000ms (2 hrs)
void loop() {
  while (millis() - startmillis < 7200000ul) {
    //Spin motor for 833ms & pause for 3000ms, do 150x
    //Motor Control - motorPin1,motorpin2

    for (i = 0; i < 150; i++) {
      //Spin motor clockwise
      digitalWrite(motorPin1, HIGH);
      digitalWrite(motorPin2, LOW);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, LOW);
      delay(833ul);  //run 833ms

      //Pause motor for 3000ms

      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, LOW);
      delay(3000ul);  //pause 3 secs
    }
    delay(400000ul);
  }
}

Thanks. Great idea but I didn't know anything until this morning when I decided to spend a few hours learning the Arduino code. I have now posted my code.

Sure, but you didn't mention how the code behaves.

Do you mean when I load it on the Arduino and power it up? I haven't tried that yet. I will do that and report back.

Yes, that.

John did a pseudocode, this functions "should" be created in your code.

For example:

void MotorOn()
{
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, HIGH);
}
void MotorOff()
{
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
}

How many motors do you have in this project?

Take a look in this tutorial.

One.

Try this example:

#include <Arduino.h>

// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;

void directionControl();
void speedControl();

void setup()
{
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void loop()
{
  directionControl();
  delay(1000);
  speedControl();
  delay(1000);
}

// This function lets you control spinning direction of motors
void directionControl()
{
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);

  // Turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(2000);

  // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(2000);

  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

// This function lets you control speed of the motors
void speedControl()
{
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++)
  {
    analogWrite(enA, i);
    delay(20);
  }

  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i)
  {
    analogWrite(enA, i);
    delay(20);
  }

  // Now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}