Store serial.write result into a float that can be called later

Currently, I want to create car automation using fuzzy logic. This car will follow the path it has been traveled before using GPS latitude and longitude that saved into an sd card. so when the remote activates the feature the GPS will stop storing data in SD CARD, and the data that has been saved before should be sent back on the Arduino as a waypoint float.

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

float waypoint;
File myFile;

void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }



  Serial.print("Initializing SD card...");


  if (!SD.begin(4)) {

    Serial.println("initialization failed!");

    while (1);

  }

  Serial.println("initialization done.");


  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  // re-open the file for reading:

  myFile = SD.open("GPS_data.txt");

  if (myFile) {

    Serial.println("GPS_data.txt:");


    // read from the file until there's nothing else in it:

    while (myFile.available()) {

     waypoint = Serial.write(myFile.read());
     

    }

    // close the file:

    myFile.close();

  } else {

    // if the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

}


void loop() {

  // nothing happens after setup

}

MODERATOR EDIT
Fixed the code tags for you

Where are you stuck ?

What do you try to achieve in this line???
If you want to read something from file and store in variable - why a Serial.write() here ?

Next, the read() method reads only one byte, not a float. Calling this method repeatedly, you overwrite the waypoint variable with single bytes rather than reading a float.

1 Like

i want to store the data from sd card into a float

thx for the answer, but if I delete the serial.write how do I test the data stored in the waypoint?

waypoint = myFile.read();

Something like this, may be?

waypoint = myFile.read();
Serial.print(waypoint);

But as I said before, you can't read float this way

1 Like

is it possible if I stores the form as it is but change it later? I have tried that way before and it results in only a float number

I don't understand, sorry

1 Like

thankyou for helping me, i can do it now :laughing:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.