0

So, I am trying to transmit float data, over UART, from my Arduino to another machine. The log of this data is really large, so I am forced to put it on an SD card, and read it into the Arduino line-by-line, then transmit that single line.

The issue is that my code seems to be "grabbing" ~65 lines (or more) at a time, transmitting them over UART, then stopping (or crashing) the Arduino.

My log looks like this (a small snippet of the several thousands of lines):

14.71927
14.71795
14.71787
14.71787
14.71787
14.71799
14.7175
14.71836
14.71746
14.71787
14.71923
14.71878
14.7184
14.71791
14.71893
14.7184
14.71746
14.71799
14.71746
14.71795
14.71874
14.71795
14.71878
14.71836
14.71881
14.72235
14.72781
14.72872
14.72917
14.72826
14.73188
14.73278
14.73595
14.73783
14.74227
14.74231
14.74363
14.74683
14.7495
14.75492
14.75899
14.76396
14.76807
14.77033
14.77161
14.77262
14.77978
14.78524
14.7884
14.78746
14.79695
14.80238
14.80652
14.80648
14.81055
14.81191
14.81865
14.82234
14.82095
14.81733
14.8286
14.82908
14.82999
14.82682
14.83673

And my code looks like this (I know it says 4Hz, but is at 1Hz, but that was to help "see" the "batch" grabbing and printing):

// Define our LED pins
#define greenLED    6
#define redLED      5

// Define our serial BAUD rate
#define BAUDrate    9600

// To read data from the SD card
#include <SD.h>

// To print data to an LCD panel
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd( 0x3F, 16, 2 );

void setup(){
    // Set LED pins as outputs
    pinMode( greenLED, OUTPUT );
    pinMode( redLED, OUTPUT );

    // LED output at start: GREEN = ON, RED = OFF
    digitalWrite( greenLED, HIGH );
    digitalWrite( redLED, LOW );

    // Begin wired comms with MSP board
    Serial.begin( BAUDrate );

    // Begin reading data from SD card
    SD.begin( 4 );                      // Need pin #4 for SD card bit-shifting
    File dataLog;                       // Store our data from the SD card
    dataLog = SD.open( "DATA.TXT", FILE_READ );
    String data = dataLog.readStringUntil( "\n" ); // Prime our data

    int i = 0;

    while( data != "" ){
        i++;
        Serial.print( i );
        Serial.print( "\t" );

        Serial.println( data );         // Send our data to MSP board
        lcdUpdate( data );              // Print our sent data to the LCD screen

        delay( 1000 );                   // Wait 1/4 of a second (4 Hz)

        data = dataLog.readStringUntil( "\n" );    // Reprime our data
    }

    // Close our data log to avoid corrupting SD card contents upon restart
    dataLog.close();

    // Switch our LEDs since we're finished streaming our data
    digitalWrite( greenLED, LOW );
    digitalWrite( redLED, HIGH );
}

void loop(){
}

// Prints the current data being sent over UART to an LCD screen
void lcdUpdate( String ourData ){
    lcd.setCursor( 0, 0 );
    lcd.print( " Sending data:  " );
    lcd.setCursor( 0, 1 );
    lcd.print( "    " );
    lcd.print( ourData );
    lcd.print( "    " );
}

Again, I want it to grab a single line, print it to the serial terminal, print it to the LCD, then grab a second line and repeat. I'm not able to test the LCD (since I don't have one with me at the moment), but the serial terminal is giving me problems, so I'm assuming the LCD won't work, either.

I'm getting something, as a Serial Terminal output, along the lines of:

1       14.71927
14.71795
14.71787
14.71787
14.71787
14.71799
14.7175
14.71836
14.71746
14.71787
14.71923
14.71878
14.7184
14.71791
14.71893
14.7184
14.71746
14.71799
14.71746
14.71795
14.71874
14.71795
14.71878
14.71836
14.71881
14.72235
14.72781
14.72872
14.72917
14.72826
14.73188
14.73278
14.73595
14.73783
14.74227
14.74231
14.74363
14.74683
14.7495
14.75492
14.75899
14.76396
14.76807
14.77033
14.77161
14.77262
14.77978
14.78524
14.7884
14.78746
14.79695
14.80238
14.80652
14.80648
14.81055
14.81191
14.81865
14.82234
14.82095
14.81733
14.8286
14.82908
14.82999
14.82682
14.83

It seems to want to stop somewhere between the 65th and 69th element, randomly. I'm assuming that it's running out of buffer space, since it's grabbing everything from the SD card, instead of a single line, one at a time.

This has become more evident with my i = 0 line, and that it isn't iterating for every line, just the once for the ~65 lines.

Am I reading the data in incorrectly, or am I parsing things incorrectly, or is what I'm trying to do simply not possible?

  • put a Serial.print("."); inside the loop() block – jsotola Sep 11 at 16:37
  • @jsotola It never even gets to the loop() block. I did what you said, and it just kind of stops, which reaffirms my suspicions that it's grabbing the whole SD card contents and overflowing the buffer, then freezing. – stillborn86 Sep 13 at 14:31
1

Arduino's reference doesn't help with your readStringUntil call (see SD's documentation), but assuming it's the same as Stream's, it needs a character, not a string as a terminator.

Your output shows that your Arduino tried to read a line, but there were no line endings found - probably because of the '' vs. "". That's why your output starts with a 1 (i's value), and there no other line numbers after that, even though in your output clearly shows newlines.

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.