Chars, strings and oled help please

Hi 1st post asking for help, been trying to set up a nano, and a 128 x 64 oled for use on my model rail set up, i want the display to act as an arrival / departures board generating random times and destinations.

I can get it running using fixed times & destinations but am having issues with the random part, my current sketch is below.

any help would be greatly appreciated.

Steve


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


char *myString[]={"London","Birmingham","Manchester","Munich","Berlin","Hamburg","Paris","Madrid","Rome","Amsterdam","Brussells",};
char *yoString[]={"10:15","10:20","10:25","10:30","11:15","11:25","11:35",};


void setup()   {

  Serial.begin(9600);
  delay(250); // wait for the OLED to power up
  display.begin(i2c_Address, true); // Address 0x3C default

  display.display();
  delay(2000);
  display.clearDisplay();
}
  
void loop()
{

  display.setTextSize(1.5);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("       ARRIVALS");
  display.setTextSize(1);
  display.println("10:20 Stutgartd");
  display.println("10:43 London");
  display.println("10:50 Hamburg");
  display.println("11:02 Paris");
  display.println("11:13 Rome");
  display.print("11:26"); display.println(" Berlin");
  display.display();
  delay(2000);
  display.clearDisplay();


  // text display tests
//  display.setTextSize(1.5);
//  display.setTextColor(SH110X_WHITE);
//  display.setCursor(0, 0);
//  display.println("       DEPARTURES");
//  display.setTextSize(1);
//  display.println("12:20 2Stutgartd");
//  display.println("12:43 2London");
//  display.println("12:50 2Hamburg");
//  display.println("13:02 2Paris");
//  display.println("13:13 2Rome");
//  display.print("13:26"); display.println(" 2Berlin");
//  display.display();
//  delay(2000);
//  display.clearDisplay();

  display.setTextSize(1.5);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("       DEPARTURES");
  display.setTextSize(1);


 
  for (int i = 0; i < 9; i++) {
  
   display.println(yoString[i+1])+(myString[i+1]);
   display.println(yoString[i+2]);(myString[i]);   
   display.println(yoString[i+3]);(myString[i]);
   display.println(yoString[i+4]);(myString[i]);     
   display.println(yoString[i+5]);(myString[i]);   
   display.println(yoString[i+6]);(myString[i]);
   display.println(yoString[i+7]);(myString[i]);  
  display.display();
  delay(2000);
  display.clearDisplay();

 }
 }

Not sure what is going on there, but those array indices are out of bounds.

Perhaps you want to use String instead of char* and do something like this?

   display.println(yoString[i+1] + myString[i+1]);

"+" does not work with C-strings (zero terminated character arrays).

To concatenate C-strings, use strcat(), or better, strncat().

https://www.cplusplus.com/reference/string/string/

Didn't check the types, my mistake.

this part does part of what i want in that it displays the times in a column, but i can't get the code to add the destinations, as you can see i've tried several ways of doing this but none seem to work.

You haven't written any code to display the destinations.
And the indices for the times are out of bounds.

The above does nothing. What did you intend it to do?

sorry part of sketch not developed there i have just amended to show

   display.print(yoString[i+1]);display.print(" ");display.println(myString[i+1]);
   display.print(yoString[i+2]);display.print(" ");display.println(myString[i+2]);   
   display.print(yoString[i+3]);display.print(" ");display.println(myString[i+4]);
   display.print(yoString[i+4]);display.print(" ");display.println(myString[i+7]);     
   display.print(yoString[i+2]);display.print(" ");display.println(myString[i+6]);   
   display.print(yoString[i+3]);display.print(" ");display.println(myString[i+2]);
   display.print(yoString[i+1]);display.print(" ");display.println(myString[i+3]); 

which gets me closer now just have to random it.

it was supposed to be used to select from string but i realise i missed the +1 etc off the end.

thanks

Still does nothing.

And the indices probably go out of bounds. Post ALL of the revised code.

Latest version of sketch


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

char *myString[]={"London","Birmingham","Manchester","Munich","Berlin","Hamburg","Paris","Madrid","Rome","Amsterdam","Brussells",};
char *yoString[]={"10:15","10:20","10:25","10:30","11:15","11:25","11:35",};
void setup()   {

  Serial.begin(9600);
  delay(250); // wait for the OLED to power up
  display.begin(i2c_Address, true); // Address 0x3C default
  display.display();
  delay(2000);
  display.clearDisplay();
}
  
void loop()
{

  display.setTextSize(1.5);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("       ARRIVALS");
  display.setTextSize(1);
  display.println("10:20 Stutgartd");
  display.println("10:43 London");
  display.println("10:50 Hamburg");
  display.println("11:02 Paris");
  display.println("11:13 Rome");
  display.print("11:26"); display.println(" Berlin");
  display.display();
  delay(2000);
  display.clearDisplay();


  // text display tests
//  display.setTextSize(1.5);
//  display.setTextColor(SH110X_WHITE);
//  display.setCursor(0, 0);
//  display.println("       DEPARTURES");
//  display.setTextSize(1);
//  display.println("12:20 2Stutgartd");
//  display.println("12:43 2London");
//  display.println("12:50 2Hamburg");
//  display.println("13:02 2Paris");
//  display.println("13:13 2Rome");
//  display.print("13:26"); display.println(" 2Berlin");
//  display.display();
//  delay(2000);
//  display.clearDisplay();

  display.setTextSize(1.5);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("       DEPARTURES");
  display.setTextSize(1);
 
  for (int i = 0; i < 9; i++) {
  
   display.print(yoString[i+1]);display.print(" ");display.println(myString[i+1]);
   display.print(yoString[i+2]);display.print(" ");display.println(myString[i+2]);   
   display.print(yoString[i+3]);display.print(" ");display.println(myString[i+4]);
   display.print(yoString[i+4]);display.print(" ");display.println(myString[i+7]);     
   display.print(yoString[i+4]);display.print(" ");display.println(myString[i+6]);   
   display.print(yoString[i+3]);display.print(" ");display.println(myString[i+2]);
   display.print(yoString[i+1]);display.print(" ");display.println(myString[i+3]);
  display.display();
  delay(2000);
  display.clearDisplay();

 }
 }

The indices of yoString and myString go out of bounds, so expect garbage on the display and perhaps program crashes.

i'm sure there will be issues as i am still learning but so far have got this.

Well, have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.