Serial write fails on the first write

Thank you for reading this. I’ve spent most of the day trying to discover why the serial write function will not send the correct data the first time. If I send the same block of data twice then the correct data is received by the Arduino. If I send the same block once more, without resetting the Arduino, then all is well. The serial write seem to only fail if I write the block once after an Arduino reset.

If I send the same data from the Arduino serial monitor then there isn’t a problem. So, the fault is something that I’m missing from my Processing code, but What?

After a bit more experimentation I’ve discovered that if I send two line feeds before the data block then all is well. Does this mean that I need some sort of marker at the beginning of the block or do I need the reset something in the serial device before sending the data?

import processing.serial.*;

Serial myPort;

boolean call_once = true;

void setup()
{
myPort = new Serial(this, “/dev/ttyUSB0”, 9600);
delay(1000); // wait for Arduino to reset
}

void draw()
{
if (call_once)
{
write_data();
call_once = false;
}
}

void write_data()
{
myPort.write(‘1’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(‘0’);
delay(500);
myPort.write(’\n’);

println(“data written”);
}

Hello,

Please format your code as per “Is your post formatted?”:
https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147/4

Otherwise we can’t easily cut and past to test code; quotes are always a problem.

The Processing code posted is fine.
I can receive the data on a terminal (puTTY) and other Processing application.

Please post your formatted Arduino code (minimal) and I will take a look.

:slight_smile:

That’s very kind of you. I’ll post the lot, it’s fairly short.

Edit. I’m not sure that I got the “code goes here working”. I highlighted the code and pressed “</>” and the 3 ``` were displayed.

#include "LedControl.h"
LedControl lc = LedControl (12, 11, 10, 1);

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

int dataNumber = 0;

int grid[] =
{
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
};

void display_grid()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow (0, i, grid[i]);
  }
}
void setup()
{
  Serial.begin (9600);
  // the MAX72XX is in power-saving mode on startup,
  // we have to do a wakeup call
  lc.shutdown (0, false);

  // set the brightness to a medium values
  lc.setIntensity (0, 0);                      // 0 = low; 8 = high

  // show grid - to demonstrate that the 8x8 array works
  display_grid();
  //delay(1000);

  // clear the display
  lc.clearDisplay (0);
}

void loop()
{
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker()
{
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false)
  {
    rc = Serial.read();

    if (rc != endMarker)
    {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars)
      {
        ndx = numChars - 1;
      }
    }
    else
    {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData()
{
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;

    for (int i = 0; i < 8; i++)
    {
      //grid[i] = atoi(receivedChars[i]);
      grid[i] = receivedChars[i] - '0';
      Serial.println(grid[i]);
    }
    display_grid();
  }
}

just ideas:

-a- change from sending chars to sending lines…

-b- already send lots of info back, show it at processing console

-c- handshake: arduino sends a line “READY”
and processing only starts sending after got that.
( so it also could help as a kind of see “Arduino REBOOT” logic )

Thank you kll and glv for your time.

It finally dawned on me that Processing is sending data to the Arduino before it’s ready. I simply increased the delay from 1 second to 1.5 seconds after myPort is created. I suppose a more technically correct method would be to have the Arduino signal when it’s ready. I also discovered that the inter-character delay is not necessary.

2 Likes