0

I made a servo motor turn if the result of random(0, 10000) is less than or equal to 1. If that is the case, the motor turns to 130 degrees, to 50 degrees, and then to default position(90 degrees). It works well except whenever I turn on the Arduino, it automatically executes the turning maneuver. It usually takes dozens of seconds for the randomNum to be 1 or 0. However, the servo always turns right away at the beginning. Here is my code. I will make comments to help you understand what I was thinking. I used no delays.

#include <Servo.h>
Servo servo;
unsigned long timer=millis();//counter
unsigned long servoTimer;
boolean turned130 = false;
int randomNum;//will hold value 0-9999
bool backCheck = false;//variables to keep track which code block to call
bool checkingAllowed = false;
bool turned50 = false;
bool startChecking = false;

void setup() {
servo.attach(12);
servo.write(90);//set default position
servoTimer=millis();
}

void loop() {
timer=millis();//update counter each time
if(checkingAllowed==false){
randomNum = random(0, 10000);//random number generator
if(randomNum<=1){//small odds
  checkingAllowed = true;//makes this codeblock unable to run
  servoTimer=millis();
  servo.write(130);
  turned130=true;/sets variables to allow other if statements to run
  turned50=false;
}
}
if(timer-servoTimer>=1000 && turned130==true && checkingAllowed==true && turned50==false){//next codeblock that runs if random <= 1 after 1 second
turn50();
}
if(timer-servoTimer>=1000 && turned130==false && checkingAllowed==true && turned50==true){
turnBack(); one second later this runs
}
if(timer-servoTimer>=1000 && checkingAllowed==true && backCheck==true){
  backCheck=false;//servo turns back to 90
  checkingAllowed = false;//allows the main code block to be called
}


delay(2);//small delay
}


void turnBack(){
random50 = random(0, 160);
servoTimer=millis();
servo.write(90);
backCheck = true;
turned50=false;
}
void turn50(){
turned130 = false;
random130  = random(0, 160);
servoTimer = millis();
servo.write(50);
turned50=true;
}

It takes a while for the random number to be 1 or 0. I do not understand how 100% of the time, the servo executes the maneuver in the beginning. I will give any more information if you need it to aid me. The random() will soon be replaced by ping() of ultrasonic sensor. Then, the problem will probably disappear. I just want to be clear on what is happening here.

3

Recognise that, despite the name, random() does not produce a truly random number. Because a computer is basically determinate, every time random() is called it produces results in a fixed sequence - it is possible that this sequence includes 0 or 1 straight away.

If you want it to start at a different point, you need to "seed" the random number generator by calling randomSeed() and passing in an integer. Note that it can't be the same integer every time - that would just move the sequence to the same point every time. The Arduino reference for random() suggests reading an unconnected analog input with analogRead() (which would produce different numbers at different instances in time) to "seed" the random number generator with a new starting position.

randomSeed(analogRead(A0)); // Seed random() with random starting point

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.