Working out some coding issues with an OLED display

I am trying to get the typewriter effect to display some text on an OLED 128x64 screen with an Arduino uno and it will be moved to a Nano once I am done with the code. I researched and found a code and adjusted it so that for the most part works. The issues that i am having are that I can't make it loop. If I put it inside the loop it start giving me some errors, second the text goes outside of the scree at the end instead of scrolling to make space for it. I have also tried using the display.setCursor, but all it does is put a letter on top of the other on the same spot. Thank you in advance! Here is the complete code:

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

#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 64     // OLED display height, in pixels
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3C for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void typeWriter(char* text);

void setup() {
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;  // Don't proceed, loop forever
  }
  // Serial.println();
  typeWriter("My name is William");
  //Serial.println();
  typeWriter("Help me out, Please");
  //Serial.println();
}

void loop() {
}

void typeWriter(char* text) {

  display.clearDisplay();
  while (text[0]) {
    display.setTextColor(WHITE);
    display.setTextSize(2);
    //display.setCursor(10, 10);
    display.write(text++[0]);
    display.display();
    delay(100);
  }
}

don't you need code in loop() that monitors the Serial interface and read the input being prompted for and calls typewriter when a complete line is entered, see readBytesUntil()

and then print the prompt again

Thanks for the input. Let me see if I am getting your info right. So i don't need to have it in the loop? So how do I recall this function to keep looping? Do I use Serial.readBytesUntil() to recall it?

no.

  • you need code to read the serial input
  • that code needs to be in loop()

for example

char buf [80];

// -----------------------------------------------------------------------------
void loop()
{
    if (Serial.available ()) {
        int n = Serial.readBytesUntil (';', buf, sizeof(buf)-1);
        buf [n] = '\0';
        Serial.println (buf);
    }
}

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

Do you mean the characters should show up one at a time as you type them? readBytesUntil() will not be of any help.

Or do you mean that after a message that arrives over serial, however fast, it should be placed on the display s l o w l y one character at a time as if being typed? readBytesUntil() will wait for an entire string of characters.

You may be in for more work that you realize, as scrolling on an OLED display is not necessarily gonna come out of the box. You may need to give the appearnce of scrolling with a fair bit of coding.

a7

Right now with the code I have on here, I am getting the effect I want. I want to take one step at a time since I am not a newbie, but I am also not a season veteran by any means. The issue that I am trying to tackle at the time is that the text that I have on does not loop. When it comes out on the OLED screen and its done "writing" it, then it jus doesn't do anything else and the "Help me out, please" line stays on the screen. I would like for it to continue to move to the first line in a looping motion.

Sorry but I am still not really understanding. I do not want or expect you to write my code for me, and I appreciate the help. I want to really understand what your example is doing so I can translate it to my code. Could you explain your example? I want it to make sense to my hard head! I really appreciate you taking your time to help me with this.

no.

  • you need code to read the serial input
  • that code needs to be in loop()

for example

char buf [80];

// -----------------------------------------------------------------------------
void loop()
{
    if (Serial.available ()) {
        int n = Serial.readBytesUntil (';', buf, sizeof(buf)-1);
        buf [n] = '\0';
        Serial.println (buf);
    }
}

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

[/quote]

presumably when you say loop, you mean you want to repeatedly print a prompt, check for input, read the input when it is available and display that input typerwrite style. is this correct?

or do you really only want to read input and display it typerwriter style just once?

when checking for input you need to repeated check if it is available. of course you can't read input if there isn't any.

perhaps you don't understand that the Arduino setup() is executed once and loop() is repeatedly invoked, over and over.

so it makes sense to have something in loop() that checks for input

    if (Serial.available ())

and when it is available read it and possibly wait for input until there's something that indicates the end of input such as a new line char, \n.

        int n = Serial.readBytesUntil (';', buf, sizeof(buf)-1);

does this sound like what you're try to do?

This is exactly what I want. I want my OLED to continuously show the "My name is William and "Help me out, Please" over and over.

I do understand this.

Is this what you want from your code?

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

#define SCREEN_WIDTH 128     // OLED display width, in pixels
#define SCREEN_HEIGHT 64     // OLED display height, in pixels
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3C for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void typeWriter(char* text);

void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
}

void loop() {
  display.setCursor(0,0);
  typeWriter("My name is William");
  display.clearDisplay();
  display.display();
  display.setCursor(0,0);
  typeWriter("Help me out, Please");
}

void typeWriter(char* text) {

  display.clearDisplay();
  while (text[0]) {
    display.setTextColor(WHITE);
    display.setTextSize(2);
    //display.setCursor(10, 10);
    display.write(text++[0]);
    display.display();
    delay(100);
  }
}

Yes, could you explain it please? I really didn't just want the code, I want to learn it as well. I really appreciate it. What type of library is SPI.h?

Here you go...

Not useful to know until you want to have total control on a communication circuit. (you do not want control)

Thank you very much for taking your time to help me out!