1 2 3 Previous Next 31 Replies Latest reply on Jun 19, 2014 6:26 PM by luismaya

    how to read data from a txt file from sd

    luismaya

      Hello I am using a 32gb sd to work with LINUX_IMAGE_FOR_SD_Intel_Galileo_v1.0.0 image within this memory I created a file are listed called "servo.txt" within this number 90,150,200,215,320,420,520 have several saved. What I intend to do reading these numbers for use within my program that will serve to position a servo motor. This file called "servo.txt" if I read it, the function read (); I put my code.

       

      #include <SD.h>

      #include <Wire.h>

      #include <Adafruit_PWMServoDriver.h>

      //#include <LiquidCrystal.h>

      #include <stdlib.h>

      //LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

       

         // called this way, it uses the default address 0x40

         Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

         // you can also call it with a different address you want

         //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

       

        // Depending on your servo make, the pulse width min and max may vary, you

        // want these to be as small/large as possible without hitting the hard stop

        // for max range. You'll have to tweak them as necessary to match the servos you

        // have!

      #define SERVOMIN  90 // this is the 'minimum' pulse length count (out of 4096)

      #define SERVOMAX  520 // this is the 'maximum' pulse length count (out of 4096)

      char valor;

      const int chipSelect = 4;

      int switchInputPin5 = 2;

      int led=3;

      double dato;

      int r=0;

      void leer();

      void setup()

      {

        

        while (!Serial)

          {

            ; // wait for serial port to connect. Needed for Leonardo only

          }

          delay(500);

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

          pinMode(10, OUTPUT);

      ////////////////////////////////////////////////////////////////////////////////////////

        if (!SD.begin(chipSelect))

        {

          Serial.println("Card failed, or not present");

          // don't do anything more:

          return;

        }

        Serial.println("card initialized.");

        delay(500);

       

          pwm.begin();

          Serial.begin(9600);

          pwm.setPWMFreq(45);  // Analog servos run at ~50 Hz updates

          pinMode(switchInputPin5, INPUT);

          pinMode(led,OUTPUT);

         

      }

       

      void loop()

      {

       

           int switchState5 = digitalRead(switchInputPin5);

       

        if (switchState5 == HIGH)

        {

          leer();

          delay(200);

        }

        pwm.setPWM(0, 0, 150);

      }

       

      void leer()

      {

       

        File dataFile = SD.open("servo.txt",FILE_READ);

        if (dataFile)

        {

          while (dataFile.available())

        {

         

           valor=dataFile.read();

            ///"valor" is where I want to save the settings that I have the "servo.txt" file

           pwm.setPWM(0, 0, valor);

          Serial.println(valor);

        

        }

       

         dataFile.close();

        } 

       

      }

        • 1. Re: how to read data from a txt file from sd
          Intel_Jesus

          Hi luismaya,

           

          What message are you getting in the serial monitor? Are you successfully reading from file? You can also use C to code on Galileo using a sketch via the Arduino IDE. It is a lot easier to read and write to a file instead of using the SD.h library. Here is a sample code to include in your sketch.

           

          datafile= fopen( "/tmp/datafile" , "r");
            if (datafile) {
               while (fgets(output, 100, datafile) != NULL)
                   ;
            fclose(datafile);
            }
            data= atoi(output);
          

           

          Also, don't forget to declare your variable at the beginning of the sketch. since you are using integers, this code should get you started.

          char output[100];
          FILE * datafile;
          int data;
          

           

          If you are more comfortable using the SD library I can also help you with that, i just need to know what messages you are getting in the Serial Monitor.

           

          Regards,

          Intel_Jesus

          • 2. Re: how to read data from a txt file from sd
            luismaya

            Are you successfully reading from file? If the file "servo.txt"

            You can also use C to code on Galileo using a sketch via the Arduino IDE. I do not understand how to do this you mention.

            Not only do I want to read the file from the sd. The  Serial Monitor, it only took me to see that my code delivery.

            I tried to run your code, but what I do not understand is how I assign the name of the file you want to read. I've removed everything from the library SD.H
            I put the code:

            #include <Wire.h>

            #include <Adafruit_PWMServoDriver.h>

            #include <stdlib.h>

             

             

               // called this way, it uses the default address 0x40

               Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

               // you can also call it with a different address you want

               //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

             

              // Depending on your servo make, the pulse width min and max may vary, you 

              // want these to be as small/large as possible without hitting the hard stop

              // for max range. You'll have to tweak them as necessary to match the servos you

              // have!

            #define SERVOMIN  90 // this is the 'minimum' pulse length count (out of 4096)

            #define SERVOMAX  520 // this is the 'maximum' pulse length count (out of 4096)

                char output[100];  

                FILE * datafile;  

                int data;

                int switchInputPin5 = 2;

             

            void leer();

             

            void setup()

            {

               

             

            ////////////////////////////////////////////////////////////////////////////////////////

             

               pwm.begin();

               pwm.setPWMFreq(45);  // Analog servos run at ~50 Hz updates

               pinMode(switchInputPin5, INPUT);

                

            }

             

            void loop()

            {

             

              int switchState5 = digitalRead(switchInputPin5); 

              if (switchState5 == HIGH) 

               { 

                leer();

                delay(200);

               } 

             

            }

             

            void leer()

            {

                   datafile= fopen( "/tmp/datafile" , "r");  

                  if (datafile) 

                  {  

                     while (fgets(output, 100, datafile) != NULL);  

                       fclose(datafile);  

                  }  

                  data= atoi(output);

                pwm.setPWM(0, 0, 190);

            }


            • 3. Re: Re: how to read data from a txt file from sd
              Intel_Jesus

              Hi luismaya,

               

              I'm not sure I am  understanding what your question is. Do you want to write to the file as well?


              This line is where you declare the file name you want to give it and what file you are reading. "/tmp/datafile" is the path to the file.

              datafile= fopen( "/tmp/datafile" , "r"); 

               

              Regards,

              Intel_Jesus

              • 4. Re: how to read data from a txt file from sd
                luismaya

                Hello Intel_Jesus

                I just want to read the file is on the SD, because the file "servo.txt" the charge previously.


                datafile= fopen( "/tmp/servo.txt" , "r"); this would be the way to tell her that I want to read file

                • 5. Re: how to read data from a txt file from sd
                  JPMontero_Intel

                  Let me see if I’m getting you question correctly, you want to read values from an SD card and then use the values to control your servo, right? Maybe you can do a little sketch and just test reading the files from the SD print them to know what is actually reading and then try to read only the numbers not the entire file, because I think in the code you posted you are not separating the different values.

                  Regards
                  JPMontero_Intel

                  • 6. Re: how to read data from a txt file from sd
                    luismaya

                    If correct, read from sd and use it to servo angles

                    Correct the file that I have stored in memory "servo.txt" 90,150,200,215,320,420,520 with these values, and I want to read only the first 90 and then to 150 ..... , as an integer.

                    with this function if I can read the memory as it is what I have saved.

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    File dataFile = SD.open("servo.txt",FILE_READ);

                      if (dataFile)

                      {

                        while (dataFile.available())

                      {

                         valor=dataFile.read();

                          ///"valor" is where I want to save the settings that I have the "servo.txt" file

                         pwm.setPWM(0, 0, valor);

                        Serial.write(valor); I can see well in the serial terminal, but are saved in the valiable "valor" as ASCII code

                        }

                        dataFile.close();

                      } 

                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    So I wanted to try this example Intel_Jesus.

                    datafile= fopen( "servo.txt" , "r");  

                          if (datafile) 

                          {  

                             while (fgets(output, 100, datafile) != NULL);  

                               fclose(datafile);  

                          }  

                          data= atoi(output);

                    ////////////////////////////////////////////////////////////////////////////////////////////////

                    but with this example, I could not read the "servo.txt" file

                    • 7. Re: how to read data from a txt file from sd
                      luismaya

                      file "servo.txt" is on the sd with the image "LINUX_IMAGE_FOR_SD_Intel_Galileo_v1.0.0", and is in the sd slot Intel Galileo

                      • 8. Re: how to read data from a txt file from sd
                        luismaya

                        This can be seen in the serial terminal.

                        this is the code:

                         

                        #include <Wire.h>

                        #include <Adafruit_PWMServoDriver.h>

                        #include <stdlib.h>

                        #include <SD.h>

                         

                           // called this way, it uses the default address 0x40

                           Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

                           // you can also call it with a different address you want

                           //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

                         

                          // Depending on your servo make, the pulse width min and max may vary, you 

                          // want these to be as small/large as possible without hitting the hard stop

                          // for max range. You'll have to tweak them as necessary to match the servos you

                          // have!

                        #define SERVOMIN  90 // this is the 'minimum' pulse length count (out of 4096)

                        #define SERVOMAX  520 // this is the 'maximum' pulse length count (out of 4096)

                            char output[100];  

                           FILE *datafile;  

                            int data=98;

                            int switchInputPin5 = 2;

                            int valor=0;

                        void leer();

                         

                        void setup()

                        {

                        ////////////////////////////////////////////////////////////////////////////////////////

                           Serial.begin(9600);

                           pwm.begin();

                           pwm.setPWMFreq(45);  // Analog servos run at ~50 Hz updates

                           pinMode(switchInputPin5, INPUT);

                            

                        }

                        void loop()

                        {

                          int switchState5 = digitalRead(switchInputPin5); 

                          if (switchState5 == HIGH) 

                           { 

                             leer();

                             delay(200);

                           } 

                         

                        }

                        void leer()

                        {

                          File dataFile = SD.open("servo.txt",FILE_READ);

                          if (dataFile)

                          {

                            while (dataFile.available()) 

                          { 

                             valor=dataFile.read();

                        pwm.setPWM(valor, 0, 150);

                             Serial.write(valor);

                          }

                           dataFile.close();

                          }  

                        }

                         

                        Untitled-1.jpg

                        • 9. Re: how to read data from a txt file from sd
                          JPMontero_Intel

                          Hi

                           

                          For Jesus’s example in the first line you posted “datafile= fopen( "servo.txt" , "r");” you need to change that for the direction you have file in. For your case would be “datafile= fopen( "/media/realroot/servo.txt" , "r");”. Then in the image you provided you can see in value is written the entire content of the file, you need to separate the numbers from that variable, in order to use them as you want. For example now when you print value it displays 90,150,200,215,320,420,520 , but you need to print a value at a time. You can write a little function that separates the values everytime you get a comma for example, and that value you can put it your pwm.setPWM(valor, 0, 150); Also if you only need these values always I would recommend you to use variables set by you in your code instead of reading them from an file in your SD. I hope this help you

                           

                          Regards
                          JPMontero_Intel

                          • 10. Re: how to read data from a txt file from sd
                            luismaya

                            HI

                            with the path you tell me, failed to read the file is on the SD

                            SD.jpg

                            that's my memory and it's what's inside

                            SD2.jpg


                            • 11. Re: how to read data from a txt file from sd
                              JPMontero_Intel

                              Then you can try with the path datafile= fopen( "/media/mmcblk0p1/servo.txt" , "r"); . Let me know if that one works.

                              Regards

                              JPMontero_Intel

                              • 12. Re: how to read data from a txt file from sd
                                luismaya

                                Hi

                                does not read the file with this new direction
                                To find out what is working, I put you to type in the serial port, but not see anything.

                                datafile= fopen( "/media/mmcblk0p1/servo.txt" , "r");

                                      if (datafile) 

                                      {  

                                         while (fgets(output, 100, datafile) != NULL);  

                                           fclose(datafile);  

                                      }  

                                      data= atoi(output);

                                Serial.write(data);

                                 

                                Regards

                                • 13. Re: how to read data from a txt file from sd
                                  luismaya

                                  as you know which direction to access the file. Where I can find it?

                                  • 14. Re: Re: how to read data from a txt file from sd
                                    Intel_Jesus

                                    Hi luismaya,

                                     

                                    Sorry for the delay. Here is a short code i wrote and it is reading the file which is at the root of the SD card. Same as yours.

                                     

                                    char output[100];
                                    FILE * file;
                                    int datat;
                                    
                                    
                                    void setup() {
                                      // put your setup code here, to run once:
                                      delay(1000);
                                      Serial.begin(9600);
                                      file= fopen("/media/realroot/servo.txt" , "r"); 
                                      if (file) { 
                                         while (fgets(output, 100, file) != NULL) 
                                             ; 
                                      fclose(file); 
                                      } 
                                      else{
                                        Serial.println("File Not Found!");
                                      }
                                      Serial.println(output);
                                    }
                                    
                                    void loop() {
                                      // put your main code here, to run repeatedly:
                                    }
                                    
                                    

                                     

                                    The output in the Serial Monitor is:

                                    90,150,200,215,320,420,520
                                    
                                    

                                     

                                    In your code i see you do Serial.write(); That also might be causing an error, i recommend using Serial.println();

                                     

                                    Regards,

                                    Intel_Jesus

                                    1 2 3 Previous Next