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!
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.
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.