How to modify the below code that the motor works (Turns on) for tamount of time

I have this below code for turning right and left a small dc motor that turns to right by entering “ 1” and turns to left by entering “2” and stop by entering “0”

All is OK by this code but will be appreciated if somebody helps me and corrects this below code that after entering “1” or “2” the motor works for the amount of time for example 5 seconds and then turns off automatically without needing to enter “0”

/*

  • Control DC motor with Smartphone via bluetooth
    */
    int motorPin1 = 3; // pin 2 on L293D IC
    int motorPin2 = 4; // pin 7 on L293D IC
    int enablePin = 5; // pin 1 on L293D IC
    int state;
    int flag=0; //makes sure that the serial only prints once the state

void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '0' the DC motor will turn off
if (state == '0') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: off");
flag=1;
}
}
// if the state is '1' the motor will turn right
else if (state == '1') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
if(flag == 0){
Serial.println("Motor: right");
flag=1;
}
}
// if the state is '2' the motor will turn left
else if (state == '2') {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: left");
flag=1;
}
}
}

Take a look at the techniques used for timing in Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Basically, save the value of millis() when the motor starts then test each time through loop() whether the required on period has elapsed. Is so then stop the motor. If not then keep going round loop()

Thanks for the note and although I studied Using millis() for timing. A beginners guide but could not understand how to modify the current code, so this will be very appreciated if an expert who is familiar with programming correct the code that I can learn how to apply in my other projects. Thanks in advance for assistance

Try this
NOTE : UNTESTED and only coded for motor right

/*
* Control DC motor with Smartphone via bluetooth
*/
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
int state;
int flag = 0;      //makes sure that the serial only prints once the state
unsigned long currentTime;  //current time used throughout the program  ***********************
unsigned long startTime;    //start time of period used throughout the program  ***********************


void setup()
{
  // sets the pins as outputs:
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  // sets enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop()
{
  currentTime = millis(); //current time **************************
  //if some date is sent, reads it and saves in state
  if (Serial.available() > 0)
  {
    state = Serial.read();
    flag = 0;
  }
  // if the state is '0' the DC motor will turn off
  if (state == '0')
  {
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    if (flag == 0)
    {
      Serial.println("Motor: off");
      flag = 1;
    }
  }
  // if the state is '1' the motor will turn right
  else if (state == '1')
  {
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
    if (flag == 0)
    {
      Serial.println("Motor: right");
      flag = 1;
      startTime = millis(); //start time of 5 second period **************
    }
    boolean doneTiming = checkTiming();  //test whether period has ended  **************************
    if (doneTiming)   //period has elapsed
    {
      digitalWrite(motorPin1, LOW); // set pin 2 on L293D low //turn motor off  *********************
      digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
      Serial.println("Motor: off");
      flag = 1;
    }
  }
  // if the state is '2' the motor will turn left
  else if (state == '2')
  {
    digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    if (flag == 0)
    {
      Serial.println("Motor: left");
      flag = 1;
    }
  }
}

boolean checkTiming()
{
  if (currentTime - startTime > 5000)  //5 seconds is up
  {
    return true;
  }
  else
  {
    return false;
  }
}

Thanks a lot for new code but unfortunately the new code could not turn off the motor right after 5 seconds. should I change it anyhow? please advise

Thanks again for your time

OK. Here is another version.

It compiles and when run gives the expected outputs to Serial.

/*
* Control DC motor with Smartphone via bluetooth
*/
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
int state;
int flag = 0;      //makes sure that the serial only prints once the state
unsigned long currentTime;  //current time used throughout the program 
unsigned long startTime;    //start time of period used throughout the program
unsigned long period = 5000;  //change this to alter the run period
boolean timing = false; //flag to indicate that timing is in progress


void setup()
{
  // sets the pins as outputs:
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  // sets enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
}

void loop()
{
  currentTime = millis(); //current time
  //if some date is sent, reads it and saves in state
  if (Serial.available() > 0)
  {
    state = Serial.read();
    flag = 0;
  }
  // if the state is '0' the DC motor will turn off
  if (state == '0')
  {
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    if (flag == 0)
    {
      Serial.println("Motor: off");
      flag = 1;
    }
  }
  // if the state is '1' the motor will turn right
  else if (state == '1')
  {
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
    if (flag == 0)
    {
      Serial.println("Motor: right");
      flag = 1;
      startTime = millis(); //start time of 5 second period **************
      timing = true;
      Serial.println("timing");
    }
  }
  // if the state is '2' the motor will turn left
  else if (state == '2')
  {
    digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    if (flag == 0)
    {
      Serial.println("Motor: left");
      flag = 1;
    }
  }
  
  if (timing) //check whether period has ended
  {
    if (currentTime - startTime >= period)  //period has ended
    {
      timing = false;
      digitalWrite(motorPin1, LOW); // set pin 2 on L293D low  turn off the motor
      digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
      Serial.println("motor stopped");
    }
  }
}

Good Job and thanks, the timing code works and I added your code for turning the motor left too that I hereby send the code I did use after your code modification, although in your code, the Serial.begin was (115200 ) that did not work and I changed it to 9600 that is working now but I have still following problems that hope you can let me know how to solve it.

NOTE: I changed time to 1000 ms that is 1 second for quicker test.

1- when I enter "1" to turn the motor to right, it works for 1, 2 or even more times correctly without problem and stop according to the 1 second that I already set but most of the times, when I enter "1", the motor does not move and no way else except entering "2" to turn the motor to left and then the entering "1" can turn the motor to right to turn the motor to right

2- Most of the time, when we enter 1 or 2 to turn the motor to right and left, the motor begins working but does not turn off according to the timing that we already set. for example after 1 second

The code that I added to the sketch as per as your code is as the follow. Please advise what wrong is.

/*

  • Control DC motor with Smartphone via bluetooth
    */
    int motorPin1 = 3; // pin 2 on L293D IC
    int motorPin2 = 4; // pin 7 on L293D IC
    int enablePin = 5; // pin 1 on L293D IC
    int state;
    int flag = 0; //makes sure that the serial only prints once the state
    unsigned long currentTime; //current time used throughout the program
    unsigned long startTime; //start time of period used throughout the program
    unsigned long period = 1000; //change this to alter the run period
    boolean timing = false; //flag to indicate that timing is in progress

void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop()
{
currentTime = millis(); //current time
//if some date is sent, reads it and saves in state
if (Serial.available() > 0)
{
state = Serial.read();
flag = 0;
}
// if the state is '0' the DC motor will turn off
if (state == '0')
{
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if (flag == 0)
{
Serial.println("Motor: off");
flag = 1;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
if (flag == 0)
{
Serial.println("Motor: right");
flag = 1;
startTime = millis(); //start time of 1 second period **************
timing = true;
Serial.println("timing");
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
if (flag == 0)
{
Serial.println("Motor: left");
flag = 1;
startTime = millis(); //start time of 1 second period **************
timing = true;
Serial.println("timing");
}
}

if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
Serial.println("motor stopped");
}
}
}

I can't see anything obviously wrong.
What do the messages on the Serial monitor say is happening ?

it should not make any difference by change the declaration of state from int to char

How is the motor powered ?

Hi and very very appreciate for following my coding problem.

I hereby send you the log of APK that you can see the problem

NOTE 1: The Right and Left after each other shows the code worked well and the motor turned to right and left and stopped automaticly after the 1 second that is already set in the code.

NOTE 2: THE Right and Right, ... or Lfet and Left, ... after each other shows the motor did not turn when I entered 1 or 2 , so I tried once, twice, … to see if the motor can turn by repeating the 1 or 2 but as you can see, finally I had no way except enter other code for example if the turn right by entering 1 did not work, I should enter 2 to turn the motor to Left and then “1” could turn the motor to Right

NOTE3: Motor: off after showing Right or Left are the one the motor did not stopped, so I had to stop it by entering “0”

Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: off
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: off
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: off
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped

I hereby send you the log of APK that you can see the problem

Does the code work OK if you enter the commands directly into the Serial monitor ?

Hi

What do you mean of "enter the commands directly into the Serial monitor"?

The code worked well without any problem when we did not add the timing code but that is not what I am looking for because timing is very very very important to me.

Hope to hear you

Regards

What do you mean of "enter the commands directly into the Serial monitor"?

Disconnect anything connected to pins 0 and/or 1
Compile and upload the code
Open the Serial monitor in the IDE
Enter the commands into the Serial monitor input field and click Send or press return
What do you see in the Serial monitor window ?
What does the motor do ?

Thanks for your valuable time

Below is the IDE logs and as you see the problem is same

NOTE1: The Right after Right shows entering "1" could not turn the motor, so I repeated enter "1" again without success and I had to enter "2" to turn the motor left and then enter "1" again to turn the motor to right.

NOTE2: The Motor: off also shows that motor did not stop after the 1 second that I turned it off by entering "0"

Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: off
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: right
timing
motor stopped
Motor: left
timing
motor stopped
Motor: right
timing
motor stopped

Regards

Here is your output edited to show each command and its subsequent output

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: off

Motor: right
timing
motor stopped

Motor: off

Motor: right
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: right
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: off

Motor: right
timing
motor stopped

Motor: right
timing
motor stopped

Motor: right
timing
motor stopped

Motor: left
timing
motor stopped

Motor: right
timing
motor stopped

I cannot see any instances where a "Motor:left" or "Motor:right" message was not followed by a "waiting" message and then a "motor" stopped message. Did I miss it ?

Does the Serial monitor output match what the motor actually does and is the monitor output correct but the effect on the motor wrong ?

Hi, and thanks for your continuous follow-up.

No, you did not miss anything, All that is written in IDE Serial monitor was copied and sent to you.

What do you mean of " I cannot see any instances where a "Motor:left" or "Motor:right" message was not followed by a "waiting" message and then a "motor" stopped message."?

What do you mean of "Does the Serial monitor output match what the motor actually does and is the monitor output correct but the effect on the motor wrong ?

All commands that was sent to IDE serial monitor, like 1 enter, 2 enter, 0 enter was shown with the feedback that I sent to you but although the feedback appeared there but no action was done when you see I tried sending command "1" for several times, this means, the feedback appeared there but the motor did not move.

Regards

What do you mean of " I cannot see any instances where a "Motor:left" or "Motor:right" message was not followed by a "waiting" message and then a "motor" stopped message."?

I man exactly what I said.

You said

The Right after Right shows entering "1" could not turn the motor, so I repeated enter "1" again without success and I had to enter "2" to turn the motor left and then enter "1" again to turn the motor to right.

Does that show up in the output ?

the feedback appeared there but the motor did not move.

Then there is something wrong with the commands that control the motor, the wiring or the power to the motor.

Thanks for all your support

The story is exactly like what I already explained.

I meant, after entering "1" to turn the motor right, either through smartphone apk or through IDE Serial Monitor, the result is same, like what I already sent to you as below.

Motor: right
timing
motor stopped
Motor: left
timing
motor stopped

This means after entering "1" or "2", either the motor turns right or left or the motor does not turn at all, the log will be the same in Smartphone APK or IDE Serial Monitor, exactly like the times that the motor is really turned right and left after receiving commands "1" or "2", so by checking the Smart phone APK or IDE Serial Monitor, no one can understand if the motor was really turned after receiving the command or not if judgeing about good and correct working is supposed to be according to the trust of what is written in Smartphone APK or IDE Serial Monitor.

This was the reason that I tried several times entering "1" or "2" one after one if the motor was not turned right or left to show you that although the command "1" or "2" was sent either through Smartphone APK or IDE Serial Monitor, the result is same and although the monitor shows the command is received by board and write Motor: right
timing
motor stopped
Motor: left
timing
motor stopped

but the motor might not be turned and just the feedback was sent to the monitor.

Thanks and regards

after entering "1" to turn the motor right, either through smartphone apk or through IDE Serial Monitor, the result is same, like what I already sent to you as below.

Motor: right
timing
motor stopped
Motor: left
timing
motor stopped

So when you enter a command the Serial monitor shows the expected output.

Is that correct ?

Exactly, and after sending any command, either the motor turns or not, the serial monitor shows expected output and does not show failed if the motor did not turn.

Regards

negahbaan:
Exactly, and after sending any command, either the motor turns or not, the serial monitor shows expected output and does not show failed if the motor did not turn.

Regards

If the motor fails to run or stop when the Serial monitor shows that it should do then you need to examine the motor control code that is executed at that point.

A suggestion. Put the motor control code in 3 separate functions, perhaps motorRight(), motorLeft() and motorStop() and call those functions when you want to control the motor. That will allow you to call each of the motor control functions in turn from setup() to test the motor control functions to ensure that they do what you want.

I asked previously how the motor is powered. Did I miss your reply ?