ฉันประสบปัญหาขณะพยายามแสดงรายการไฟล์ในการ์ด SD: ฉันมีรหัสต่อไปนี้:
#include <SPI.h>
#include <SD.h>
#include <TFT.h>
#define sd_cs 4
#define lcd_cs 10
#define dc 9
#define rst 8
TFT TFTscreen = TFT(lcd_cs, dc, rst);
PImage logo;
void intro() {
TFTscreen.begin();
TFTscreen.background(255, 255, 255);
TFTscreen.stroke(0, 0, 255);
TFTscreen.println();
TFTscreen.println("Welcome..");
delay(500);
}
void draw(char *imageName) {
TFTscreen.begin();
TFTscreen.background(255, 255, 255);
// Load the image file.
logo = TFTscreen.loadImage(imageName);
if (!logo.isValid()) {
Serial.println("error while loading the image");
}
if (logo.isValid() == false) {
return;
}
Serial.println("drawing image");
// draw the image to the screen
TFTscreen.image(logo, 0, 0);
}
void initSD() {
Serial.print("Initializing SD card...");
if (!SD.begin(sd_cs)) {
Serial.println("failed!");
return;
}
Serial.println("OK!");
}
void setup()
Serial.begin(9600);
intro();
initSD();
draw("2.bmp");
}
void loop() { }
โค้ดด้านบนใช้งานได้ดี ข้อความ "ยินดีต้อนรับ" จะปรากฏขึ้นบนหน้าจอ ตามด้วยการแสดงรูปภาพ "2.bmp" ตามที่คาดไว้ เอาต์พุตแบบอนุกรมจะพิมพ์ออกมา:
IniInitializing SD card...OK!
File size: 61496
Image Offset: 54
Header size: 40
Bit Depth: 24
Image size: 128x160
drawing image
ตอนนี้ฉันพยายามแสดงรายการไฟล์บนรูทของการ์ด SD:
File root;
void listSD() {
root = SD.open("/");
printDirectory(root);
}
void printDirectory(File dir) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
dir.rewindDirectory();
break;
}
Serial.println(entry.name());
}
}
void setup() {
Serial.begin(9600);
intro();
initSD();
listSD(); //<- this function here
draw("2.bmp");
}
แต่ฉันได้รับข้อผิดพลาด file not found ทันทีหลังจากแสดงรายการโฟลเดอร์รูท:
Initializing SD card...OK!
1.BMP
2.BMP
loadImage: file not found: 2.bmp
error while loading the image
คุณช่วยบอกฉันหน่อยได้ไหมว่ามีอะไรผิดปกติที่นี่? ฉันสงสัยว่า root = SD.open("/");
จะส่งผลต่อ TFTscreen.loadImage(imageName);
ในทางใดทางหนึ่ง แต่ฉันไม่รู้ว่าจะทำอย่างไร
ฉันใช้ Arduino UNO R3 ที่มีหน้าจอ TFT LCD เวอร์ชัน IDE คือ 1.5.5 .