Go Down

Topic: Read data from .txt file (from SD card) to run pan & tilt servos (Read 4732 times) previous topic - next topic


Hello Everyone,

I'm trying to build a project which involves animatronics (realistic human neck simulation). The Idea is to play the servos from the recorded/datalogged data. This data is generated from a wii remote using DarwiinRemote program. Im trying to use this data in the .txt format which is then uploaded onto a SD card slot of an ethernet shield through which Im trying to read the file. Though I found dumpfile and read/write examples in arduino, but I am unable to understand how to use this data to run a servo. I have also attached the .txt file for the reference. I want to access the AccX, AccY, AccZ data sequentially and individually in order to play the corresponding three servos.

incase if you are unable to view the attachment, the .txt file looks like this


hope I am able to communicate my thoughts to you all. Any suggestions and guidance is appreciated.



Should be doable; remember, readable by human and readable by computer are 2 very different things. 

you have floating point values there for each number; Those numbers aren't too bad but they represent what the sensor said and not what a servo would have to do to replicate that number.  Also, they need to (ought to) be quantized into integers that a servo would understand if you used the servo library to ease input/output math.  That value could be stored as 1 or 2 bytes in a formatted binary file instead of as ascii text in a human readable file.

The numbers you have represented there indicate an orientation in x, y, and z assuming gravity pulls down, there is 1 g of acceleration on the z, and x/y are both very low values. 

You need to do a bit of math to convert those acceleration amplitudes into an angle that can be represented as pan and tilt.  It would be a good plan to import these values into excel and convert the x and y values into a number between 0-255 for full negative deflection (-1) and full positive deflection (1).
Numbers over 1g indicate that more than gravity was acting on this thing, and it was accelerating on its own accord.


Thanks for the quick revert Frollard. I'm completely aligned with your thought. But my concern was,

1. Are there any functions from Arduino to convert these large values (example: 0.018519) to (between: 0-1000)
2. how to access the desired columns (top to bottom) from the .txt file individually so assign these converted values to corresponding servos.



Are there any functions from Arduino to convert these large values (example: 0.018519) to (between: 0-1000)

0.018519 is not a large value. What integer value should that correspond to?

The suggestion was that the PC end do all the work of converting the floats to angles that the servo would move to, so that all you'd have to do is read a servo position and send the servo there.

how to access the desired columns (top to bottom) from the .txt file individually so assign these converted values to corresponding servos.

The data on the SD card is arranged byte after byte. There is nothing about rows and columns inherent in the data. It is up to you to read a row/record, stopping reading whenever you have read a whole row/record.

If the records are ended by carriage returns or line feeds or both, stop reading when you have detected one of them (or both of them). If the records are fixed width, with do delimiters, stop when you have read the correct number of characters.

Parsing the "columns" depends on how a "column" is defined. strtok() and atoi() and atof() bear investigation. Not strtok_r().


PaulS put it very simply, you have to read the file from end to end, stopping at the data you want.

If you save your data in CSV -- comma separated values -- you can store columns in a program like excel as

Then have a sketch read the file, one byte at a time.  if it encounters a comma, it moves on to the next data value.  If it encounters asterisk, it stops reading until the next timestamp.

Not super useful code but it gets what I mean across I hope;  Won't work unless the file is explicitly designed to do this.  Might also trigger false positives if the values of "," and "*" come up...
Code: [Select]

//assumes a binary file with "," separating values in bytes, and "*" and the end of each line.
char dataByte[2];
int myservos[4];
int i = 0;
while dataByte[0] != "*"{
dataByte[0] = dataByte[1];
dataByte[1] = myFile.read();
//do something to act on that byte, this is where Paul's suggestion of atoi(); //ascii to integer comes in since you
//might need to load more than one byte to make a number bigger than 255
if (dataByte[0] == ",") {
myservos[i] = dataByte[1];


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131