SD card module and SSD1306 OLED display - how can I display regular text instead of integers?

no for //Library Init============================================================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <SPI.h>
#include <SD.h>
//========================================================================

//OLED SCREEN PARAMETERS==================================================
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
//========================================================================

//Push button states======================================================
int button1 = 0; //Selector State
int button2 = 0; //Apply Color State
int button3 = 0; //Random Color State
//========================================================================

//RGB LED Values==========================================================
int red = 0;
int green = 0;
int blue = 0;
//========================================================================

//Selector switch state for choosing R,G,B colors=========================
int sel = 1;
//========================================================================

int potVal = 0;//The Value stored from the potentiometer
int gunVal = 0;//The Value transformed from potVal to
//map(potVal,0,1023,0,255);
//========================================================================
long randNumber;//Used for storing a random number

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

//SD CARD DECLERATION FOR FILE============================================
File myFile;

//SETUP PORTION============================================================
void setup() {
  Serial.begin(9600); //Establish Serial connection
  //========================================================================
  //Pin init. - used pins are 6,9,19,11,12,13
  //========================================================================
  pinMode(11, OUTPUT);//RED
  pinMode(10, OUTPUT);//GREEN
  pinMode(9, OUTPUT);//BLUE
  //========================================================================
  pinMode(13, INPUT); //Button1
  pinMode(12, INPUT); //Button2
  pinMode(6, INPUT); //Button3
  //========================================================================
  randomSeed(analogRead(0)); //Generate seed for random number via
  //analog pin 0
  //========================================================================
  //DISPLAY INIT. FIRST TIME================================================
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D
    //for 128x32
    Serial.println(F("SSD1306 allocation failed"));//If there
    //wasn't enough memory
    for (;;);
  }
  delay(2000);
  display.clearDisplay();//Remove contents of display

  display.setTextSize(1); // Set size of text on OLED
  display.setTextColor(WHITE); // Color for font
  display.setCursor(0, 10); // Control cursor position

  display.println("Starting");  // Display static text
  display.display(); //Show stuff on OLED
  delay(1000);
  display.clearDisplay();
  display.display();


  //SD CARD CODE============================================================
  if (!SD.begin(4)) {
    oledDisplayCenter(String("INIT FAILED"));
    delay(1000);
  }
  oledDisplayCenter(String("INIT DONE"));
  delay(1000);


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    oledDisplayCenter(String("Writing to test.txt..."));
    delay(1000);
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    oledDisplayCenter(String("done"));
    delay(1000);
  } else {
    // if the file didn't open, print an error:
    oledDisplayCenter(String("Error opening test.txt"));
    delay(1000);
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    oledDisplayCenter(String("test.txt:"));
    delay(1000);

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      //    Serial.write(myFile.read());
      String sdStoredString = String(myFile.read());
      oledDisplayCenter(sdStoredString);
      delay(1000);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    oledDisplayCenter(String("ERROR OPENING"));
    delay(1000);
  }



}
//========================================================================
//========================================================================
//========================================================================
void loop() {
  //Button1 Read value is located in selector() function
  button2 = digitalRead(12); //Read value for Button2
  button3 = digitalRead(6);// Read value for Button3
  //========================================================================
  potVal = analogRead(A5); //Use A5 to read from potentiometer
  gunVal = map(potVal, 0, 1023, 0, 255); //Transform the value into
  //a usable 255 PWM value.
  //========================================================================
  selector(); //checks for Selector state by running function
  showDisplay();//Updates display
  //========================================================================
  if (button2 == 1) { //apply color by pressing button 2 in conjuction with
    //selector value
    if (sel == 1) {
      red = gunVal;
    }
    if (sel == 2) {
      green = gunVal;
    }
    if (sel == 3) {
      blue = gunVal;
    }
  }
  //========================================================================
  if (button3 == 1) { //RANDOM NUMBER GENERATOR
    randomSeed(analogRead(0));
    red = random(256);
    green = random(256);
    blue = random(256);
    delay(200);
  }
  //========================================================================
  analogWrite(11, red);//write to RGB LED
  analogWrite(10, green);//write to RGB LED
  analogWrite(9, blue);//write to RGB LED
  //========================================================================
}//END OF LOOP FUNTION

//========================================================================
//========================================================================
//========================================================================
//========================================================================

//FUNCTIONS:::::==========================================================
void selector()//Function to choose next color, loops back to 1
{
  button1 = digitalRead(13); //Read Button 1 State
  if (button1 == 1) {
    if (sel < 3) {
      sel = sel + 1;
      delay(200);
    }
    else {
      sel = 1;
      delay(200);
    }
  }
}
//========================================================================
void showDisplay()//Function to bring contents of RGB selection values
//to OLED display in order to have a real time update on values.
{
  display.clearDisplay();
  display.setCursor(0, 10);
  display.println(String(red) + " " + String(green) + " " + String(blue)
                  + " " + String(gunVal) + " " + String(sel));
  display.display();
}
//========================================================================
void oledDisplayCenter(String text) { //CENTER ANY TEXT BY CALLING FUNCTION
  //AND A STRING INSIDE THE BRACKETS
  int16_t x1;
  int16_t y1;
  uint16_t width;
  uint16_t height;

  display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);

  // display on horizontal and vertical center
  display.clearDisplay(); // clear display
  display.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  display.println(text); // text to display
  display.display();
}
//========================================================================

I can't get the Oled to properly display a string from the SD card file. All I'm getting are integers instead of readable text.

I've tried looking everywhere but I couldn't find anything.

The problem is in the section "SD CARD CODE" which is really just the example for read/write but modified to use the OLED display instead of serial.

I can't see your code (hint) but I'm going to guess that you're printing int values, not char values.

Many members, myself included, are wary of downloading code. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Make helping you as easy as possible and you will get faster help.

Alright, I edited it in text form.

I just wanted to clarify - using the Arduino example SD card read write code it actually displays the file contents from the SD card normally in the serial monitor.

I think it's converting it to an int for some reason instead of human readable text.

The problem is that you are using Strings where they are not needed OR wanted (they never are, and should be avoided as they cause memory problems and program malfunctions).

You should be using C-strings (zero terminated character arrays) instead.

Study the Adafruit GFX docs for examples of proper usage.

read() returns an int, I believe.

Try casting it to char and see how you get on.

It actually worked! But there's a small problem - it's only printing up the letters one at a time.
Can I somehow combine them to show more at once?

    while (myFile.available()) {
  //    Serial.write(myFile.read());
      char sdStoredString = char(myFile.read());
      
      oledDisplayCenter(String(sdStoredString));
    delay(1000);
    }
    // close the file:
    myFile.close();

What's wrong with adding the chars to a String, and then displaying the String, like you were doing before?
(Apart from using a String, obviously)

I don't think the massive delay is doing you any favours either.

Edit: I may have confused this topic with another.

What should I read for the above (this is to test my knowledge on tense)?
What is wrong
or
What was wrong

"what is"
(I don't believe there is a common elision for "what was")

1 Like

Hey, I tried out something based off of your advice.

    while (myFile.available()) {
  //    Serial.write(myFile.read());
     sdStoredString = myFile.read();
     String one = String(sdStoredString);
     sdStoredString = myFile.read();
     String two = String(sdStoredString);
     sdStoredString = myFile.read();
     String three = String(sdStoredString);
     sdStoredString = myFile.read();
     String four = String(sdStoredString);
     sdStoredString = myFile.read();
     String five = String(sdStoredString);

     
     oledDisplayCenter(String(one)+String(two)+String(three)+String(four)+String(five) );

    delay(1000);

    }

Basically, I made 5 variables that can hold the info from the char value, transform it into a string and finally connect everything so that the display can show it. I'll work on the formatting and add some more values but this more or less fixes the issue.

Thank you TheMemberFormerlyKnownAsAWOL

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