Pages: [1]   Go Down
Author Topic: open random file from SD card  (Read 6534 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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


Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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 );
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 smiley-wink
Code:
//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);

  }
}

« Last Edit: January 22, 2011, 08:32:42 pm by strangemachines » Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it doesn't seem to print now (what it did before)
Does the printer have paper?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the printer has plenty of paper

and it works with the following code change:

Code:
 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 );

Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and it works with the following code change
Glad to hear you have it working.

Did you increase the buffer size?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 23, 2011, 02:36:19 pm by bcook » Logged

Pages: [1]   Go Up
Jump to: