Random Generator stops for some reason

Hello,

A little bit of background about what I'm trying to do, I'm creating a device that teaches people Braille, and this mode allows them to ,earn a random nuber. For example, when i activate this mode, my goal is to get it to start by displaying one randomly chosen number, (let's say 4), andthen move on from there to another random braille number, (e.g. 2) then so on and so on forever until it's stopped by someone manually.

void randomNumber() {
     lMode = random(1, 12);
}
void learnNumbersRandom()  {
  recvOneChar();
  randomNumber();
    if (lMode == 1) {
      Serial.println(" ");
      Serial.println("This is the Number character.");
      Serial.println("It is displayed before every number so you know that it is a number and not a letter!");
      lMotor = 1;
      lMode = 12;
    }
    if (lMotor == 1)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
         if (lMode == 2) {
      Serial.println(" ");
      Serial.println("This is 0.");
      lMotor = 2;
      lMode = 12;
    }
    if (lMotor == 2)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
     if (lMode == 3) {
      Serial.println(" ");
      Serial.println("This is 1.");
      lMotor = 3;
      lMode = 12;
    }
    if (lMotor == 3)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
              if (lMode == 4) {
      Serial.println(" ");
      Serial.println("This is 2.");
      lMotor = 4;
      lMode = 12;
    }
    if (lMotor == 4)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
              if (lMode == 5) {
      Serial.println(" ");
      Serial.println("This is 3.");
      lMotor = 5;
      lMode = 12;
    }
    if (lMotor == 5)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
     if (lMode == 6) {
      Serial.println(" ");
      Serial.println("This is 4.");
      lMotor = 6;
      lMode = 12;
    }
    if (lMotor == 6)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
              if (lMode == 7) {
      Serial.println(" ");
      Serial.println("This is 5.");
      lMotor = 7;
      lMode = 12;
    }
    if (lMotor == 7)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
              if (lMode == 8) {
      Serial.println(" ");
      Serial.println("This is 6.");
      lMotor = 8;
      lMode = 12;
    }
    if (lMotor == 8)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
 randomNumber();
     }
              if (lMode == 9) {
      Serial.println(" ");
      Serial.println("This is 7.");
      lMotor = 9;
      lMode = 12;
    }
    if (lMotor == 9)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
      randomNumber();
     }
     if (lMode == 10) {
      Serial.println(" ");
      Serial.println("This is 8.");
      lMotor = 10;
      lMode = 12;
    }
    if (lMotor == 10)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
      randomNumber();
     }
              if (lMode == 11) {
      Serial.println(" ");
      Serial.println("This is 9.");
      lMotor = 11;
      lMode = 12;
    }
    if (lMotor == 11)  {
      //servo_10.write(angle10);
      //servo_11.write(angle11);
      //servo_12.write(angle12);
      //servo_13.write(angle13);
      Serial.println("Motors Activate (This is a placeholder)");
      delay(5000);
      resetServo();
      randomNumber();
     }
}

Basically at the start, I start by saying, get me a random number to start with and then get the random function in the called if statement to get it rolling. Also, think of lMode and lMotor as pairs, only separated because of certain text i want to only print once.

My problem is that it ony displays 2 or 3 of these numbers before it stops and doesn't call a random number again. Is this just part of random() or is it something I've done.

I've also put randomSeed(analogRead(0)); in void setup().

Regards,
Justin

I’ll have to say I’m having a really hard time understanding the purpose of the function learnNumbersRandom() for a couple of reasons:

  1. I don’t know the context from which it is being called, and
  2. When lMode is tested for a number lMode gets set to 12 and lMotor gets set to a number which is immediately evaluated by the next if statement, which it always enters, and lMode gets overwritten by the randomNumber() function. After that, lMode may or may not test equal in the rest of the if statements.

Did you mean to use else if statements?

doesn't call a random number again

what is not calling random number again?

you also have a 5 sec delay between each call.

Sorry if I'm not making this clear, I actually had written out a long response but the website crashed for some reason.

Basically, I'm sure you are aware of Braille and how it looks. In this programme, I want to give the users random numbers (in a physical Braille format using a mechanism I've built). But I don't want to go in order.

To explain my lMode ifs, I only want the text within each statement to print once (but I want to be able to call it again, so I can't put it in void setup()). In order to stop it from printing twice, I need to split my task in two. My task is to tell the user which number I am displaying in Braille, activate the motors I need to make that shape, and then call a new number to display. But, If i did that in one if statement, the text would print forever. Instead, my text bit lMode, is called first, it then sets itself to an integer never called by an if statement (12, which as you can see is not a condition in any statement) so that the if statement is closed. I then immediately activate the motor if statement which is why i say they are pair, but really should be one function.

If I'm reading this correctly, there's no reason that lMotor shouls stop calling randomNumber(). It will go through about 2 or 3 numbers and then just stop, as if randomNumber isn't there.

The reason for the delay is to make sure the user has enough time to read the braille.

Apologies if this isn't making it clearer, but thanks.

For help with code, post ALL of the code. We need to see how you have declared variables, etc.

By first, use Ctrl-T in the program editor, to format the code for easy reading.

But, If i did that in one if statement, the text would print forever. Instead

don't understand why

i think part of the problem is your call to randomNumber() at the top of loop(). in subsequent calls w/in if statements, if the value returned by randomNumber() is not one of the "ifs" following the call and before the bottom of loop(), it won't be recognized because the next call to loop calls randomNumber() and lmode is overwritten.

it seems randomNumber() should be called once at the top of loop() and the value of lmode tested by the if statements and some action performed. That action can be both moving the servo and the print. you can delay for 5000 at the bottom and only at the bottom of loop().

each invocation of loop() processes on random number and waits 5 sec before repeating

It was very long for my initial post probably would’ve surpassed the limit. And as I have a small number of posts, I’m on a 5 minute post restriction but here it is:

servobutton.ino (24.2 KB)

Do yourself and forum members a favor, and add some comments to the code explaining what the code is supposed to do.

If you put this project away for six months, you will forget why you did anything.

I know, I'm in a rush to get this done for a competition so they are the least of my worries.

void randomNumber() {
  lMode = random(1, 12);
}
void learnNumbersRandom()  {
  recvOneChar();
  randomNumber();
  if (lMode == 1) {
    Serial.println(" ");
    Serial.println("This is the Number character.");
    Serial.println("It is displayed before every number so you know that it is a number and not a letter!");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
  }
  if (lMode == 2) {
    Serial.println(" ");
    Serial.println("This is 0.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
  }
  if (lMode == 3) {
    Serial.println(" ");
    Serial.println("This is 1.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
  }
  if (lMode == 4) {
    Serial.println(" ");
    Serial.println("This is 2.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    randomNumber();
  }
  if (lMode == 5) {
    Serial.println(" ");
    Serial.println("This is 3.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    randomNumber();
  }
  if (lMode == 6) {
    Serial.println(" ");
    Serial.println("This is 4.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
  if (lMode == 7) {
    Serial.println(" ");
    Serial.println("This is 5.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
  if (lMode == 8) {
    Serial.println(" ");
    Serial.println("This is 6.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
  if (lMode == 9) {
    Serial.println(" ");
    Serial.println("This is 7.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
  if (lMode == 10) {
    Serial.println(" ");
    Serial.println("This is 8.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
  if (lMode == 11) {
    Serial.println(" ");
    Serial.println("This is 9.");
    lMode = 12;
    //servo_10.write(angle10);
    //servo_11.write(angle11);
    //servo_12.write(angle12);
    //servo_13.write(angle13);
    Serial.println("Motors Activate (This is a placeholder)");
    delay(5000);
    resetServo();
    
  }
}

This is it a bit more streamlined. I know everybody asks for all the code but lMode is declared an int, resetServo set all servos to 90degrees and that's about it. I have another function almost identical to this but it calls the numbers in order and it runs correctly so it's not a problem of not being called incorrectly.

From what I gather, it's a problem with me setting lMode to 12 and randomNumber clashing together?

Explain why you set lMode to 12, and what the program should do in that case.

"clashing together" is meaningless.

If you use else ifs or a switch then you won't have to set lMode = 12;

Also, instead of:

    if (textMode != 7)  {
      textMode = 0;
    }
    if (textMode == 0)  {
      Serial.println("TMode");
    }
    textMode = 7;

why not just...

    if (textMode != 7)  {
      Serial.println("TMode");
      textMode = 7;
    }

Explain why you set lMode to 12, and what the program should do in that case.

There is no reason now that I've looked over it and I've removed it completely.

If you use else ifs or a switch then you won't have to set lMode = 12;

Also, instead of:...

Thanks, as you've all probably guessed, I'm not exactly a master of code so I appreciate the little refinement :slight_smile:

I think I've narrowed it down to the function not being called repeatedly, instead just defaulting back to it's parent function LearnNumbers(). Everytime I enter the key to ' activate' LearnNumbersRandom it gives me a random output, but just not in a row by itself. I just need to figure out how to get the program to be called until it is told not to.

This may not be particularly correct but I have gotten the problem solved by getting the if statement to call the same function again.

Thanks to everybody,
Justin