Hello ![]()
first post here, I'll try to be as clear as possible, forgive me in advance if I'm not precise enough.
My project:
I'm working on a project for an exhibition where Arduino randomly selects .txt files containing small ASCII art from an SD card and sends them to a thermal printer. The function will be trigged by people pressing a button.
What I'm working with:
• Adafruit thermal printer
• Arduino Uno
• Shield and 8GB SD card
So far I've been able to make Arduino select random files from the SD card and print them. My first try was with just 4 .txt files and when run on the serial monitor the code worked fine, displaying the random ASCII art.
However, when I went to add more files into the SD card (I put 20), on the serial monitor a series of random character appeared (I attach a picture). I tried to gradually lower the number of files and it seems that when no more than 10 files are in the card the monitor can still display the actual ASCII art, but as soon as more are stored, the problem represents itself.
I attach my code here, I'm not sure if the issue is within it or has something to do with the hardware.
I hope someone with similar experiences or suggestions can help me! ![]()
Thank you!
/* SDlistFiles
This example shows how print out the files in a directory on a SD card
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 Adafruit #2971 and Metro/Uno
created Nov 2010 by David A. Mellis
modified 9 Apr 2012 by Tom Igoe
modified 2 Feb 2014 by Scott Fitzgerald
modified 12 Apr 2018 by Mike Barela
This example code is in the public domain.
*/
#include "Adafruit_Thermal.h"
#include <SPI.h>
#include <SD.h>
#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
File root;
int run;
int buttonPin;
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT); digitalWrite(7, LOW);
mySerial.begin(19200); // Initialize SoftwareSerial
printer.begin(); // Init printer (same regardless of serial type)
// Open serial communications and wait for port to open:
Serial.begin(9600);
randomSeed(analogRead(0));
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip
Serial.print("");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("");
root = SD.open("/");
printDirectory(root, 0);
// re-open the file for reading:
root = SD.open(getRandomFilename("/", getFileCount("/")));
if (root) {
Serial.println(getRandomFilename("/", getFileCount("/")));
// read from the file until there's nothing else in it:
while (root.available()) {
Serial.write(root.read());
}
// close the file:
root.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
Serial.println(getFileCount("/"));
Serial.println(getRandomFilename("/", getFileCount("/")));
//
// Serial.println(getFileName(root, 0, random(countDirectory(root,0)) ) );
}
void loop() {
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
String getFileName(File dir, int numTabs, int index)
{
int counter = 0;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
if (counter == index) return entry.name();
Serial.print(entry.name());
entry.close();
counter++;
}
}
int getFileCount( String dir )
{
File d = SD.open( dir );
int count_files = 0;
while ( true )
{
File entry = d.openNextFile();
if ( !entry )
{
// no more files. Let's return the number of files.
return count_files;
}
String file_name = entry.name(); //Get file name so that we can check
//if it's a duplicate
if ( file_name.indexOf('~') != 0 ) //Igrnore filenames with a ~. It's a mac thing.
{ //Just don't have file names that have a ~ in them
count_files++;
}
}
}
String getRandomFilename( String dir, int maxIndex )
{
File d = SD.open( dir );
int count_files = 0;
int random_index = random(maxIndex - 1);
while ( true )
{
File entry = d.openNextFile();
if ( !entry )
{
// no more files. Let's return the number of files.
//return count_files;
}
String file_name = entry.name(); //Get file name so that we can check
//if it's a duplicate
if ( file_name.indexOf('~') != 0 ) //Igrnore filenames with a ~. It's a mac thing.
{ //Just don't have file names that have a ~ in them
if ( count_files == random_index) return file_name;
count_files++;
}
}
}
