SD_uFAT Help. Trying to print a variable to Flash

I have been playing with this code for hours now. I got a sd flash drive connected to my Arduino and am using the SD_uFAT code. When I do this:

SD.print("hola.txt","7");

it writes 7 to hola.txt

I want to make "7" a variable but don't know the correct format to do it in. I tried =

int seven = 7; SD.print("hola.txt",(seven)); SD.print("hola.txt","(seven)"); SD.print("hola.txt","[seven]"); SD.print("hola.txt", seven); SD.print("hola.txt",((seven))); SD.print("hola.txt", 'seven'); SD.print("hola.txt",""(seven)"");

etc., but keep on getting errors. I was wondering what the appropriate code is to print a variable using SD_uFAT. If not, is there a better library I can download that is easy to use and has this function available?

Thx, Jackie

Did you try

int seven = 7;
SD.print("hola.txt", seven);

?

Yes, didn’t work :frowning:

Error message -

avrdude: write to file 'C:\Documents and Settings\jackie\Desktop\SDuFAT\SDuFAT\examples\SD_uFAT_basic\applet\SD_uFAT_basic.hex' failed
 In function 'void loop()':
error: call of overloaded 'print(const char [9], int&)' is ambiguoushardware\libraries\SDuFAT/SDuFAT.h:120: note: candidates are: int SDuFAT::print(const char*, byte*) <near match>


hardware\libraries\SDuFAT/SDuFAT.h:121: note:                 int SDuFAT::print(const char*, char*) <near match>

Here is line 119-121 of SduFAT.h

// print - append a string at the end of the card
int print(const char* filename, byte* data);
int print(const char* filename, char* data);

Try this…

int seven = 7;
SD.print("hola.txt", (char*)seven);

I tried that and it actually compiled but it would only create a new line, would not type 7 into hola.txt :(

int seven = 7;
SD.println("hola.txt", (char*)seven);

At least it compiled, were on the right track :)

By the way, here is the link to the library. http://blushingboy.net/p/SDuFAT/

I am using it with http://www.libelium.com/tienda/catalog/product_info.php?cPath=21&products_id=66

yeah, I dug out my sd card module and tried it after I made that post. I also got the same result. I'm trying to figure it out, I thought I had this all figured out but I guess I didn't and I definately need this to work for my project.

I'll update as soon as I can figure something out.

Thank you so much Iceman, it seems like an easy thing made hard :frowning: Keep me posted :slight_smile:

Ok, I got it working for numbers. The trick is that SD_uFAT is written to specifically handle writing strings to the sd card. Once I figured that out I was able to convert a number to a null terminated string so 7 becomes "7\0" and the uFAT library understands that.

The reason converting to a char doesn't work is because it thinks it's the ascii code 7 which is a non-printing character, often called a control character. The ascii value for 7 is 55 and if you were to create a 2 position array and put 55 in the firt position and '\0' in the second and send its pointer to the uFAT print or println function you would get a 7 when you read the file. You don't have to know all the ascii codes for simply converting a number to a string. There is a function called itoa() that will do the magic for you. You can see it working in the code sample bellow.

Just for fun I changed the number from 7 to 123 and it printed out 123 just fine.

Hope that helps.

include "SDuFAT.h"

define MEM_PW 8

char buf[5];

void setup(){ pinMode(MEM_PW, OUTPUT); digitalWrite(MEM_PW, HIGH); Serial.begin(9600); }

void loop(){ if (Serial.available() > 0) { int result = 0; byte inSerByte = Serial.read(); switch (inSerByte) { case 'R': result = SD.cat("hola.txt"); break; case 'P': int number = 7; result = SD.println("hola.txt", itoa(number, buf, 10)); break; } } }

Quick Update:

It can handle negative numbers as well.

Thanks Iceman, I got it to work. However it doesn't apply much to my application since it only prints numbers up to 5 digits and I wanted to use it to print a buffer full of numbers, about 200 or so. Anyway to print a number this long? Also is there a way to get it working with txt? Thanks again

Come to think of it, is there any way to simply write anything thats printed to serial using the Serial.print function, directly to the flash card?

Basically anything that shows up in serial monitor gets written to the flash card.

This would make things a lot easier.

I'm going to assume you have an array full of numbers that you want to print to the SD card. (if this is not correct then just let me know what you mean by buffer full of numbers)

In my application I'm reading an analog pin and storing each 0-1023 value that is read. I'm reading the pin 240 times a second so the SD card does not keep up with that speed. Thats where I have a FRAM module that I buffer all my readings to. From there I have a function that writes the contents of the buffer (FRAM chip) to my SD card. I'm prefacing all my bufferd numbers with an id followed by a comma. Each value read from the analog pin that was buffered in memory is written to the SD card followed by another comma.

To accomplish this simply loop through the buffer and send each number to the SD card using the print function and itoa.

I'm not sure if you can capture what you send out the serial port using the Serial.print. You can definately store what is entered into the serial monitor window of the arduino IDE.

Sence you are sending serial messages out fromt he arduino I'd just make a call to the SD card at the same time as you did your Serial.print call.

If I'm not even close here let me know some more details of what you are doing. Maybe some background info or code samples.

Thanks

Let me expand on that some more. (using the input box and send button on the arduino IDE) You can use the Serial.read() command to capture what you type in the input box and the value is the ascii value for what ever character you entered. throw that into a char array and your golden.

I basically want to write all the text that shows up in the Arduino IDE serial monitor to the SD card. Lets use this as an example:

void setup() 
{ 
  Serial.begin(9600); 
} 

void loop() 
{ 
  Serial.print("hello world");
  delay(1000);
}

If I put this code on the arduino and then click serial monitor on the arduino IDE it will print hello world every second. I want this to print to the SD card, basically whatever prints to serial monitor, will print to the SD card, no matter what it is, just like a log. This will solve all my problems if its something possible :)

I have to say that I am looking for something very similar to what you want, I am not looking to receive 240 readings a second, more like 4 or 5…

If you can get a solution to this then brilliant! Shame I can’t help you much/at all…

Mowcius

The problem I see is that you don’t recieve what you send out to the IDE.

This does not mean that you can’t accomplish what you want to accomplish if you approach it from a differant angle.

The messages going out to the IDE come from the arduino to begin with, you could simply write what ever messages you send out (using Serial.print) to the sd card at the same time. You could also write a function to capture all incoming serial bytes and store them to the SD card as well. This would capture anything that was sent out as well as what was received. For sending out messages simply write a function that takes what you want to send out and write it to the SD card as well as the call to Serial.Print. using the Serial.available() and the Serial.read() functions you can capture what comes in from the serial port.

Of course this is a solution to using the IDE’s serial monitor so its tailored to that. If your goal is to create a dataloger that simply listens to the serial port and writes what it gets to the SD card then the answer to your problem is much more simple. This is where the specifics of your project would determine the appropriate solution.

If you can give me some more details I’ll be glad to help you come up with a solution. If you can’t give any more details than that, just let me know if I’m hot or cold and We’ll take it from there.

I am really new to programming so the second option of the data logging listening to the serial port works best for me. I am working on a GPS datalogger that will Serial.print the location every 5 seconds. Can use for different applications to, right now I am just experimenting. If I can simply log everything that gets written to serial to the flash drive that would work great.

This should do the trick then…

#include “SDuFAT.h”
#define MEM_PW 8
char _SerialInput[201];

void setup(){
pinMode(MEM_PW, OUTPUT);
digitalWrite(MEM_PW, HIGH);
Serial.begin(9600);
}

void loop(){
int charCount = 0;
while (Serial.available()){
_SerialInput[charCount] = Serial.read();
delay(1);
charCount++;
}

if(charCount > 0){
_SerialInput[charCount] = ‘\0’;
SD.print(“hola.txt”, _SerialInput);
//For testing print out the results back to the serial monitor
for(int i=0; i<charCount; i++){
Serial.print(_SerialInput*);*

  • }*
  • Serial.println();*
  • }*
    }
    [/quote]
    I tested it with this 200 digit number.
    > 11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000

Hi Iceman, I appreciate all the effort your putting in to helping me. Unfortunately, that code only logs what you write in the Arduino IDE serial monitor box and press send. I want to log all activity being displayed in the serial monitor. Like if I see "hello world, Hello world, lalala" on my screen while in Arduino IDE serial monitor, it should also be in my SD card.

For example, the following code compiled onto my Arduino starts displaying the following in serial monitor:

#include "SDuFAT.h"
#define MEM_PW 8
char _SerialInput[201];

void setup(){
 pinMode(MEM_PW, OUTPUT);
 digitalWrite(MEM_PW, HIGH);
 Serial.begin(9600);
}

void loop(){
 int charCount = 0;
 while (Serial.available()){
   _SerialInput[charCount] = Serial.read();
   delay(1);
   charCount++;
 }  

 if(charCount > 0){
   _SerialInput[charCount] = '\0';
   SD.print("hola.txt", _SerialInput);

 }
 {
   Serial.print("hello world");
  delay(1000);


}
}

Displays in serial monitor:

hello worldhello worldhello worldhello worldhello worldhello world[hola.txt] - add string

hello worldhello worldhello worldhello worldhello world[hola.txt] - add string

hello world

The [hola.txt] - add string appeared everytime I entered any key in in the text box and clicked send. For instance, I typed 1 and then I typed 2. Instead of my txt file on my SD card displaying the hello world etc., it only logged "12", the text I wrote in the text box and pressed send. Maybe I have to switch Serial.write("hello world"); to something else that will mimic typing it into the text box?