Announcement

Collapse
No announcement yet.

MicroSD Card Write

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Thanks! That works. I ran into some trouble at the beginning with it still saving only one time. I discovered that you must have a file_Close at the end of it for it to save multiple times. Now that I have it saving in the text file the text file looks like this:

    DataStringDataStringDataString........

    I would like to have it save to the text file like this:

    DataString
    DataString
    DataString


    This formatting would be better for analyzing the data in Excel or Matlab.
    Also if I was wanting to have three entries like this:

    DataString1 DataString2 DataString3


    How could I do that? Is there a way to tab over in between each file_Write? Thanks for all the help!

    Comment


    • #17
      Hi Chessmaster,

      Have you tried something like this one?

      Code:
      fileHandler := file_Open(ptr, 'a');     // append
      
      file_PutS(buffer, fileHandler);   // contains the DataString
      
      file_PutS("\n", fileHandler);       // appends the \n character to the last position of handle , try using file_PutC() also
      
      file_Close(fileHandler);
      you can look for the ASCII equivalent for special characters like tab and include it in filePutC() or filePutS().

      Regards,

      Comment


      • #18
        Hey Joseph,

        Thanks for the advice! That did work. I was able to find what I needed. I am starting to get the hang of programing the 4Duino and working 4d Workshop. I am getting closer to getting my gyroscope data saved to the microSD card.

        I had one problem today and that was when I was trying to write save to the microSD card. I am using the file_Write command. Here is my code where I am having the problem:

        Code:
        //Create variables to hold the output rates.
        int xRate, yRate, zRate;
        //Read the x,y and z output rates from the gyroscope.
        xRate = readX();
        yRate = readY();
        zRate = readZ();
        //Print the output rates to the terminal and save to microSD.
        Serial.print(xRate);
        Serial.print('\t');
        Display.file_Write(5,xRate,handle1); //xRate is where the gyroscope readings are for the x-axis - LINE THAT IS GIVING ERROR
        Display.file_PutC('\t',handle1);
        Serial.print(yRate);
        Serial.print('\t');
        Display.file_Write(5,yRate,handle1); //LINE THAT IS GIVING ERROR
        Display.file_PutC('\t',handle1);
        Serial.print(zRate);
        Serial.print('\n');
        Display.file_Write(5,zRate,handle1); //LINE THAT IS GIVING ERROR
        Display.file_Write(2,"\n",handle1);
        Display.file_Close(handle1);
        //Wait 10ms before reading the values again
        delay(10);


        The error that I get is: invalid conversion from 'int' to 'char*'. How should I go about solving this so that way I can save the gyroscope data?


        Thanks for all your help!
        Last edited by Chessmaster; 26th January 2017, 11:58 AM.

        Comment


        • #19
          Not a simple thing in C, apparently: http://www.sanfoundry.com/c-program-...ng-vice-versa/

          Too late and I should have been in bed hours ago... try it and let us know.

          Comment


          • #20
            PatrickC

            I have not tried that, but I did find another way, I think. I know that I have seen codes for a normal arduino that saves temperature measurements to an SD card. The way they went around it was they used DataString += String(measurement). I tried that and it seems to work put now I cannot use Display.file_Write anymore.

            Here is my code:
            word handle1;
            handle1 = Display.file_Open("AnkAng.txt",'a');
            //The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
            //Create variables to hold the output rates.
            int xRate, yRate, zRate;
            //Make a string for assembling the data to log:
            String DSX = "";
            //Read the x,y and z output rates from the gyroscope.
            xRate = readX();
            DSX += String(xRate);
            yRate = readY();
            zRate = readZ();
            //Print the output rates to the terminal, seperated by a TAB character.
            Serial.print(xRate);
            Serial.print('\t');
            Display.file_PutS(DSX,handle1); //LINE THAT ERROR OCCURS

            I get the following error:
            no matching function for call to 'Picaso_Serial_4dlib::file_Write(int, String&, word&)'.

            Anyone have any ideas of how to write a string? Thanks!

            Comment


            • #21
              Hi Chessmaster,

              One way is you could put your desired values into a character array, then you can use file_PutS(charArray, handle1);
              Code:
              word handle1;
              char charArray[10] ;
              xRate = 16;
              
              // put some function here that puts the integers value into a character array.  like in arduino forum below
              
              charArray[10] = {'1','6',0,0};
              
              handle1 = Display.file_Open("write.txt",'a');
              
              Display.file_PutS(charArray,handle1); 
              
              Display.file_Close(handle1);
              arduino forum: Converting int into char array
              https://forum.arduino.cc/index.php?topic=126618.0

              Regards,

              Comment


              • #22
                Originally posted by Joseph Herrera View Post
                Hi Chessmaster,

                I have attached a simple 4Duino Project that writes "Hello World" on the SD Card. Then reads the text written on the sd card to be displayed on the LCD.

                Attached, test-sd.zip

                Here is the App Note related to the sample project:

                Serial Arduino uSD Card Access FAT16
                http://www.4dsystems.com.au/appnote/4D-AN-00113/

                For the 4Duino and Arduino, you could also try something like these:
                //------------------------
                handle = Display.file_Open("SAMPLE.txt",'w');
                Display.putstr("Writing to uSD... \n");
                Display.putstr("HELLO WORLD to SAMPLE.txt \n");
                delay(2000);
                Display.file_Write(11,"HELLO WORLD",handle);
                Display.file_Close(handle);
                //-------------------------

                Hope this helps and Regards,
                Hi, Joseph
                and what about binary data instead of text? I want to transfer a file from PC to SD card, I succesfuly made with another boards (using SD library). I can transfer without problems a text file or even a binary file (for instance a 3Mb JPG file).
                This is the code (when running the sketch, I send the file through a Seria Terminal, for instance TERATERM, and it copies to SD):

                Code:
                /******Dump_Binary_SD.ino*****************/
                #include "SD.h"
                #include "SPI.h"
                
                File myFile;
                unsigned long last = 0;
                long count = 0;
                bool read = 0;
                
                void setup() {
                Serial.begin(9600);
                if (!SD.begin(8)) //Init SD
                {
                Serial.println("Init error SD!");
                return;
                }
                }
                
                void loop() {
                if (!read) {
                myFile = SD.open("42.jpg", O_CREAT | O_WRITE);
                while (millis() - last < 1000 || count== 0) // Delay time, try to change if doesn't work
                {
                while (Serial.available() > 0)
                {
                myFile.write(Serial.read());
                last = millis();
                count++;
                }
                }
                myFile.close();
                read = 1;
                Serial.print ("Copy OK");
                }
                }
                Now, I want to do the same with 4Duino, but SD.h library doesn't work as I cannot init with SD.begin(chipSelected). I don't know how to adapt the function "myFile.write(Serial.read());" to Picaso function "Display.file_Write(sizeof(data),data,handle);" where 'data' must be a char* in Picaso library.
                This is the code:
                Code:
                /****************************/
                unsigned long last = 0;
                long count = 0;
                bool read = 0;
                
                void loop(){
                
                if (!read) {
                word handle;
                handle = Display.file_Open("42.jpg",'w'); //myFile = SD.open("42.jpg", O_CREAT | O_WRITE);
                while (millis() - last < 1000 || count== 0)
                {
                while(Serial.available() > 0)
                {
                Display.file_Write(1, Serial.read(),handle); // myFile.write(Serial.read());
                last = millis();
                count++;
                }
                }
                Display.file_Close(handle); // myFile.close();
                read = 1;
                Display.putstr("Made!");
                }
                
                }
                
                /*************/
                Any idea?

                Comment


                • #23
                  Hello

                  It is possible to do this with Display.file_Write but if you are more familiar with Arduin SD commands we have an experimental library that allows you to access the SD card with the SD commands you are familiar with.

                  eg.

                  #include "SD4dPicaso.h";

                  File Testfile;

                  SD.begin(); // leave the parenthesis empty
                  Testfile = SD.open("4dtest.txt", FILE_WRITE);

                  while (Serial.available() > 0)
                  {
                  Testfile.write(Serial.read());
                  last = millis();
                  count++;
                  }

                  It is still a work in progress, but you are welcome to give it a try.

                  Best regards

                  Paul
                  Attached Files

                  Comment


                  • #24
                    Hi, Pauleilio. I´ll check the solution with SD4dPicaso, but when you say it is possible with Display.file_Write, can you tell me how?Thanks a lot.

                    Comment


                    • #25
                      Hi

                      The way you would use Display.file_Write is to read an amount of bytes into a buffer and then send that buffer with Display.file_Write which is more efficient than single bytes

                      You could use this command which would work for sending a single byte

                      file_PutC

                      and to read

                      file_GetC

                      I hope this helps

                      Best regards

                      Paul

                      Comment


                      • #26
                        I have tested SD4dPicaso library but it doesn't work. It creates the file but sometimes is empty, or with strange symbols or even with pieces of other files included into SD card.
                        So I decided to go to basics and checked this code:
                        Code:
                        if (!SD.begin())  
                          {
                            Serial.println("Error SD!");
                            return;
                          }
                          else Serial.println("OK SD!");
                        
                        
                        
                         ...  
                        
                          myFile = SD.open("test4.txt", FILE_WRITE);
                          if (myFile) {
                            Serial.print("Writing to test.txt...");
                            myFile.println("testing 1, 2, 3.");
                            myFile.close();
                            Serial.println("done.");
                          } else {
                            Serial.println("error opening test.txt");
                          }
                        It happens the same: (0 bytes file, or strange symbols, or corrupted file). The date (in properties) is strange as well (not the current date).

                        Comment


                        • #27
                          Hello,

                          It worked well in testing on my 4Duino but it is experimental and was a quick conversion from Diablo to Picaso. I will test your code with my 4Duino and let you know the result.

                          Best regards

                          Paul

                          Comment


                          • #28
                            Hi,

                            I just tested your code and it worked fine with the SD4dPicaso library, The date and time won't be correct as the 4Duino doesn't know that. I have attached a screenshot

                            Click image for larger version

Name:	screenSD4d.png
Views:	19
Size:	60.7 KB
ID:	63187
                            It would be interesting to know why it failed on your 4Duino. Would you mind sending me your project that you used to test the library so I can find the reason why it failed.

                            Best regards

                            Paul

                            Comment


                            • #29
                              This is the whole code (I'm not at home now, so I'll cannot test it until tomorrow):

                              Code:
                              %%Display%%.DefineResetLine ;      // *Replaced* at compile time with define for reset line connected to the display
                              %%Display%%.DefineDisplaySerialx ; // *Replaced* at compile time with define the Serial Port connected to the display
                              
                              #include "TestSDConst.h"
                              #include "SD4dPicaso.h"
                              
                              %%Display%%.IncludeSerial_4DLib ;          // *Replaced* at compile time with an Include the Serial Library relevant to the display
                              %%Display%%.IncludeSerial_Const4D ;        // *Replaced* at compile time with an Include the Constants file relevant to the display
                              %%Display%%.AssignDisplaySerialtoLibrary ; // *Replaced* at compile time with an Assign of the correct Serial port to the correct library
                              
                              word hndl ;
                              unsigned long last = 0;
                              long count = 0;
                              bool read = 0;
                              
                              File myFile;
                              
                              
                              void setup()
                              {
                              
                                Serial.begin(9600) ;      
                              // Note! The next statement will stop the sketch from running until the serial monitor is started
                              //       If it is not present the monitor will be missing the initial writes
                              //    while (!Serial) ;             // wait for serial to be established
                               if (!SD.begin())  // Init SD
                                {
                                  Serial.println("Error SD!");
                                  return;
                                }
                                else Serial.println("OK SD!");
                              
                               pinMode(RESETLINE, OUTPUT);       // Display reset pin
                              %%Display%%.Toggle_Reset_On ;       // *Replaced* at compile time with correct rest on logic for the attached display
                                delay(100);                       // wait for it to be recognised
                              %%Display%%.Toggle_Reset_Off ;      // *Replaced* at compile time with correct rest off logic for the attached display
                                delay(3000) ;                     // give display time to startup
                              
                                // now start display as Serial lines should have 'stabilised'
                                %%Display%%.DisplaySerial.Begin_Speed ; // *Replaced* at compile time with command to start the serial port at the correct speed
                                Display.TimeLimit4D = 5000 ;      // 5 second timeout on all commands
                              
                                Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
                                Display.putstr("Mounting...\n");
                                if (!(Display.file_Mount()))
                                {
                                  while(!(Display.file_Mount()))
                                  {
                                    Display.putstr("Drive not mounted...");
                                    delay(200);
                                    Display.gfx_Cls();
                                    delay(200);
                                  }
                                }
                                hndl = Display.file_LoadImageControl("TESTSD~1.dat", "TESTSD~1.gci", 1);
                              
                                Display.gfx_Cls();            //clear the screen    */
                              
                                myFile = SD.open("test4.txt", FILE_WRITE);
                                if (myFile) {
                                  Serial.print("Writing to test.txt...");
                                  myFile.println("testing 1, 2, 3.");
                                  myFile.close();
                                  Serial.println("done.");
                                } else {
                                  Serial.println("error opening test.txt");
                                }
                              }
                              
                              void loop()
                              {
                              
                              }

                              Comment


                              • #30
                                Hi,

                                if you move,

                                if (!SD.begin())
                                {
                                Serial.println("Error SD!");
                                return;
                                }
                                else Serial.println("OK SD!");


                                to below this line,


                                hndl = Display.file_LoadImageControl("TESTSD~1.dat", "TESTSD~1.gci", 1);


                                It should work ok for you.
                                Code:
                                %%Display%%.DefineResetLine ;      // *Replaced* at compile time with define for reset line connected to the display
                                %%Display%%.DefineDisplaySerialx ; // *Replaced* at compile time with define the Serial Port connected to the display
                                
                                #include "TestSDConst.h"
                                #include "SD4dPicaso.h"
                                
                                %%Display%%.IncludeSerial_4DLib ;          // *Replaced* at compile time with an Include the Serial Library relevant to the display
                                %%Display%%.IncludeSerial_Const4D ;        // *Replaced* at compile time with an Include the Constants file relevant to the display
                                %%Display%%.AssignDisplaySerialtoLibrary ; // *Replaced* at compile time with an Assign of the correct Serial port to the correct library
                                
                                word hndl ;
                                unsigned long last = 0;
                                long count = 0;
                                bool read = 0;
                                
                                File myFile;
                                
                                
                                void setup()
                                {
                                
                                  Serial.begin(9600) ;      
                                // Note! The next statement will stop the sketch from running until the serial monitor is started
                                //       If it is not present the monitor will be missing the initial writes
                                //    while (!Serial) ;             // wait for serial to be established
                                 
                                 pinMode(RESETLINE, OUTPUT);       // Display reset pin
                                %%Display%%.Toggle_Reset_On ;       // *Replaced* at compile time with correct rest on logic for the attached display
                                  delay(100);                       // wait for it to be recognised
                                %%Display%%.Toggle_Reset_Off ;      // *Replaced* at compile time with correct rest off logic for the attached display
                                  delay(3000) ;                     // give display time to startup
                                
                                  // now start display as Serial lines should have 'stabilised'
                                  %%Display%%.DisplaySerial.Begin_Speed ; // *Replaced* at compile time with command to start the serial port at the correct speed
                                  Display.TimeLimit4D = 5000 ;      // 5 second timeout on all commands
                                
                                  Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
                                  Display.putstr("Mounting...\n");
                                  if (!(Display.file_Mount()))
                                  {
                                    while(!(Display.file_Mount()))
                                    {
                                      Display.putstr("Drive not mounted...");
                                      delay(200);
                                      Display.gfx_Cls();
                                      delay(200);
                                    }
                                  }
                                  hndl = Display.file_LoadImageControl("TESTSD~1.dat", "TESTSD~1.gci", 1);
                                
                                if (!SD.begin())  // Init SD
                                  {
                                    Serial.println("Error SD!");
                                    return;
                                  }
                                  else Serial.println("OK SD!");
                                
                                
                                  Display.gfx_Cls();            //clear the screen    */
                                
                                  myFile = SD.open("test4.txt", FILE_WRITE);
                                  if (myFile) {
                                    Serial.print("Writing to test.txt...");
                                    myFile.println("testing 1, 2, 3.");
                                    myFile.close();
                                    Serial.println("done.");
                                  } else {
                                    Serial.println("error opening test.txt");
                                  }
                                }
                                
                                void loop()
                                {
                                
                                }
                                Paul

                                Comment

                                Working...
                                X