0

Displaying random element from array on OLED
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Displaying random element from array on OLED

by mattmans on Wed Oct 11, 2017 1:00 pm

Hi guys

C isn't my strong subject and I'm struggling with something which I thought would be simple.

I have an array of strings and I want to display a random one on an OLED display each time my Pro Trinket is turned on. I have it working with a single string, but I can't work out how to grab a random one from my array. Can anyone help?

Here's my code

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

String messagesArray[] = { "test 1", "test 2", "test 3" };         #This is the array that I want to pick a random string from

char message[] = "This works";               #This works
int  x, minX;

void setup() {

  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
 
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setTextWrap(false);
  x    = display.width();
  minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2

}

void loop() {
  display.clearDisplay();
  display.setCursor(x, 20);
  display.print(message);
  display.display();
  if(--x < minX) x = display.width();
}


Thanks for your help.

Matt

mattmans
 
Posts: 2
Joined: Wed Oct 11, 2017 12:43 pm

Re: Displaying random element from array on OLED

by adafruit_support_rick on Wed Oct 11, 2017 3:05 pm

Arduino has a random() function and a randomSeed( function. You can read about them here:

You don't want to use a String object for your messages. You can simply use a character array.

Get a random number between 0 and (n-1), where n is the number of messages. Use that to index your array of messages:
Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

char* messagesArray[] = { "test 1", "test 2", "test 3" };         //This is the array that I want to pick a random string from

//Get the nuber of messages in the array
#define MSG_COUNT (sizeof(messagesArray)/sizeof(char*))

//char message[] = "This works";               //This works
int  x, minX;

int messageIndex = 0;

void setup() {

  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
 
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setTextWrap(false);
  x    = display.width();

  randomSeed(analogRead(0));  // initialize the randow number generator. must use an unconnected analog input

}

void loop() {

  messageIndex = random(MSG_COUNT);  //get a random number between 0 and the nuber of messages in the array (exclusive)
  minX = -12 * strlen(messagesArray[messageIndex]); // 12 = 6 pixels/character * text size 2

  display.clearDisplay();
  display.setCursor(x, 20);
  display.print(messagesArray[messageIndex]);
  display.display();
  if(--x < minX) x = display.width();

  delay(1000);
}

adafruit_support_rick
 
Posts: 34768
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Displaying random element from array on OLED

by mattmans on Thu Oct 12, 2017 3:38 am

Thanks so much Rick - that's really helpful. It makes sense now that I've seen it.

I moved the messageIndex = random(MSG_COUNT); line out of the loop and into the setup section because it was changing the message randomly as it was scrolling across - an interesting effect, but not quite what I wanted on this occasion :)

Thanks again for your help!

Matt

mattmans
 
Posts: 2
Joined: Wed Oct 11, 2017 12:43 pm

Re: Displaying random element from array on OLED

by adafruit_support_rick on Thu Oct 12, 2017 8:18 am

You’re welcome!

adafruit_support_rick
 
Posts: 34768
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Please be positive and constructive with your questions and comments.