Displaying random char string

In sendinfo(); label i would like it to choose a random char string 1-6 I tried various ways with creating a new variable ''xstring" and in the label stating “xstring = random(1,6):” but i cant figure out how to change “printStringWithShift(string1, 100);” to display a random char string.

char string1[] = "  A  "; 
char string2[] = "  B  ";
char string3[] = "  C  "; 
char string4[] = "  D  ";
char string5[] = "  E  "; 
char string6[] = "  F  ";


void setup(){
  m.init(); 
  m.setIntensity(5); 
}


void loop(){
  Sendinfo();
}



void Sendinfo(){

 printStringWithShift(string1, 100);
 
}

MAX7219_test.ino (8.34 KB)

Take a look at this:
http://arduino.cc/en/Reference/Random

Try this then expand or change it to do what you want

char bb[] = {"QWERTYUIOPASDFGHJKLZXCVBNM"};

void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  Serial.println(bb[random(0, 25)]);
  delay(1000);
}

Note that as it stands, each time that the program is run it will produce the same sequence of letters.

UKHeliBob: Try this then expand or change it to do what you want

char bb[] = {"QWERTYUIOPASDFGHJKLZXCVBNM"};

void setup() {   Serial.begin(115200); }

void loop() {   Serial.println(bb[random(0, 25)]);   delay(1000); }



Note that as it stands, each time that the program is run it will produce the same sequence of letters.

ok but what if there were a "char bb" and a "char bb2" and you dont want to randomize each char in the array but each array

char string1[] = "  red  "; 
char string2[] = "  blue  ";
char string3[] = "  green  ";

you want to display any of the strings 1-3

Is very easy too, but you need to make some changes.

See the example:

char string[3][4] = { "abc", "def", "ghi"};

void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
    randomSeed(analogRead(0));
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(string[random(0, 3)]);
  delay(1000);
}

Its working great thanks.

In "char string[3][4]" what do the 3 and 4 mean, is it memory locations from 3 to 4?

In this pseudo code example..... "char string[3][4] = { "msg1", "msg2", "msg3"};"

I changed "msg1" to a very long sentence and I got a error, so I changed the [4] to [200] and got a error i exceeded SRAM memory of 2048. I then changed it to [180] and it checked out fine. SRAM showed I nearly used all the memory.

I kept "msg1" the same and changed "msg2" to also a very long sentence, I checked that and it came back with a error as expected, but now I was able to change [180] to [300] and have everything work fine? SRAM shows I only used half the memory. :open_mouth:

ac21: In "char string[3][4]" what do the 3 and 4 mean, is it memory locations from 3 to 4?

3 is the number of strings and 4 is the number of characters in each string +1 (you need this because the C compiler add one special character at the end of each string).

This strings are stored in code memory, so, that error is strange. Can you do the same but with the line of the string like:

const char string[3][300] = { "abc", "def", "ghi"};

luisilva: This strings are stored in code memory, so, that error is strange. Can you do the same but with the line of the string like:

const char string[3][300] = { "abc", "def", "ghi"};

I get a invalid conversion error in the call

 printStringWithShift(string[random(0, 4)],100);

It is not saving in program memory. Each string will have different number of characters some 200 and some 10, it seems wasteful to allocate memory space of 200 for a 10 character word? Is there a option of not defining?

There are a couple of issues here:

const char string[3][300] = { "abc", "def", "ghi"};

printStringWithShift(string[random(0, 4)],100);

First, I didn’t see the code for the printStringWithShift(), so I don’t really know what it is supposed to do. Second, I don’t know what the purpose of the 100 is for the second argument. Third, the random() call should be with the arguments 0, 3 not 0, 4. C arrays start with 0, so only elements 0, 1, and 2 are valid for your example and the upper limit parameter is exclusive for random().

Is this what you’re trying to do?

const char string[3][300] = { "abc", "def", "ghi"};

void printStringWithShift(const char *str) {
  Serial.println(str);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  randomSeed(analogRead(0));
}


void loop() {
  // put your main code here, to run repeatedly:
  int index = random(0, 3);
  Serial.print("Index = ");
  Serial.println(index);
  printStringWithShift(string[index]);
  delay(500);
}

Good answer econjack. Thanks for the help. :)

I forgot to address your wasted memory question. The program I just posted used 4124 bytes, with a good chunk being wasted because of the "300" element size. The following line

const char string[3][300] = { "abc", "def", "ghi"};

is wasteful. You can use Purdum's Right-Left Rule to verbalize this definition as: "string is an array of 3 elements each holding 300 chars which are constants".

Now write the same line as:

const char *string[] = { "abc", "def", "ghi"};

and the code size shrinks to 3238 bytes, a savings of 886 bytes. The reason it is smaller is because the compiler now allocates only enough space for each string constant. Note that you don't have to supply the element size when you have a complete initializer list. (You could have written your line as: const char string[][300] = { "abc", "def", "ghi"};) This definition is verbalized with the Right-Left Rule as: "string is an array of pointers to char which are constants."

One more time you are right. All that you point in this last reply is my fault, but I try to do only a simple example to show how this can be used, and I didn't know at that time that some of the strings can have 300 chars, what I have at the time I wrote this example was something like this:

char string1[] = "  A  "; 
char string2[] = "  B  ";
char string3[] = "  C  "; 
char string4[] = "  D  ";
char string5[] = "  E  "; 
char string6[] = "  F  ";


void setup(){
  m.init(); 
  m.setIntensity(5); 
}


void loop(){
  Sendinfo();
}



void Sendinfo(){

 printStringWithShift(string1, 100);

}

@luisilva: No, I don't mean to seem like I'm pointing out errors in yours or anyone else's code. I'm simply trying to show how alternatives exist that might be useful. I'm a retired professor who used to teach programming, and I can't seem to get out of the habit.

“printStringWithShift” is sending one string “abc” to a dot matrix and the 100 is the scroll speed, the random 0,4 is just a error as i was adjusting things to see how it would work. correction of how it is really has been made below.

econjack:
There are a couple of issues here:

const char string[3][300] = { "abc", "def", "ghi"};

printStringWithShift(string[random(0, 3)],100);




First, I didn't see the code for the *printStringWithShift()*, so I don't really know what it is supposed to do. Second, I don't know what the purpose of the 100 is for the second argument. Third, the *random()* call should be with the arguments 0, 3 not 0, 4. C arrays start with 0, so only elements 0, 1, and 2 are valid for your example and the upper limit parameter is exclusive for *random()*.

Is this what you're trying to do?



const char string[3][300] = { “abc”, “def”, “ghi”};

void printStringWithShift(const char *str) {
  Serial.println(str);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  randomSeed(analogRead(0));
}

void loop() {
  // put your main code here, to run repeatedly:
  int index = random(0, 3);
  Serial.print("Index = ");
  Serial.println(index);
  printStringWithShift(string[index]);
  delay(500);
}

I put this code in but but the matrix isnt displaying anything but on the serail monitor i do see it working as it should, it looks like the speed at what it should shift at is required

void Sendinfo(){

 int index = random(0, 3);
  Serial.print("Index = ");
  Serial.println(index);
  printStringWithShift(string[index]);
  delay(500); 
  
}

void printCharWithShift(char c, int shift_speed){
  if (c < 32) return;
  c -= 32;
  memcpy_P(buffer, CH + 7*c, 7);
  m.writeSprite(32, 0, buffer);
  m.setColumn(32 + buffer[0], 0);
  
  for (int i=0; i<buffer[0]+1; i++) 
  {
    delay(shift_speed);
    m.shiftLeft(false, false);
  }
}

void printStringWithShift(char* s, int shift_speed){
  while (*s != 0){
    printCharWithShift(*s, shift_speed);
    s++;

  }

}

does this method load the strings into program memory?