user29796: In my recent project, I'm working with the motor. I have to drive the motor for the specific time like for 2 minutes, 3 minutes, 4 minutes and 5 minutes. I already created functions for that. And motor drive based on readings taken from the sensor.
My code is below:
#define PIN_OUTPUT 6

const int kPinIN1 = 4;
const int kPinIN2 = 5;

void setup()
{
  Serial.begin(9600);

  pinMode(kPinIN1, OUTPUT);
  pinMode(kPinIN2, OUTPUT);
}

void loop() 
{
  Serial.println(analogRead(A0));

  if(analogRead(A0) > 100 && analogRead(A0) < 200)
  {
    motorDriveFor2Min();
  }
  else if(analogRead(A0) > 200 && analogRead(A0) < 300)
  {
    motorDriveFor3Min();
  }
  else if(analogRead(A0) > 300 && analogRead(A0) < 400)
  {
    motorDriveFor4Min();
  }
  else if(analogRead(A0) > 400 && analogRead(A0) < 500)
  {
    motorDriveFor5Min();
  }      
}

void motorDriveFor2Min()
{
  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);            

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(64000);  
}


void motorDriveFor3Min()
{
  motorDriveFor2Min();

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(32000);  
}

void motorDriveFor4Min()
{
  motorDriveFor3Min();

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(46000);   
}

void motorDriveFor5Min()
{
  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(120000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(120000);
}

void verboseDelay(unsigned long ms)
{
  unsigned long now = millis();
  unsigned long start = now;

  do
  {
    now = millis();
  }
  while (now - start < ms);
}

Here, I have four functions named motorDriveFor2Min(), motorDriveFor3Min(), motorDriveFor4Min() and motorDriveFor5Min() for my specific time means for 2 minutes, 3 minutes, 4 minutes and 5 minutes respectively.
So, suppose If I want to drive motor for 4 min then I have to call function motorDriveFor4Min() and for that, I have to set my reading between 300 & 400. But I can't able to do that because when my reading reaches at 100 then it starts executing another function means motorDriveFor2Min() and then continuously run that functionality. During that time I try to change the reading but nothing happens. After restating Arduino it run according to the reading.
So, I thought maybe delay() function block everything. So I replace inbuilt delay function with custom delay function. But that can't solve my problem. Then I google it out and I found out that analogRead may cause this problem. Any suggestion for this.

user4143: It's not analogRead. It's not (just) delay. It's the whole method of your program.
The way it's written you can do nothing else while driving a motor for X minutes.
You need to rethink your whole program so that it's not "run for X minutes" each time, but instead "Run the motor until the time is up" and constantly re-evaluate what the time should be.
That means:

No "run for X" functions.
No delay() functions (of any form - yours is as bad as the normal one).
A state machine to determine what should be happening when.
Constantly referencing both millis() and analogRead() to determine what state the state machine should be in or progress to and when.


29796: <p>In my recent project, I'm working with the motor. I have to drive the motor for the specific time like for 2 minutes, 3 minutes, 4 minutes and 5 minutes. I already created functions for that. And motor drive based on readings taken from the sensor.</p>

<p>My code is below:</p>

<pre><code>#define PIN_OUTPUT 6

const int kPinIN1 = 4;
const int kPinIN2 = 5;

void setup()
{
  Serial.begin(9600);

  pinMode(kPinIN1, OUTPUT);
  pinMode(kPinIN2, OUTPUT);
}

void loop() 
{
  Serial.println(analogRead(A0));

  if(analogRead(A0) &gt; 100 &amp;&amp; analogRead(A0) &lt; 200)
  {
    motorDriveFor2Min();
  }
  else if(analogRead(A0) &gt; 200 &amp;&amp; analogRead(A0) &lt; 300)
  {
    motorDriveFor3Min();
  }
  else if(analogRead(A0) &gt; 300 &amp;&amp; analogRead(A0) &lt; 400)
  {
    motorDriveFor4Min();
  }
  else if(analogRead(A0) &gt; 400 &amp;&amp; analogRead(A0) &lt; 500)
  {
    motorDriveFor5Min();
  }      
}

void motorDriveFor2Min()
{
  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);            

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(64000);  
}


void motorDriveFor3Min()
{
  motorDriveFor2Min();

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(32000);  
}

void motorDriveFor4Min()
{
  motorDriveFor3Min();

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(46000);   
}

void motorDriveFor5Min()
{
  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(120000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, HIGH);
  digitalWrite(kPinIN2, LOW);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(7000);

  digitalWrite(kPinIN1, LOW);
  digitalWrite(kPinIN2, HIGH);

  analogWrite(PIN_OUTPUT, 255);

  verboseDelay(120000);
}

void verboseDelay(unsigned long ms)
{
  unsigned long now = millis();
  unsigned long start = now;

  do
  {
    now = millis();
  }
  while (now - start &lt; ms);
}
</code></pre>

<p>Here, I have four functions named <code>motorDriveFor2Min()</code>, <code>motorDriveFor3Min()</code>, <code>motorDriveFor4Min()</code> and <code>motorDriveFor5Min()</code> for my specific time means for 2 minutes, 3 minutes, 4 minutes and 5 minutes respectively.</p>

<p>So, suppose If I want to drive motor for 4 min then I have to call function <code>motorDriveFor4Min()</code> and for that, I have to set my reading between 300 &amp; 400. But I can't able to do that because when my reading reaches at 100 then it starts executing another function means <code>motorDriveFor2Min()</code> and then continuously run that functionality. During that time I try to change the reading but nothing happens. After restating Arduino it run according to the reading.</p>

<p>So, I thought maybe delay() function block everything. So I replace inbuilt delay function with custom delay function. But that can't solve my problem. Then I google it out and I found out that analogRead may cause this problem. Any suggestion for this.</p>
 4143: <p>It's not analogRead. It's not (just) delay. It's the whole method of your program.</p>

<p>The way it's written you can do <em>nothing</em> else while driving a motor for X minutes.</p>

<p>You need to rethink your whole program so that it's not "run for X minutes" each time, but instead "Run the motor until the time is up" and constantly re-evaluate what the time should be.</p>

<p>That means:</p>

<ul>
<li>No "run for X" functions.</li>
<li>No <code>delay()</code> functions (of <em>any</em> form - yours is as bad as the normal one).</li>
<li>A state machine to determine what should be happening when.</li>
<li>Constantly referencing both <code>millis()</code> and <code>analogRead()</code> to determine what state the state machine should be in or progress to and when.</li>
</ul>
