Go Down

Topic: insert a carraige return? (Read 440 times) previous topic - next topic

bobthebanana

hi, I'm pretty new to the arduino language and the current program I'm working on doesnt make much sense to me. First of all heres the code(mostly copied from other people):
Code: [Select]


//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>

//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

char name[] = "10.txt";     //Create an array that contains the name of our file.
char contents[256];           //This will be a data buffer for writing contents to the file.
char in_char=0;
int index=0;                  //Index will keep track of our position within the contents buffer.
int temp;
int data_size;
int in;

void setup(void)

    Serial.begin(9600);        //Start a serial connection.
    pinMode(10, OUTPUT);       //Pin 10 must be set as an output for the SD communication to work.
    card.init();               //Initialize the SD card and configure the I/O pins.
    volume.init(card);         //Initialize a volume on the SD card.
    root.openRoot(volume);     //Open the root directory in the volume.
}
void loop(void){
    temp = (1.8663 - (5.0 * analogRead(A5) / 1024.0)) / 0.01169; 
    file.open(root, name, O_CREAT | O_APPEND | O_WRITE);    //Open or create the file 'name' in 'root' for writing to the end of the file.
    sprintf(contents, "Temp:,%0d", temp);
    file.print(contents);    //Write the 'contents' array to the end of the file.
    file.close();            //Close the file.
   
delay(1000);
}



Basically I'm reading a temp. sensor and (if I understand properly) sending the data to a buffer and then to my sd card with the help of the sdFat library. So the problem is that I want a carraige return after each reading but I dont know how to accomplish that. Currently when I read off the card I get this: Temp:,24Temp:,25Temp:,24 but i want to get:

Temp:,24
Temp:,25
Temp:,24
Do any of you know how to do this?

aremotelocation

#1
Jul 30, 2011, 04:51 am Last Edit: Jul 30, 2011, 04:54 am by aremotelocation Reason: 1
change this line sprintf(contents, "Temp:,%0d", temp);
to
sprintf(contents, "Temp:,%0d\n", temp);

reference: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

bobthebanana

thanks for the quick reply. and even more for the refference. Will use it again!

Go Up