0

I'm trying to print a txt file from an SD card to a SSD1306 using the u8x8 library. Here's the current code:

#include <SD.h>

#include <Arduino.h>
#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

  U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);   

/*
  This example will probably not work with the SSD1606, because of the
  internal buffer swapping
*/
#define maxChar  128
#define rows 8
#define cols 16

File myFile;
int x = 0;
int y = 0;
char c;

void setup()
{
  /*Serial.begin(9600);
  Serial.print("Initializing SD... ");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");*/

  u8x8.begin();
  delay(500);
  u8x8.setFont(u8x8_font_amstrad_cpc_extended_f);

  myFile = SD.open("target.txt", FILE_READ);
  u8x8.clear();
  u8x8.print("ciao");
  u8x8.print(myFile.read());
  delay(2000);
  printNewSet();
}
void loop()
{
  
}

void printNewSet()
{
  u8x8.clear();
  x = 0;
  y = 0;
  while (myFile.available() && y<rows)
  {
    c = (char) myFile.read();
    if (c == '\n') c = 'a';
    u8x8.print(c);
    stepCoords();
  }
}

void stepCoords()
{
  x++;
  if (x >= cols) newLine();
}
void newLine()
{
  u8x8.print('\n');
  x = 0;
  y++;
}

The function printNewSet() is supposed to print a batch of characters until either the file ends or the display limit is reached.

The file displayed correctly until i switched from while(myFile.available()) u8x8.print( (char) myFile.read()); to printNewSet(), at which point myFile.read() only returns -1 and myFile.available() is always false.

What is going wrong?

EDIT: While debugging i discovered that myFile fails to be initialized. Am I running out of memory?

2
  • 1
    what Arduino and what are the reported memory usage values?
    – Juraj
    Dec 6 '20 at 8:59
  • I'm on an Uno with 54% rom and 62% ram usage Dec 6 '20 at 9:07
1

I had accidentally commented out the SD initialization along with the serial initialization. After uncommenting it it works correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.