Struggling with random number generation to move a motor

I am attempting to get a servo motor to move 90 degrees and then back again, at random intervals between 0 and 45 seconds. It all works as expected, except the motor continuously moves rather than moving at random as I would like it to. I'm fairly new to this, sorry if it's a silly mistake!

#include <Servo.h> 
 
Servo servo1;
 
int pos = 0;

long previousMillis = 0;

void setup() 
{ 
  servo1.attach(9);
}
 
void loop() 
{
  long interval = random(45000);

  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    
    for(pos = 0; pos < 90; pos += 1)
    {
      servo1.write(pos);
      delay(15);
    } 
    for(pos = 90; pos>=1; pos-=1)
    {                                
      servo1.write(pos);
      delay(15);
    } 
  } 
}

You choose a random number between 0 and 44999. If the interval between two times is not more than that random number, you IMMEDIATELY choose another random number. It won't take long until you have chosen a small random number, such that the interval is less than that number.

Try drawing a time line to explain what you are trying to do. I do not understand what currentMillis and previousMillis are supposed to represent. Meaningful names would be useful. I do not understand why you choose a new random number on EVERY pass through loop(). It seems to me that you want to move the servo once every n milliseconds, where n is a random value. That means that you would use delay(n) after moving the servo.

Do you want to get a new random number 1000 times a second, or just every time you move the servo?

aarg:
Do you want to get a new random number 1000 times a second, or just every time you move the servo?

Every time I move the servo

PaulS:
You choose a random number between 0 and 44999. If the interval between two times is not more than that random number, you IMMEDIATELY choose another random number. It won't take long until you have chosen a small random number, such that the interval is less than that number.

Try drawing a time line to explain what you are trying to do. I do not understand what currentMillis and previousMillis are supposed to represent. Meaningful names would be useful. I do not understand why you choose a new random number on EVERY pass through loop(). It seems to me that you want to move the servo once every n milliseconds, where n is a random value. That means that you would use delay(n) after moving the servo.

I used the example program BlinkWithoutDelay as a basis for the timing of my program. This uses the variables currentMillis and previousMillis to monitor how much time has passed since the action (in this case the motor moving) last occurred.

I did originally use delay(random(45000)), but had to change it to prevent the delay from interfering with the other two servos I am going to add.

But do you want to gradually move it between 0 and 90? Or from 0 to 90 fast, wait random time, 90 to 0, wait another random time?

septillion:
But do you want to gradually move it between 0 and 90? Or from 0 to 90 fast, wait random time, 90 to 0, wait another random time?

I'm trying to move it from 0 to 90 then back to 0, and have it wait a random time.

EmYo:
Every time I move the servo

Well, where you currently have it, it will happen 1000's of times a second, because that's how often the code in loop() runs.

aarg:
Well, where you currently have it, it will happen 1000's of times a second, because that's how often the code in loop() runs.

Ah, oops! Fixed it :slight_smile:

septillion:
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

Also:

"The door opens and the light comes on." => "When the door opens, the Arduino should turn the light on."