Go Down

Topic: open random file from SD card (Read 6883 times) previous topic - next topic

strangemachines

I am trying to open a random text file from a SD card.

I am using an indexing array for the filenames but this method seems pretty dumb to me
Code: [Select]
File myFile;
char* myTXTs[]={"1.txt", "2.txt", "3.txt", "4.txt", "5.txt","6.txt","7.txt", "8.txt", "9.txt",
              "10.txt", "11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", "18.txt", "19.txt",
              "20.txt", "21.txt", "22.txt", "23.txt", "24.txt", "25.txt", "26.txt", "27.txt", "28.txt", "29.txt",
              "30.txt", "31.txt", "32.txt", "33.txt", "34.txt", "35.txt", "36.txt", "37.txt", "38.txt", "39.txt",
              "40.txt", "41.txt"," 42.txt"," 43.txt", "44.txt", "45.txt", "46.txt", "47.txt", "48.txt", "49.txt",
              "50.txt", "51.txt", "52.txt", "53.txt", "54.txt", "55.txt", "56.txt", "57.txt", "58.txt", "59.txt",
              "60.txt", "61.txt", "62.txt", "63.txt", "64.txt", "65.txt", "66.txt", "67.txt", "68.txt", "69.txt",
              "70.txt", "71.txt", "72.txt", "73.txt", "74.txt", "75.txt", "76.txt", "77.txt", "78.txt", "79.txt",
              "80.txt", "81.txt", "82.txt", "83.txt", "84.txt", "85.txt", "86.txt", "87.txt", "88.txt", "89.txt",
              "90.txt", "91.txt", "92.txt", "93.txt", "94.txt", "95.txt", "96.txt", "97.txt", "98.txt", "99.txt"};
              *
*
*
*

thisTXT = random(1, 100);

myFile = SD.open(myTXTs[thisTXT]);




this method uses a lot of ram and I'am asking if there is simpler way?
I would like to pick out one random text file from a pool of 2000 stored on SD card



Coding Badly

Code: [Select]

#include <stdio.h>

int thisTXT;
char FileName[[glow]7[/glow]];  [glow]// DO NOT make this buffer too small[/glow]

thisTXT = random(1, [glow]100[/glow]);  [glow]// DO NOT go over 100 without adjusting the size of the buffer[/glow]
sprintf( FileName, "%i.txt", thisTXT );
myFile = SD.open( FileName );

strangemachines

#2
Jan 23, 2011, 01:03 am Last Edit: Jan 23, 2011, 02:32 am by strangemachines Reason: 1
thx for that super fast answer.. What I am exactly trying to do is print out random text snipplets stored on sd card on a POS printer

here is the full code but it doesn't seem to print now (what it did before)

Since I am quite a noob this code ,ay look a little bit odd
;)
Code: [Select]
//Microprinter V0.1
#include <SD.h>

#include <Debounce.h> // für taster
#include <TrueRandom.h>
#include <stdio.h>
int taster = 5;       // INPUT PIN TASTER
int ledPin1 = 13;     // OUTPUT PIN LED
int thisTXT;
char FileName[7];

Debounce debouncer = Debounce(20 , taster ); //debounce Taster mit 20ms Debounce Zeit

const byte esc = 0x1B; //definiert esc zur Druckerkommunikation
const byte gs = 0x1D;  //definiert gs zur Drukcerkommunikation

File myFile;




             
             






void setup()
{


 pinMode(ledPin1, OUTPUT); //
 pinMode(taster, INPUT);
 digitalWrite(taster, HIGH);

 Serial.begin(9600); //serial anschluss für den Drucker


randomSeed(TrueRandom.random());
SD.begin();
}



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



void SDPrint()
{
 
thisTXT = random(1, 100);  // DO NOT go over 100 without adjusting the size of the buffer
sprintf( FileName, "%i.txt", thisTXT );
     
   
 
 if (!SD.begin(4))
 

myFile = SD.open( FileName );
 if (myFile) {
   

 
   
   // read from the file until there's nothing else in it:
   while (myFile.available()) {
     
 
         Serial.write(myFile.read());


   
   }
   

   // close the file:
   
   myFile.close();
 } else {
   
   
       // if the file didn't open, print an error:
   Serial.print("kann Texte nicht lesen");
Serial.print(0xA, BYTE);

 
 }
}

void loop()

{

 debouncer.update();

 int tasterVal = debouncer.read();
 int val = digitalRead(taster);
 if (val == HIGH) {
   digitalWrite(ledPin1, HIGH);
  SDPrint();
   delay(1000);

 }

 else {
   digitalWrite(ledPin1, LOW);

 }
}



Coding Badly

Quote
it doesn't seem to print now (what it did before)

Does the printer have paper?

strangemachines

the printer has plenty of paper

and it works with the following code change:

Code: [Select]
 initprinter();
 schrift();
 umlaut();
 center();
thisTXT = random(1, 120);  // DO NOT go over 100 without adjusting the size of the buffer
sprintf( FileName, "%i.txt", thisTXT );



Coding Badly

Quote
and it works with the following code change

Glad to hear you have it working.

Did you increase the buffer size?

strangemachines

I increased the buffer size to 8. The esc/POS protocol for the printer communication seems to be a little bit picky but everything is running fine now.

What does the stdio libary do exactly? I couldn't find any reference for it on the arduino pages.

Coding Badly

#7
Jan 23, 2011, 08:32 pm Last Edit: Jan 23, 2011, 08:36 pm by bcook Reason: 1
Quote
What does the stdio libary do exactly?

For very general questions like that, the best choice is...
http://www.google.com/search?q=stdio

Bear in mind that, on the AVR processor, stdio deviates from the standard stdio.
http://www.google.com/search?q=stdio+avr+gcc

Go Up