I have been trying to get this code to work this whole morning with no success. It is supposed to read LED values stored in a test.txt file on a sd card. And I do get the output on the Serial monitor, but for some reason it just doesnt light up the LEDs.
I have also tried just lighting up one LED with given values rather than the ones in the file, no success. The strip does work, i have tested it with the neo pixel demo library. I just don't understand what is wrong.
Here is my code, any help will be greatly appreciated.
#include <Time.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 4;
#define PIN 6
#define NUMPIXELS 400
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char inputString[5000]; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean readarray=false;
String inString = "";
int c=0;
int cc=0;
int led=0;
int i=0;
int j=0;
int pixel_n=0;
unsigned long start;
unsigned long endd;
//char* ledd[]={"", "", ""};
int ledd[3]={0,0,0};
File myFile;
void setup() {
Serial.begin(115200);
SD.begin(4);
pixels.begin(); // This initializes the NeoPixel library.
myFile = SD.open("test.txt");
while (myFile.available()) {
char inChar = myFile.read();
if (isDigit(inChar)) {
inString += (char)inChar;
}
if (inChar == '(') {
led=led+1;
}
if (inChar == ',') {
ledd[c]=inString.toInt();
inString="";
c=c+1;
}
if (inChar == ')') {
ledd[c]=inString.toInt();
inString="";
pixels.setPixelColor(led, pixels.Color(ledd[0],ledd[1],ledd[2])); // THIS DOES NOT SEEM TO WORK
pixels.show(); // THIS EITHER. IT DOESNT LIGHT UP THE LEDS
Serial.println(ledd[0]);
Serial.println(ledd[1]);
Serial.println(ledd[2]);
Serial.println("END OF LED ");
Serial.println(led);
c=0;
}
if (inChar=='*') {
Serial.write("NEXT FRAME");
myFile.close();
break;
}
}
}
void loop() {
}
pixels.setPixelColor(led, pixels.Color(ledd[0],ledd[1],ledd[2])); // THIS DOES NOT SEEM TO WORK
pixels.show(); // THIS EITHER. IT DOESNT LIGHT UP THE LEDS
Because it is not how the call works.
The first parameter is the LED number, the second is the colour. What you have written is a nonsense.
Maybe the Arduino cannot communicate with the SD card and the LED strip at the same time. So I tried storing each frame in a frame array matrix[400][3], then go through the array and light up the leds, and then flush the array at the end of each frame, but I get a memory error of course.
The SPI bus apparently messes with the pins. So I need to close it after reading, except I need to send data to the strip while reading on the sd card.
It's not, but it is clearly affecting my code. I am using pin 4 for SD card reading, and pin 6 for LED strip data line. When I do not include the libraries or initiate the SD card, everything is smooth. When I do include the SD libraries or try to interact with the SD card, I cannot send data to the LED strip anymore.
So it is the SD library for sure.
If at least I could first read the array on the SD card, close the SPI, store the array temporarily and use it, but I can't, my matrix is 400 x 3 rgb values.
This is really problematic. I have read about SDFat.h which apparently contains update for better SPI behavior. Any info on that? This is extremely inconvenient and problematic.
Just try and read one LEDs worth of data in, then shut down the SD card and try and write to the LEDs. It will tell you if that is the possibility of a fix.
Except I am not positive this will actually solve my problem. Is there not an option with a regular sd card reader? I have this one: here
EDIT: Sorry GrumpyMike didn't see your message. No this is not changing anything, either way the data led on the arduino doesn't even light up. It's just not sending anything, I believe the SPI.h allocates all the pins making them unusable for something else than the sd card.
There's gotta be a way to cope with that.
I believe the SPI.h allocates all the pins making them unusable for something else than the sd card.
No that is not true. I have used an SD card and A/D bit banged on pins and two things on the I2C bus, so it is possible to use other pins with the SD card.
Your reply said:-
I had the same problem with using the SD reader with a SPI SRAM. It turns out that the very cheap SD reader cards do not like multiple SPI components on the same port.
This is nothing like the problem you described, you have not got two devices on the SPI bus. Getting that card will not help you.
What Arduino are you using? You are using 5K as an input buffer, you might be running out of memory.
The animations I store on the sd card are stored as follows:
FRAME delimiter
400 rgb values as follows: (250,250,250)
END FRAME delimiter
There can be up to 500 frames per animation, and I need a lot of animations (dozens). My micro sd is 16gb so the issue is not the storage memory, but as you say maybe the input buffer.
The Uno only has 2K of memory. You use 1.5K for your led array and then want 5.1K as a buffer for the SD card. Can you see why it is not working?
You need to cut down on the buffers. Why do you need to read the whole of the frame array in and in ASCII? Do not have an SD buffer, simply have the frame data in hex in the SD card, read the SD card and then store it in the pixel buffer ( pixel.set ). You might get away with that but even then it will be touch and go.
If you get a Mega that has 8K of memory, if you need more then you will have to go for an Arduino Zero.