Hello guys, newbie here so please dont be too hard on me.
So, my project is an animated pixelart clock made with the following components:
One arduino mega.
One screen made of 16x16 ws2812b leds:
on regular days it will show images stored in a sd card.
on special ocasions, such as birthdays, it will display special images that are appropriate for the date.
Six Regular led matrixes:
used to display time, date and temperature.
One Rtc module.
All went well at first until i realized PROGMEM could only handle 64kb, which is not enough to store many images.
So, to solve that i decided to add an sd card module to allow me to store more images for my RGB led matrix.
What i need the loadImages function to do once called is to open the first file stored in the SD card, which contains an array of long variables, send those variables to the RGB matrix and close. Once that function is called again in the loop it should open the next file and do the same, so that every time i run through the loadImages function it shows a new picture. I cant simply display all images in a while loop because that would freeze my clock.
So, i studied the SD library examples and documentation and came to the conclusion that openNextFile() function i what i need to call but i believe i am misunderstanding it because it returns nothing every time i call it.
After almost a week of tinkering desperation kicked in and i decided to copy an instructables code (i know, not great) and for some wizardry it works, except that it uses a while loop to display all images, which messes with my clock. I tried changing the code to the best of my skills but i cannot get the openNextFile() function to return anything outside of a while loop.
There must be something i am misunderstanding here, so... any tips, guys?
Here is the relevant part of the code i am using:
String inString = "";
void loadImages() {
File dir = SD.open("/");
while (true) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
if (!entry.isDirectory() && entry.size() != 4096) {
FastLED.clear();
int counter = 0;
while (entry.available()) {
int inChar = entry.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
int32_t c = -inString.toInt();
inString = "";
leds[counter] = c;
counter++;
}
}
FastLED.show();
delay(200);
}
entry.close();
}
dir.close();
}
Please follow the advice on posting code given in posting code
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
Hello, UKHeliBob, thank you for your reply and guidance.
Sorry, i didnt know how to post code...
Well, my problem is quite simple... and my full code is quite large, bigger than it is allowed. I will try cutting comments. Also, my project is working reasonably well, the only thing is that i cant call openNextFile() outside of a while loop so it freezes my code right there. After much trial and error i simply cannot make it work outside of a while loop...
I got it to work somehow...
I decided to start over writing the function and now it works, not sure what i fixed... here is the code i am using right now.
Well, just a new minor problem popped up though... the first led in the neopixel strip is always black when i run this new sketch... the led itself is not broken, i tested it and it works fine in the old sketch. Dunno why. What in my new code you guys think might be causing this issue?
#include <SPI.h>
#include <SD.h>
File root;
String inString = "";
#include "FastLED.h"
#define NUM_LEDS 256 // Número de leds ws2812b
#define DATA_PIN 2 // Pino conectado ao primeiro led Ws2812b
#define USE_OLD_STYLE_WIRING
CRGB leds[NUM_LEDS]; // Define o array de leds Ws2812b
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Init of the Fastled library
FastLED.setBrightness(75);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(22)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
root = SD.open("/");
}
void loop() {
loadImages(root);
Serial.println("deu a volta");
}
void loadImages(File dir) {
FastLED.clear();
byte counter = 0;
File entry = dir.openNextFile();
if (!entry) {
dir.rewindDirectory();
}
if (!entry.isDirectory() && entry.size() != 4096)
{
while (entry.available()) {
int inChar = entry.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '\n') {
uint32_t c = -inString.toInt();
leds[counter] = c;
inString = "";
counter++;
}
}
FastLED.show();
delay(10);
entry.close();
}
}