Trying to randomize a selection of a word

Hi all, I'm trying to make a program to randomly choose from a selection of words to blink it with Morse code via a LED. However, even when I used a randomSeed() and using a analog port with nothing attached, the word always is 'BISTRO' when I use the roll() function. How can I get the word to randomize differently each time I start the sketch!

Thanks in Advance!

int port = 12;
int randPort = A0;
int repeatWord = 2750;
int betweenLetter = 1250;
int dot = 400;
int dash = 1000;
int between = 300;
void setup() {
  // put your setup code here, to run once:
//Sets up port for MORSE LED
 pinMode(port, OUTPUT);
 //Sets up analog port - connected to nothing to make random values
 pinMode(randPort, INPUT);
 Serial.begin(9600);
}
//Gets an word for the morse to use - only once
String message = roll();

void loop() {
  // put your main code here, to run repeatedly:
  //Morse playing, repeats
  Serial.println(message);
  Morse(message);
  delay(repeatWord);
}
//Program for the dot of a morse
void smalls(int freq) {
  for(int i = 0; i<freq; i++){
    digitalWrite(port, HIGH);
  delay(dot);
  digitalWrite(port,LOW);
  delay(between);
  }
  
}
//Program for the dash of a morse
void larges(int freq) {
  for(int i = 0; i<freq; i++){
    digitalWrite(port, HIGH);
  delay(dash);
  digitalWrite(port,LOW);
  delay(between);
}

} 
//Program to get the morse to run
void Morse(String code) {
  for(int i = 0; i <code.length(); i++){
    String section = code.substring(i,i+1);
    if(section.equals("A") || section.equals("a")){
      smalls(1);
      larges(1);
    }
    else if(section.equals("B") || section.equals("b") ){
      larges(1);
      smalls(3);
    }
    else if(section.equals("C") || section.equals("c") ){
      larges(1);
      smalls(1);
      larges(1);
      smalls(1);
    }
    else if(section.equals("D") || section.equals("d")){
      larges(1);
      smalls(2);
    }
    else if(section.equals("E") || section.equals("e")){
      smalls(1);
    }
    else if(section.equals("F") || section.equals("f")){
      smalls(2);
      larges(1);
      smalls(1);
    }
    else if(section.equals("G") || section.equals("g")){
      larges(2);
      smalls(1);
    }
    else if(section.equals("H") || section.equals("h")){
      smalls(4);
    }
    else if(section.equals("I") || section.equals("i")){
      smalls(2);
    }
    else if(section.equals("J") || section.equals("j")){
      smalls(1);
      larges(3);
    }
    else if(section.equals("K") || section.equals("k")){
      larges(1);
      smalls(1);
      larges(1);
    }
    else if(section.equals("L") || section.equals("l")){
      smalls(1);
      larges(1);
      smalls(2);
    }
    else if(section.equals("M") || section.equals("m")){
      larges(2);
    }
    else if(section.equals("N") || section.equals("n")){
      larges(1);
      smalls(1);
    }
    else if(section.equals("O") || section.equals("o")){
      larges(3);
    }
    else if(section.equals("P") || section.equals("p")){
      smalls(1);
      larges(2);
      smalls(1);
    }
    else if(section.equals("Q") || section.equals("q")){
      larges(2);
      smalls(1);
      larges(1);
    }
    else if(section.equals("R") || section.equals("r")){
      smalls(1);
      larges(1);
      smalls(1);
    }
    else if(section.equals("S") || section.equals("s")){
      smalls(3);
    }
    else if(section.equals("T") || section.equals("t")){
      larges(1);
    }
    else if(section.equals("U") || section.equals("u")){
      smalls(2);
      larges(1);
    }
    else if(section.equals("V") || section.equals("v")){
      smalls(3);
      larges(1);
    }
    else if(section.equals("W") || section.equals("w")){
      smalls(1);
      larges(2);
    }
    else if(section.equals("X") || section.equals("x")){
      larges(1);
      smalls(2);
      larges(1);
    }
    else if(section.equals("Y") || section.equals("y")){
      larges(1);
      smalls(1);
      larges(2);
    }
    else if(section.equals("Z") || section.equals("z")){
      larges(2);
      smalls(2);
    }
    else if(section.equals("0")){
      larges(5);
    }
    else if(section.equals("1")){
      smalls(1);
      larges(4);
    }
    else if(section.equals("2")){
      smalls(2);
      larges(3);
    }
    else if(section.equals("3")){
      smalls(3);
      larges(2);
    }
    else if(section.equals("4")){
      smalls(4);
      larges(1);
    }
    else if(section.equals("5")){
      smalls(5);
    }
    else if(section.equals("6")){
      larges(1);
      smalls(4);
    }
    else if(section.equals("7")){
      larges(2);
      smalls(3);
    }
    else if(section.equals("8")){
      larges(3);
      smalls(2);
    }
    else{
      larges(4);
      smalls(1);
    }
    delay(betweenLetter);
  }
}
//Gets a random word to use in the program - doesnt reset on strikes!
String roll(){
   randomSeed(analogRead(randPort));
   int seed = random(16);
   Serial.println(seed);
   if(seed == 0){
   return "SHELL";
   }
   else if(seed == 1){
   return "HALLS";
   }
   else if(seed == 2){
   return "SLICK";
   }
   else if(seed == 3){
   return "TRICK";
   }
   else if(seed == 4){
   return "BOXES"; 
   }
   else if(seed == 5){
   return "LEAKS"; 
   }
   else if(seed == 6){
   return "STROBE";
   }
   else if(seed == 7){
   return "BISTRO";
   }
   else if(seed == 9){
   return "FLICK";
   }
   else if(seed == 10){
   return "BOMBS";
   }
   else if(seed == 11){
   return "BREAK";
   }
   else if(seed == 12){
   return "BRICK";
   }
   else if(seed == 13){
   return "STEAK";
   }
   else if(seed == 14){
   return "STING";
   }
   else if(seed == 15){
   return "VECTOR";
   }
   
   else{
   return "BEATS";
   }
}

This comment is not correct. For analogRead(), pinMode() is not used.

 //Sets up analog port - connected to nothing to make random values
 pinMode(randPort, INPUT);

There is no guarantee that analogRead() on a disconnected pin will return a random value. The voltage on the pin may be stuck at 0, Vcc (1023) or something in between. There are better approaches to randomizing the generator, like timing the random pushes of a button.

The current version.

Thanks, but even when I put that in it still only displays 'BISTRO' in the Serial whenever I start the sketch. Is the fact that I'm using the random() in a different method causing the problem?

even when I put that in

You probably made a mistake. Post the code, every time, so we have some idea what you are talking about.

The best approach is to break the problem down. Forget the current code, and write a tiny program that works just to generate a single random number, different every time you restart the Arduino.

1 Like

I just made a new smaller code and it works! Put it back into the project and I got it working. Thanks and sorry if this was simple, im still quite new.

#include <EEPROM.h>
int port = 12;
int randPort = A0;
int repeatWord = 2750;
int betweenLetter = 1250;
int dot = 400;
int dash = 1000;
int between = 300;
String message;
void reseedRandom( void )
{
  static const uint32_t HappyPrime = 937;
  union
  {
    uint32_t i;
    uint8_t b[4];
  }
  raw;
  int8_t i;
  unsigned int seed;
  
  for ( i=0; i < sizeof(raw.b); ++i )
  {
    raw.b[i] = EEPROM.read( i );
  }

  do
  {
    raw.i += HappyPrime;
    seed = raw.i & 0x7FFFFFFF;
  }
  while ( (seed < 1) || (seed > 2147483646) );

  randomSeed( seed );  

  for ( i=0; i < sizeof(raw.b); ++i )
  {
    EEPROM.write( i, raw.b[i] );
  }
}

void setup() {
  // put your setup code here, to run once:
//Sets up port for MORSE LED
 pinMode(port, OUTPUT);
 reseedRandom();
 Serial.begin(9600);
 //Gets an word for the morse to use - only once
 message = roll(random(16));
}

void loop() {
  // put your main code here, to run repeatedly:
  //Morse playing, repeats
  Serial.println(message);
  Morse(message);
  delay(repeatWord);
}
//Program for the dot of a morse
void smalls(int freq) {
  for(int i = 0; i<freq; i++){
    digitalWrite(port, HIGH);
  delay(dot);
  digitalWrite(port,LOW);
  delay(between);
  }
  
}
//Program for the dash of a morse
void larges(int freq) {
  for(int i = 0; i<freq; i++){
    digitalWrite(port, HIGH);
  delay(dash);
  digitalWrite(port,LOW);
  delay(between);
}

} 
//Program to get the morse to run
void Morse(String code) {
  for(int i = 0; i <code.length(); i++){
    String section = code.substring(i,i+1);
    if(section.equals("A") || section.equals("a")){
      smalls(1);
      larges(1);
    }
    else if(section.equals("B") || section.equals("b") ){
      larges(1);
      smalls(3);
    }
    else if(section.equals("C") || section.equals("c") ){
      larges(1);
      smalls(1);
      larges(1);
      smalls(1);
    }
    else if(section.equals("D") || section.equals("d")){
      larges(1);
      smalls(2);
    }
    else if(section.equals("E") || section.equals("e")){
      smalls(1);
    }
    else if(section.equals("F") || section.equals("f")){
      smalls(2);
      larges(1);
      smalls(1);
    }
    else if(section.equals("G") || section.equals("g")){
      larges(2);
      smalls(1);
    }
    else if(section.equals("H") || section.equals("h")){
      smalls(4);
    }
    else if(section.equals("I") || section.equals("i")){
      smalls(2);
    }
    else if(section.equals("J") || section.equals("j")){
      smalls(1);
      larges(3);
    }
    else if(section.equals("K") || section.equals("k")){
      larges(1);
      smalls(1);
      larges(1);
    }
    else if(section.equals("L") || section.equals("l")){
      smalls(1);
      larges(1);
      smalls(2);
    }
    else if(section.equals("M") || section.equals("m")){
      larges(2);
    }
    else if(section.equals("N") || section.equals("n")){
      larges(1);
      smalls(1);
    }
    else if(section.equals("O") || section.equals("o")){
      larges(3);
    }
    else if(section.equals("P") || section.equals("p")){
      smalls(1);
      larges(2);
      smalls(1);
    }
    else if(section.equals("Q") || section.equals("q")){
      larges(2);
      smalls(1);
      larges(1);
    }
    else if(section.equals("R") || section.equals("r")){
      smalls(1);
      larges(1);
      smalls(1);
    }
    else if(section.equals("S") || section.equals("s")){
      smalls(3);
    }
    else if(section.equals("T") || section.equals("t")){
      larges(1);
    }
    else if(section.equals("U") || section.equals("u")){
      smalls(2);
      larges(1);
    }
    else if(section.equals("V") || section.equals("v")){
      smalls(3);
      larges(1);
    }
    else if(section.equals("W") || section.equals("w")){
      smalls(1);
      larges(2);
    }
    else if(section.equals("X") || section.equals("x")){
      larges(1);
      smalls(2);
      larges(1);
    }
    else if(section.equals("Y") || section.equals("y")){
      larges(1);
      smalls(1);
      larges(2);
    }
    else if(section.equals("Z") || section.equals("z")){
      larges(2);
      smalls(2);
    }
    else if(section.equals("0")){
      larges(5);
    }
    else if(section.equals("1")){
      smalls(1);
      larges(4);
    }
    else if(section.equals("2")){
      smalls(2);
      larges(3);
    }
    else if(section.equals("3")){
      smalls(3);
      larges(2);
    }
    else if(section.equals("4")){
      smalls(4);
      larges(1);
    }
    else if(section.equals("5")){
      smalls(5);
    }
    else if(section.equals("6")){
      larges(1);
      smalls(4);
    }
    else if(section.equals("7")){
      larges(2);
      smalls(3);
    }
    else if(section.equals("8")){
      larges(3);
      smalls(2);
    }
    else{
      larges(4);
      smalls(1);
    }
    delay(betweenLetter);
  }
}
//Gets a random word to use in the program - doesnt reset on strikes!
String roll(int seed){
   if(seed == 0){
   return "SHELL";
   }
   else if(seed == 1){
   return "HALLS";
   }
   else if(seed == 2){
   return "SLICK";
   }
   else if(seed == 3){
   return "TRICK";
   }
   else if(seed == 4){
   return "BOXES"; 
   }
   else if(seed == 5){
   return "LEAKS"; 
   }
   else if(seed == 6){
   return "STROBE";
   }
   else if(seed == 7){
   return "BISTRO";
   }
   else if(seed == 9){
   return "FLICK";
   }
   else if(seed == 10){
   return "BOMBS";
   }
   else if(seed == 11){
   return "BREAK";
   }
   else if(seed == 12){
   return "BRICK";
   }
   else if(seed == 13){
   return "STEAK";
   }
   else if(seed == 14){
   return "STING";
   }
   else if(seed == 15){
   return "VECTOR";
   }
   
   else{
   return "BEATS";
   }
}

A smaller and better version of that randomizing code is found in the "final" post of that thread, here:

You are calling roll() to initialize a global variable. This is done BEFORE the Arduino core is initialized. It is entirely possible that "analogRead()" returns a constant if called before the ADC hardware is initialized. Setting randomSeed() to a constant will always produce the same result.

Move the call to 'randomSeed()' to setup() and initialize 'message' after that in setup(). That has a chance of fixing the problem.

Do not use pinMode() on pins used only for analogRead(). On a Nano, setting pinMode(A7, INPUT) changes Pin 9 to INPUT.