I have an if - else loop, but I cannot make the motor stop spinning

My project requires me to be able to spin a little motor motor for (x) time at (x) speed. I need to be able to control when it begins and when it stops. It needs to be activatable once every few hours, but that is unnecessary right now, I just need to be able to run the little motor at a certain speed for a certain time.

I have an if - else loop that has the machine wait four seconds and then activate an LED for 2 seconds and the motor for 2 seconds. the LED does this just fine. The real problem is that the motor won't ever turn off. It just keeps on going. that would cause my machine a real problem.

What am I doing wrong?
Could it be an issue of wiring? the motor is off for the first four seconds, as per the program, and then turns on soon after the LED, so it isn't indiscriminately receiving power, but it just won't turn off.

Here is the code:


int motorPin = 10;
int ledPin = 3;

void setup()

{
int ledPin;
int motorPin;
pinMode (motorPin, OUTPUT);
}

void loop()
{

if(millis () == 4000 )
{

analogWrite(ledPin, 1000);
delay(2000);
analogWrite(motorPin, 110);
delay (500);
}
else
{
analogWrite(ledPin, 0);
analogWrite(ledPin, LOW);
}

}

T-57:
I have an if - else loop that has the machine wait four seconds and then activate an LED for 2 seconds and the motor for 2 seconds. the LED does this just fine. The real problem is that the motor won't ever turn off. It just keeps on going. that would cause my machine a real problem.

What am I doing wrong?

First, you are not using code tags. Reply to this post by pressing the QUOTE just above the message body, and have a look at how quote tags work.

Second, if, else is not a loop. The condition determines which block of code is executed, and then it drops out of the block.
Now for the code. See the comments.

int motorPin = 10; 
int ledPin = 3;

void setup()
{
  int ledPin;  // These redeclare ledPin and motorPin. It doesn't matter in this case, because you don't
  int motorPin; // assign anything to them. But if you did, the changes would only be visible in setup()
  pinMode (motorPin, OUTPUT);
}

void loop()
{
  if(millis () == 4000 )  // this will only happen once, and never again until the program runs again.
  {
    analogWrite(ledPin, 1000);
    delay(2000);
    analogWrite(motorPin, 110);
    delay (500);
  }
  else
  {
    analogWrite(ledPin, 0);   // Oh look! You turned off the LED twice.
    analogWrite(ledPin, LOW); // Perhaps the motor will stop to have a look at the LED.
  }
}

You don't ever turn the motor off:
analogWrite (motorPin, 0);

You do attempt to turn the LED off twice:

analogWrite(ledPin, 0);
analogWrite(ledPin, LOW);

You don't ever turn the motor off:
analogWrite (motorPin, 0);

You do attempt to turn the LED off twice:

analogWrite(ledPin, 0);
analogWrite(ledPin, LOW);

Crossroads, thank you. You are right, I just mistyped the command to turn off the LED twice. thanks for pointing that out.

Much abliged

(Postscriptum note: Lar3ry mentioned the "Turn of LED twice" frst, in the commented version of the original code. Answers sometimes get submitted independently.

It is well worth reading the commented code as there are other seriouse flaws in the code, when it gets expanded.

BTW: The millis()==4000 will occur once ever 49.7 days :slight_smile: )

analogWrite(ledPin, 1000);

Bzzzt

I saw the comment that "The millis()==4000 will occur once ever 49.7 days".

This might be true. Maybe not. There is no guarantee that millis() will produce every possible integer. Sometimes it skips a little bit. It is entirely possible that millis()==4000 NEVER occurs! It depends upon little details like what kind of Arduino is being used. The code as it exists is fragile.

millis() >= 4000L might be a better choice.