Serial communication freezes my MKRZERO

Hi guys!

First of all, I’m a noobie so if I made horrible mistakes in my code or idea of functionment, please forgive me :slight_smile:

I already posted on another forum (“networking with windows”) but as I didn’t get any answer, I guess that it wasn’t the place to post my question.

I use a MKRZERO with a OLED screen to display informations on the case of my HTPC. I planned to send these infos from the PC running Kodi to the arduino to display everything.

I first used a Nano and it worked well with the serial monitor but it was power limited to use the OLED screen. I then changed to a MKRZERO but now, sending one string via the Serial port of the GUI causes the freeze of the board and GUI. I must say that is works well when sending nothing, I can see the dots of the hour display bliking as expected.

Here is the code:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI u8g2(U8G2_R2, 12, 7, 6);
int t;

String Ser;
String C;
String L;
char *T = "1234567890123456789012345678901234567890123456789012345678901234567890";
char *A = "12345678901234567890123456789012345678901234567890123456789012345678901";
char *H = "12  34";
char *R = "0:35:42/2:10:50";

void setup(void) {
  u8g2.begin();
  Serial.begin(115200); // opens serial port, sets data rate to 9600 bps-
  t = 0;
  T = "1234567890123456789012345678901234567890123456789012345678901234567890";
  A = "12345678901234567890123456789012345678901234567890123456789012345678901";
  H = "12  34";
  R = "0:35:42/2:10:50";
}


void loop(void) {

  u8g2.firstPage();
  do {
    //informations
    u8g2.setFontPosBottom();
    u8g2.setFont(u8g2_font_helvR14_tf);
    u8g2.drawUTF8(0, 30, T); //titre
    u8g2.setFont(u8g2_font_helvB10_tf);
    u8g2.drawUTF8(0, 50, A); //auteur

    //heure
    u8g2.setFont(u8g2_font_helvB08_tn);
    u8g2.setFontPosTop();
    u8g2.drawUTF8(226, 0, H);
    if (t < 3) {
      u8g2.drawUTF8(234, 0, "     ");
    } else {
      u8g2.drawUTF8(234, 0, "  :  ");
    }

    //barre avancement
    u8g2.drawBox(0, 63, 170, 1);
    u8g2.drawBox(0, 60, 100, 3);
    //texte avancement
    u8g2.setFont(u8g2_font_5x8_tn);
    u8g2.setFontPosBottom();
    u8g2.drawUTF8(180, 64, R);

  } while ( u8g2.nextPage() );
  //Clignotement ":"
  t += 1;
  if (t > 5) {
    t = 0;
  }
  while (Serial.available()) {
    Ser = Serial.readString(); // read the incoming data as string
    L = Ser.substring(1);
    C = Ser.substring(0, 1);
    if (C == "T") {
      strcpy (T, L.c_str());
    } else if (C == "A") {
      strcpy (A, L.c_str());
    } else if (C == "H") {
      strcpy (H, L.c_str());
    } else if (C == "R") {
      strcpy (R, L.c_str());
    }
  }
  delay(50);
}

Any idea?
Thanks in advance,
Nicolas

I have no experience of the MKRZERO and based on what you said I suspect your nano program was very different from the MKRZero program.

Have you tried the unchanged nano program on the Mkrzero? If that works then you could be sure the problem is in the extra code.

...R

In fact, this is the exact code I used on the nano and that I uploaded on the mkrzero so...

Lyechee:
In fact, this is the exact code I used on the nano and that I uploaded on the mkrzero so...

I guess I misinterpreted "I first used a Nano and it worked well with the serial monitor but it was power limited to use the OLED screen."

...R

Hi!
I fact it's not the serial communication that freezes the MKRZERO, it works well when using only the serial.readstring() part.

The line messing up is the strcpy (T, L.c_str());

L being the string I want to display and T the const char* I need to use the u8g2 drawUTF8 function.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Hello!
Thank you a lot for your answer!
As I'm kinda lost in your tuto, I took a look at your demo code here : Python TO Serial TO Arduino. Send a string? - Interfacing w/ Software on the Computer - Arduino Forum

If course it needs to be adapted but I think it should do the job!

I still have a question: do I really need the arduino to answer to python? I mean I only need the board to display the serial incoming variables, there won't be any servos or valuable informations coming from the arduino.

Nicolas

Lyechee:
I took a look at your demo code here : Python TO Serial TO Arduino. Send a string? - Interfacing w/ Software on the Computer - Arduino Forum

I wonder if you meant this Python - Arduino demo

I still have a question: do I really need the arduino to answer to python? I mean I only need the board to display the serial incoming variables, there won't be any servos or valuable informations coming from the arduino

It is not necessary for the Arduino to send data to the PC if you have no use for that data. However it can be helpful to let the PC program know that the Arduino is ready at startup and to let it know that data has been received or that the Arduino is ready for another message.

...R