Making tekst automatically go to next line using U8x8 library

Hi people,

For a project I’m trying to make connect an Arduino Mkr-Zero, with and Usb host mini V2 and a 5.5" 256x64 Green Graphic OLED Display Module-SPI. I also used the u8x8 library of OliKraus with it, since this device can be used with those libraries. I was experimenting what is possible to programm. I was able to make the screen type the letters I want thanks to the library and I also tried to make extra functions like backspace. There was one function however where I struggled a bit to implement it.
It is the function where if you type a word and you reach the end of the horizontal screen–> word will show totally on the next line(see attachment)

I thought of a way to implement the function:

  • Make an array for string of bytes
  • After spacebar is pressed save the distance between current cursor and end X distance of current line
  • When serialdata is available again and it is a letterkey read it and add it to the array/buffer.
    -if the string in an array is larger than differencelength of current position cursor and end of the screen
    place the word in the next line
    -otherwise place the word in the same line.

This is how i wrote the code!
main code!

#include <Arduino.h>
#include <SPI.h>
// V6 u8g8
#include <U8x8lib.h>

U8X8_SSD1322_NHD_256X64_4W_SW_SPI u8x8(/* clock=*/ 12, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9);
const int ArraySize = 15;
char incomingByte = 0; // for incoming serial data
long timer = 0;
char xdis = 0; //for location on display
char ydis = 0; //for location on display
char stringBuffer[ArraySize]; 
 
void setup(void) {

  Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(LED_BUILTIN, OUTPUT); //sets the led on the arduino as an output

  u8x8.begin(); //starts the library
  u8x8.setPowerSave(0);
  u8x8.setFlipMode(1); //flips the screen for right oriëntation
  u8x8.setFont(u8x8_font_pxplusibmcgathin_r); //this one was the best personally
   
  u8x8.drawString(0,0,"DuoCOM versie 2.0");       
  delay(2000); 
  u8x8.clear(); //clears the screen
}

void loop(void) {
  timer++; //add 1 to the timer
  if (xdis > 31) { //the end of the screen  
     char new2_ydis = ydis;
        new2_ydis = ydis+1;
        u8x8.clearLine(new2_ydis);  // clear next line
    xdis = 0; //start on the left of screen
    ydis ++; //goes one row lower
  }
  else
  {  // stay in the same position
    xdis = xdis;
    ydis = ydis;
  }

  if (ydis > u8x8.getRows()) { //bottom of the screen
    ydis = 0; //back to the top of the screen
    u8x8.clearLine(0);
  }
  else{
  // stay in the same position
    xdis = xdis;
    ydis = ydis;
  }
  
  if (timer > 100000) { //keeps the timer on 1000000
    timer = 1000000;
  }   
  if (Serial1.available() > 0) { //serial input comes in
        digitalWrite(LED_BUILTIN, HIGH); //turn the LED on (HIGH is the voltage level)
    incomingByte = Serial1.read(); //reads the serial data and stores it into a buffer
    delay(50); //an delay for the LED
        digitalWrite(LED_BUILTIN, LOW); //turn the LED on (HIGH is the voltage level)
    if (timer > 100000) { //program can only go further with timer of this value to prevent double characters      
      if (incomingByte >= 33 ) { //print normal keys
        print_keys();
      }
      else if (incomingByte == 32) { //adds a space
        print_keys();
      }
      else if (incomingByte == 8) { //backspace
       backspace();        
      }
      else if (incomingByte == 27) { //clear screen function
        u8x8.clear();
        xdis = 0;
        ydis = 0;
      }
      else if (incomingByte == 13) { //enter function
        char new_ydis = ydis; //char for variable for enter
        new_ydis = ydis+1;
        u8x8.clearLine(new_ydis); // when enter clear the next line
        xdis = 0;
        ydis ++;
      }
      else if (incomingByte == 23) { //up arrow
        xdis = xdis;
        ydis = ydis -1;
        u8x8.setCursor(xdis, ydis);
      }
      else if (incomingByte == 19) { //down arrow
        xdis = xdis;
        ydis = ydis +1;
        u8x8.setCursor(xdis, ydis);
      }
       else if (incomingByte == 1) { //left arrow
        xdis = xdis -1;
        ydis = ydis ;
        u8x8.setCursor(xdis, ydis);
      }
       else if (incomingByte == 4) { //right arrow
        xdis = xdis +1;
        ydis = ydis ;
        u8x8.setCursor(xdis, ydis);
      }
      else {
         print_keys();
      }
      timer = 0; //reset the timer when actions are done
    }
  }
}

function which i tried to implement

void print_keys(void) {
char byteofString = 0; // char used for the array
char counter = 0; // used as a counter everytime data is available
char xEndDis = 31; //end distance of horizontal led screen
char xdisDiff = 0; // xdis difference
xdisDiff = xEndDis - xdis; // max value of screen minus current position of xdis cursor
if (Serial1.available() > 0 ) { //serial input comes in
  counter++;      
    byteofString = Serial1.read(); //reads the serial data and stores it into a buffer
    if (byteofString >= 33) { // when a normal key is pressed
      
      if (xdis > 31 && xdisDiff > counter) //when the size of the string is greater than the max length print string in new line
      {
        char new2_ydis = ydis;
          new2_ydis = ydis+1;
          u8x8.clearLine(new2_ydis); //clear the next line 
          stringBuffer[counter] = byteofString; // buffer with the string of bytes
          u8x8.write(stringBuffer); //write the string
          xdis ++; //cursor goes to the next spot
      }
      else //stay in the same line 
      {
          stringBuffer[counter] = byteofString; // buffer with the string of bytes
          u8x8.write(stringBuffer); //write the string
          xdis ++; //cursor goes to the next spot
      }
    }
   else
    {
      xdis ++; // just put cursor on next position
    }
   } 
  }

But I keep getting this error message.

my error message!

Arduino: 1.8.9 (Windows 7), Board: "Arduino MKRZERO"
print_keys:18:34: error: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
           u8x8.write(stringBuffer); //write the string
                                  ^
In file included from N:\Users\Stage\_opdrachten\009-DuoComm Groot Display\software\V8-ArduinoMKRZero - kopie (2)\oled55 u8x8\oled55\oled55.ino:4:0:
C:\Users\stage3\Documents\Arduino\libraries\U8g2\src/U8x8lib.h:284:12: note:   initializing argument 1 of 'virtual size_t U8X8::write(uint8_t)'
     size_t write(uint8_t v);
            ^~~~~
print_keys:24:34: error: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
           u8x8.write(stringBuffer); //write the string
                                  ^
In file included from N:\Users\Stage\_opdrachten\009-DuoComm Groot Display\software\V8-ArduinoMKRZero - kopie (2)\oled55 u8x8\oled55\oled55.ino:4:0:
C:\Users\stage3\Documents\Arduino\libraries\U8g2\src/U8x8lib.h:284:12: note:   initializing argument 1 of 'virtual size_t U8X8::write(uint8_t)'
     size_t write(uint8_t v);
            ^~~~~
Using library SPI at version 1.0 in folder: C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\libraries\SPI 
Using library U8g2 at version 2.26.14 in folder: C:\Users\stage3\Documents\Arduino\libraries\U8g2 
Using library Wire at version 1.0 in folder: C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\libraries\Wire 
exit status 1
invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]

So i tried some different things like changing the array to uint8_t making and making an unsigned char of an array. But i keep getting a similar kind of error. Does somebody know how I can solve this error in this case? Or maybe an idea to a different approach to what I tried to accomplish with my intended function. Suggestions are welcome and appreciated
Thank you in advance

leon

The write method accepts a single byte (aka uint8_t), not an array.

TheMemberFormerlyKnownAsAWOL:
The write method accepts a single byte (aka uint8_t), not an array.

How is about this:

Serial.write(myArray, sizeof(myArray));

GolamMostafa:
How is about this:

Serial.write(myArray, sizeof(myArray));

I was hoping my hint might lead the OP to the correct answer.

TheMemberFormerlyKnownAsAWOL: The write method accepts a single byte (aka uint8_t), not an array.

Ah ok i was thinking of a way of maybe fooling the u8x8.write into thinking it was not an array. But thank you for the hint.

GolamMostafa: How is about this:

Serial.write(myArray, sizeof(myArray));

I will try it out to see what the results are, thanks in advance.

So i tried the suggestions and i was expecting something to see on the oled(thought it would show nothing until end of screen was reached)but only got the error that there was no matching function for it.
U8x8.write can also only read bytes in and even sizeoff tip it still had the problem that it would only be able
to read one argument.

So I tried a different approach for that one function with printing tekst to the nextline. This one was based off the post of Robin2https://forum.arduino.cc/index.php?topic=396450.
So I first wanted to try if i can print a string of bytes after detecting that spacebar( which means when it serial reads a 32) is pressed.
If this is succesfull then I want to add something that will print the string after spacebar is pressed and after detecting that the string is longer than end of the screen.
To print it on the screen i was trying to implement u8x8.print, since i saw on this site u8x8reference#print that it only needed one argument. The argument could also be a string.

This is the code i wrote
the only thing i changed in mainfunction is adding the boolean newData so i dont think it’s needed to also add it here.

void print_keys(void) {
  char xEndDis = 31; //end distance of horizontal led screen
  char xdisDiff = 0; // xdis difference  
  char arrIndex = 0;
  char endMarker = 32; // endmarker is spacebar
  static byte ndx = 0;
  static boolean recvInProgress = false;
  u8x8.setCursor(xdis, ydis); //put the cursor in the right position
  // save the current position and when text comes see if value is smaller then xdifference
 // xdisDiff = xEndDis-xdis+1;
  
  while (Serial.available() > 0 && newData == false) 
  // Read serial
  incomingByte = Serial1.read();

  // If the spacebar is read , print values and reset ndxit.
  if (incomingByte != endMarker) {
     // Print the elements.
     stringBuffer[ndx] = incomingByte;
     ndx++;
     if(ndx >= ArraySize){
      ndx = ArraySize -1;
     }
    }
  else{
    stringBuffer[ndx] = ' '; // terminate the string when spacebar is detected
    ndx = 0;
    newData = true;
  }

  if (newData = true)
  {
   u8x8.print(stringBuffer);
   xdis ++; //goes to the next spot  
   newData = false; 
  }
}

But after compiling I still got this error!

print_keys:33:27: error: no matching function for call to 'print(int [15])'

    u8x8.print(stringBuffer);

                           ^



                 from N:\Users\Stage\Leon\stage tijdelijke opdracht(en)\commap duo display\V2 display_arduinoNano\oled55 u8x8\oled55\oled55.ino:1:

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:67:12: note: candidate: size_t Print::print(char) <near match>

     size_t print(char);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:67:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'char' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^

                 from N:\Users\Stage\Leon\stage tijdelijke opdracht(en)\commap duo display\V2 display_arduinoNano\oled55 u8x8\oled55\oled55.ino:1:

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:68:12: note: candidate: size_t Print::print(unsigned char, int) <near match>

     size_t print(unsigned char, int = DEC);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:68:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'unsigned char' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:69:12: note: candidate: size_t Print::print(int, int) <near match>

     size_t print(int, int = DEC);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:69:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'int' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^

In file included from C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Stream.h:26:0,

                 from C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/HardwareSerial.h:24,

                 from C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Arduino.h:78,

                 from N:\Users\Stage\Leon\stage tijdelijke opdracht(en)\commap duo display\V2 display_arduinoNano\oled55 u8x8\oled55\oled55.ino:1:

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:70:12: note: candidate: size_t Print::print(unsigned int, int) <near match>

     size_t print(unsigned int, int = DEC);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:70:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'unsigned int' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^

                 from N:\Users\Stage\Leon\stage tijdelijke opdracht(en)\commap duo display\V2 display_arduinoNano\oled55 u8x8\oled55\oled55.ino:1:

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:71:12: note: candidate: size_t Print::print(long int, int) <near match>

     size_t print(long, int = DEC);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:71:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'long int' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^


                 from N:\Users\Stage\Leon\stage tijdelijke opdracht(en)\commap duo display\V2 display_arduinoNano\oled55 u8x8\oled55\oled55.ino:1:

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:72:12: note: candidate: size_t Print::print(long unsigned int, int) <near match>

     size_t print(unsigned long, int = DEC);

            ^~~~~

C:\Users\stage3\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.3\cores\arduino/Print.h:72:12: note:   conversion of argument 1 would be ill-formed:

print_keys:33:27: error: invalid conversion from 'int*' to 'long unsigned int' [-fpermissive]

    u8x8.print(stringBuffer);

                           ^

exit status 1
no matching function for call to 'print(int [15])'

From what I can see the function still thinks I want to convert something and the conversion is not allowed. But I don’t understand why it can’t since from what I think it should be able to allow it.
If anyone could help that could bring me in the right direction. Also if anyone has a suggestion of how I
should implement my function in a better/easier way ( how I want to print my tekst), then that is also very welcome if someone has tips.

kind regards,