Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: mooop on Apr 27, 2017, 10:45 am

Title: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 10:45 am
Hi all,

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.

Code: [Select]
#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() {

}
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 27, 2017, 10:54 am
Code: [Select]
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.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 11:03 am
I hear you but how is this a nonsense?

the variable led is a number, and the array ledd[] contains the led rgb values as ints. How is this a nonsense?
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 11:12 am
I have tried with the following as well:

pixels.setPixelColor(i, (250,250,250));

It just doesn't work. The data LED doesn't even light up the Arduino.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 27, 2017, 11:31 am
Quote
the variable led is a number, and the array ledd[] contains the led rgb values as ints. How is this a nonsense?
Because that call expects ONE number not an array of numbers.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 11:39 am
Yes, ledd[0], ledd[1], ledd[2] all contain an individual number, do they not?

I thought maybe the problem was in the minimal code, with my variables declarations and libraries.

I tried this:

Code: [Select]
#include <Time.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN            6
#define NUMPIXELS      400

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
  Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.
      for (int i=0; i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, (0,0,0));
  }
  pixels.show();

  delay(2000);
}

void loop() {

  for (int i=0; i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, (25,250,250));
  }

  pixels.show();

  delay(20000);
 
}


Works like a charm. But if I add this extra code at the top, it doesn't work anymore. The LEDs don't respond.

Code: [Select]
#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);

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(9600);
  pixels.begin(); // This initializes the NeoPixel library.
      for (int i=0; i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, (0,0,0));
  }
  pixels.show();

  delay(2000);
}

void loop() {

  for (int i=0; i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, (25,250,250));
  }

  pixels.show();

  delay(20000);
 
}


Can it be a library/variable conflict?



Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 11:47 am
I have tried the following simple code and it works, so it's not that @Grumpy_Mike:

Code: [Select]
  for (int i=0; i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, (ledd[0],ledd[1],ledd[2]));
  }


It's when I include the SD libraries that it doesn't respond anymore. This is driving me crazy.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 11:52 am
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.

Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 12:09 pm
My problem seems related to this issue here:

https://arduino.stackexchange.com/questions/13698/use-led-on-pin-13-again-after-using-the-sd-library (https://arduino.stackexchange.com/questions/13698/use-led-on-pin-13-again-after-using-the-sd-library)

(http://i.imgur.com/HlvLSZF.png)

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.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 27, 2017, 05:45 pm
But your code is not using pin 13 is it?
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 08:03 pm
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.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 27, 2017, 08:42 pm
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.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 08:55 pm
Ok, thank you for your help again Grumpy_Mike,

I will try that tomorrow morning, for now I'm working on my animations!
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 09:42 pm
I just tried. No luck. Even with SPI.end(). I just cannot access the strip anymore.

Code: [Select]
#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);

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() {


  pixels.begin(); // This initializes the NeoPixel library.
  pixels.show();

    Serial.begin(9600);
  SD.begin(4);


  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="";
     
      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();
      SPI.end();
      Serial.println("Ok we're done");
      delay(2000);

      pixels.setPixelColor(5, (200,200,200));
      pixels.show();
      break;
    }   
  }
 
}

void loop() {

}
Title: Re: Neopixel and SD card
Post by: mooop on Apr 27, 2017, 10:00 pm
Tried with the SdFat.h, no luck either.

It's really odd, this is a very common way of doing. I'm trying to find animation scripts.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 28, 2017, 12:10 am
Code: [Select]
pixels.setPixelColor(5, (200,200,200));
No that's not right how about
Code: [Select]
pixels.setPixelColor(5, pixels.Color (200,200,200));
Title: Re: Neopixel and SD card
Post by: mooop on Apr 28, 2017, 01:37 pm
Any option or help is greatly appreciated.

I posted here: https://arduino.stackexchange.com/questions/37617/led-strip-and-sd-card (https://arduino.stackexchange.com/questions/37617/led-strip-and-sd-card)

This guy is advising me to go for this: https://www.adafruit.com/product/1141 (https://www.adafruit.com/product/1141)

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 (http://www.ebay.fr/itm/1007-Micro-SD-Card-Module-SPI-Interface-Mini-TF-Card-Reader-Writer-For-Arduino-/291970921924?var=&hash=item43fad2b5c4:m:mow6tuKgT3GZFdEjjbKZBsQ)

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.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 28, 2017, 11:05 pm
Quote
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:-
Quote
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.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 29, 2017, 11:21 am
I am using the latest available Arduino Uno.

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.

In which case, what would the solution be?

Thanks for all your help.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 29, 2017, 07:08 pm
Quote
I am using the latest available Arduino Uno.
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.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 30, 2017, 03:13 pm
I see.

How do you calculate how much buffer is needed though? How do you know I need 1.5k for my led array, and where does the 5.1k come from? I can see why it is not working because 5.1 is greater than 2, but I don't see how you get these numbers. So if you can enlighten me here :)
If there a way to calculate the needed sram for one frame of 400 rgb values in hex?

Second, how do you store the frame data in hex?

And finally, on this thread here (https://forum.arduino.cc/index.php?topic=365220.0) they seem to mention you can change the buffer size in some conf file of the arduino zero. Is this accurate?
The Arduino Zero has 32Kb in sram indeed, so that could be enough, but do you use the same way you use an Arduino Uno? Just upload and go?

EDIT: Ok so, hex does indeed take less storage space, but I don't know if it will fit in the buffer.
For one led value, made of maximum 3 numbers of maximum 3 digits, with the delimiters, I need 13 characters, 13 x 400=5.2K.
With the hex and the delimiters, I need 7 x 400=2.8K. So we are still too short here. So my only option is to get the Zero or Mega?

hex: A5C1BE,
rgb: (165,193,190)
Title: Re: Neopixel and SD card
Post by: mooop on Apr 30, 2017, 03:55 pm
Also my global variables already use 1020 bytes (49%) of the SRAM. So it's definitely not enough. My guess is that I need to upgrade to the zero or mega. Even with sketch optimization, I'll be way above 2k.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 30, 2017, 06:55 pm
Quote
How do you calculate how much buffer is needed though? How do you know I need 1.5k for my led array,
Each LED needs three bytes to store how bright it is, so 400 LEDs times 3 = 1200 bytes which is 1200/1024 = 1.1718K to be precise.

char inputString[ 5000 ] is 5000 bytes = 5000/1024 = 4.882K to be precise. ( not sure where 5.1K came from now I do the sums again )

Quote
If there a way to calculate the needed sram for one frame of 400 rgb values in hex?
It is 400 LEDs times 3 = 1200 bytes

Quote
And finally, on this thread here they seem to mention you can change the buffer size in some conf file of the arduino zero. Is this accurate?
Yes it is accurate but has absolutely nothing to do with what you are doing or want to do. This is the buffer for serial input, nothing to do with a buffer for an SD card.


Quote
For one led value, made of maximum 3 numbers of maximum 3 digits, with the delimiters,
You don't need delimiters at all. You don't need to read it all in first before you transfer it to your LED buffer. So you have no need for any SD buffer at all.
Title: Re: Neopixel and SD card
Post by: mooop on Apr 30, 2017, 09:38 pm
That inputstring variable was an old one I don't use anymore. But my variables still use 49% of available SRAM for some reason.

Here is my testing sketch at the moment. It reads one frame of an animation stored on the SD card and stops.

Code: [Select]


#include <Time.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#include <SD.h>
#include <SPI.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);

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() {


  pixels.begin(); // This initializes the NeoPixel library.
  pixels.show();

    Serial.begin(9600);
  SD.begin(4);

  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="";
      
      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;
    }  
  }

        delay(500);

      SPI.end();
      Serial.println("Ok we're done");
      delay(2000);

      pixels.setPixelColor(5, pixels.Color(200,200,200)); // THIS WILL NOT WORK
      pixels.show(); // THIS EITHER
      Serial.println("done");
  
}

void loop() {

}



I don't understand how I don't need a buffer. I am reading 400 values stored on the SD card, and only then I update the pixels. How do I send the value without reading it? Sorry I'm confused here.

Also, how do I not need delimiters? The only was I see is by reading char by char and send the value every 6 characters, but that's still 6 x 400=2.4K, and my global variables already use 49% of 2K. I would store the data like this: 4d220d4d220d4d220d4d220d4d220d4d220d4d220d4d220d4d220d but I still need the buffer no?

Thank you for your help and patience, it is much appreciated.

EDIT:

Quote
Each LED needs three bytes to store how bright it is, so 400 LEDs times 3 = 1200 bytes which is 1200/1024 = 1.1718K to be precise.
I need colors though. For me, this is all the data I need for one LED: 4d220d. No?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on Apr 30, 2017, 10:26 pm
Quote
I don't understand how I don't need a buffer.
You read one byte from the file and then you put it in the strip array. You repeat this until you have read 400 * 3 bytes. You do not read all the frame into memory and then transfer it to the strip array.

Quote
I need colors though. For me, this is all the data I need for one LED: 4d220d. No?
Also, how do I not need delimiters?
Yes just three bytes no delimiter because you always have three bytes, you don't need anything else to tell you that all the data has been read.


Quote
But my variables still use 49% of available SRAM for some reason.
Because that is only an estimate, it will not give you an accurate figure.
Title: Re: Neopixel and SD card
Post by: mooop on May 01, 2017, 12:58 pm
Quote
You read one byte from the file and then you put it in the strip array.
What is the strip array?

Quote
Yes just three bytes no delimiter because you always have three bytes, you don't need anything else to tell you that all the data has been read.
I'm really confused. Hex color 4d220d is 6 bytes, how do I only need 3, and which array do I store it into?

The way I was thinking of doing this is:

Code: [Select]
while c<400:

while char_count<6:

str value+=str char
char_count+=1

char_count=1
setpixelcolor(c, value)
c+=1

pixels.show()


What way are you thinking of doing this?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 01, 2017, 03:59 pm
Quote
What is the strip array?
The array that is set up when you do this:-
Adafruit_NeoPixel pixels = Adafruit_NeoPixel ....

Quote
I'm really confused. Hex color 4d220d is 6 bytes,
No it is not it is three bytes 0x4d, 0x22, 0x0d. A byte is 8 bits. It takes two characters to express one byte in hexadecimal but the storage is only one byte.

Your sample code seems to be in Python not C.
Quote
What way are you thinking of doing this?
Open a file and get the file pointer and store it in a variable called fileptr
then something like this:-

Code: [Select]

byte red,green,blue;
int count = 0;
while(count < 400){
  red = fileptr.read();
  green = fileptr.read();
  blue = fileptr.read();
pixels.setPixelColor(count, red,green,blue);
count ++;
}
pixels.show();


Edit this compiles.
Title: Re: Neopixel and SD card
Post by: mooop on May 01, 2017, 05:06 pm
It just doesn't work man, this is really annoying.

Just including the libraries messes it all up. This is the official simple test from the Adafruit Neopixel library examples. It works fine, and if I add the 2 includes SD.h and SPI.h, it doesn't work anymore.

Code: [Select]
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#include <SPI.h> // When I add these 2 lines,
#include <SD.h> // it stops working, nothing lights up

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      16

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}


However there is slight change: the Arduino "L" LED does light up. But the LED strip doesn't.
Title: Re: Neopixel and SD card
Post by: mooop on May 01, 2017, 06:45 pm
I found a working code on instructables for exactly my setup, except with 256 LEDs.

I read the frame files and it worked just fine. Then, I modified the frames, added 144 pixels to make them 400 pixels, modified the N_PIXEL variable (number of pixels) in the sketch, and it doesn't work anymore. So it is a memory issue.

Here is the working code with 200 pixels:

Code: [Select]
// NeoBoard

#include <SPI.h>
#include <SD.h>
#include <Adafruit_NeoPixel.h>

String inString = "";    // string to hold input
int char_counter=1;

int N_PIXELS = 200; // Number of pixels

void setup() {
  Serial.begin(9600);
  SD.begin(4);
}

void loop() {
  loadImages("Galaga0.txt");
  loadImages("Galaga01.txt");
}

void loadImages(String imagee) {
  File entry = SD.open(imagee);
  while (true) {
    //File entry =  dir.openNextFile();
    if (!entry) {break;}
    if (!entry.isDirectory() && entry.size() != 4096){
      //Serial.print(entry.name()); Serial.print(" - "); Serial.println(entry.size(), DEC);
      int bright = analogRead(1);
      //Serial.println(bright);
      int dim = map(bright, 0, 1023, 0, 255); //dimmer function
     
      Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, 6, NEO_GRB + NEO_KHZ800);
      strip.begin();
      strip.setBrightness(dim); //insert dimmer function
      int counter = 0; //start at 0

      while(counter<N_PIXELS){
        int inChar = entry.read();
        inString += (char)inChar;
        if (char_counter==6) {
          Serial.println(inString);
          Serial.println(counter);
         
          strip.setPixelColor(counter, strip.Color(0,150,0));
          inString = "";
          counter++;
          char_counter=0;
        }
        char_counter++;
      }
      strip.show();
      delay(5000); // This is how fast the arduino will cycle through the image
    }
    entry.close();
    char_counter=0;
  }
  //dir.close();
}


And if I change N_PIXELS to 400, it doesn't work. Here is what a frame file looks like:

Code: [Select]
29b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b29929b299... 400 times

So is my only option to buy a Mega or Zero?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 01, 2017, 06:59 pm
Quote
I found a working code on instructables
Instructables are crap, they are mainly written by idiots.

This particular idiot is using String which has problems with memory usage and tends to crash. It also loads in a heap of text files so it needs a large buffer

Here is some short code that will do what I said, I left the print in the code to see what it is doing. The data file is just filled up with some random hex numbers.
Code: [Select]

// Simple reading neopixels from an SD card by Mike Cook
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      6

#include <SPI.h>
#include <SD.h>

File fileptr;
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {
  Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.
  wipe();
  pixels.show();
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    fileptr = SD.open("test.PAT");
  if (fileptr) {
    Serial.println("test.PAT open");
    // fileptr.close();
  }

}

void nextPattern(){
byte red,green,blue;
int count = 0;
while(count < 6){
    Serial.print("LED ");
    Serial.print(count);
    Serial.print(" - ");
  red = fileptr.read();
    Serial.print(red); // for seeing what is happening comment out later
    Serial.print(",");
  green = fileptr.read();
    Serial.print(green);
    Serial.print(",");
  blue = fileptr.read();
    Serial.print(blue);
    Serial.println("");
pixels.setPixelColor(count, red,green,blue);
count ++;
}
pixels.show();
}

void loop() {
  for(int i=0;i<92;i++){
    Serial.println();
    Serial.print("Pattern ");
    Serial.println(i);
    nextPattern();
    delay(delayval);
  }
  fileptr.close();
  Serial.println("file closed");
  wipe();
  pixels.show();
  Serial.println("Finished patterns press reset to repeat");
  while(true) { } // hang here with the file closed
}

void wipe(){
       for(int i=0;i<pixels.numPixels();i++){
       pixels.setPixelColor(i, pixels.Color(0,0,0));
       }
  }


 
This is the print out in the serial monitor

Code: [Select]

Pattern 89
LED 0 - 48,48,48
LED 1 - 48,48,10
LED 2 - 48,48,48
LED 3 - 48,48,48
LED 4 - 10,48,48
LED 5 - 48,48,48

Pattern 90
LED 0 - 48,10,48
LED 1 - 48,48,48
LED 2 - 48,48,10
LED 3 - 48,48,48
LED 4 - 48,48,48
LED 5 - 10,48,48

Pattern 91
LED 0 - 48,48,48
LED 1 - 48,10,48
LED 2 - 48,48,48
LED 3 - 48,48,10
LED 4 - 48,48,48
LED 5 - 48,48,48
file closed
Finished patterns press reset to repeat



Attached is the test pattern file to put on the SD card.
Change the file type from .TXT to .PAT because the forum won't let me upload strange extensions.
P.S. I just made up the .PAT short for pattern.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 01, 2017, 07:09 pm
It looks like the limit on this is about 240 LEDs. You can probably get a bit more by taking out the print statements but it will not approach what you want so yes buy a Mega or a Zero.
Note if you go for a Zero then that uses 3V3 signals so you will have to build a buffer to drive the Neopixels.

Note we have knocked on the head your theory that you can't run an SD card and drive Neopixels.
Title: Re: Neopixel and SD card
Post by: mooop on May 01, 2017, 07:40 pm
Ok, I will buy a Mega then, the code is the exact same one right? 8K SRAM should be enough?

Thanks a lot for your help. One last question, when I modify the patterns file by adding 6 different colors:

29B299
B22929
000CFF
FBFF00
35AD4D
1EDADF
000000
000000
000000

The LED don't change colors, they're always in the blue spectrum. Why is that? I'm not mistake by saying that this:

29B299
B22929
000CFF
FBFF00
35AD4D
1EDADF

Is one frame of 6 pixels right? So my first pattern should display these pixels.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 01, 2017, 10:32 pm
Quote
when I modify the patterns file by adding 6 different colors:
How are you changing this file?

Quote
they're always in the blue spectrum.
The blue numbers are much bigger than the other ones.
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 11:09 am
Ok so I went to the electronics store, they didn't have any Arduino Mega, only a "replica" which the guy said work great. Regardless of what he said  I didn't have time to buy one online, so I got this one, and my SD shield does not seem to work with this one. It keeps telling me that the SD initialization failed. One time it detected the card, but said not FAT partition was found. It works great with the Arduino.

The Arduino Mega 2560 replica characteristics are the exact same, what am I doing wrong?

 I am using the example sketch from the SD library:

I am guessing this is due to shitty hardware, could be wrong.

Code: [Select]

// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

void setup() {
  // 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("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}


EDIT: CORRECTION. My Arduino is also returning an initialization error now. What the hell happened I didn't change anything? Is it possible that the Mega replica messed with the shield?

EDIT 2: CORRECTION. It is working on the Uno, I got the pins wrong. But it is definitely not working on the Mega replica.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 11:15 am
The mega has different pins for the SPI bus than the Arduino. In fact these lines are not on the normal pins but on the header pins. Do a search for the exact details.
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 11:23 am
I get this once in a while:


Initializing SD card...Wiring is correct and a card is present.

Card type: SD1
Could not find FAT16/FAT32 partition.
Make sure you've formatted the card


But most of the time just could not initialized. How come?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 11:29 am
Because the error messages assume you have an SD card connected to the SPI pins. You have not, they are floating pins so they can read back as anything. Hence the error messages make no sense.
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 11:33 am
I checked online, here is what I found:

MISO on pin 51
MOSI on pin 50
CLK on pin 52
CS on pin 53

I rewired this way, and it does not change anything. I still get card detected but no FAT partition once in 10 times or so.

Quote
Because the error messages assume you have an SD card connected to the SPI pins. You have not, they are floating pins so they can read back as anything.
Thanks for your help again. How do I fix this?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 11:37 am
Have you changed the software so that the CS pin is pin 53?
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 11:48 am
I have changed the chipSelect variable.

I found this, but when I try these lines of code I get an error. I guess the library doesn't support that anymore.

(http://i.imgur.com/w1kVu9C.png)
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 01:15 pm
Well the code as I posted works on my Mega by just changing the SD.begin(4) to SD.begin(53) and rewiring it:-

SPI Uno Mega
SS 10 53
MOSI 11 51
MISO 12 50
SCK 13 52

It also works when I set the number of pixels to 400.
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 01:35 pm
Ok it is now working, however I dot understand why when I change your test.PAT to my hex colors it doesn't change anything.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 01:58 pm
As I said before, how are you changing it and how are you checking it?
Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 02:59 pm
I insert the micro SD into my computer slot through an adapter, I modify the contents of the file as follows, for instance:

29B299
B22929
000CFF
FBFF00
35AD4D
1EDADF

I save, I insert the micro sd into the arduino shield, and it doesn't change the led values. Just blue.

Title: Re: Neopixel and SD card
Post by: mooop on May 03, 2017, 03:26 pm
There is also this text file from instructables containing led matrix values, but I don't even understand what type of data that is.

-16777216
-16777216
-29922026
-29922026
-29922026
-29922026
-16777216
-16777216
-16777216
-16777216
-16777216
-16777216
-29922026
-29922026
-29922026
-29922026
-27884011
-26635696
-26635696
-27884011
-20063548
-20063548
-20063548
-20063548
-20063548
-20063548
-27884011
-26635696
-26635696
-27884011
-20063548
-16777216
-16777216
-29922026
-21769968
-16777216
-16777216
-21769968
-29922026
-29922026
-29922026
-29922026
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 03, 2017, 07:36 pm
Quote
I modify the contents of the file as follows,
Yes I am asking how you modify the contents of the file to do that. It seems to me like you are not doing what you think you are doing. Most of those colours will be blue.
What if you change the numbers to be :-
800000
800000
800000
800000
800000
800000

Then you should see the first 6 as red. If you don't then you are not changing the file like you think you are.
Title: Re: Neopixel and SD card
Post by: mooop on May 04, 2017, 07:35 am
I modify the file by taking the micro sd card out of the shield, I insert it into my computer, I modify test.PAT, I replace the first couple lines with different HEX color codes like so:

FBFF00
35AD4D
1EDADF

I save the changes to the file, I take the micro sd card out of my computer, and put it back into the slot.

What am I doing wrong?
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 04, 2017, 07:44 am
You are still not answering my question. What software did you use to change the file?
Title: Re: Neopixel and SD card
Post by: mooop on May 04, 2017, 08:36 am
I used Sublime Text 2 for Mac, on Mac.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 04, 2017, 09:51 am
Thank you.
Now the problem with that is that is is only a text editor so you can not use it to change the hex values in a file, all it will do is change ASCII values. So as I said you are not doing what you think you are doing.
Look at the printout from that code I sent, you will see the LED values the code is reading. You will see that it is not what you thought you set in your text editor.

To change a file so that the bytes change to exactly what you want you need to use what is called a hex editor. As you are on a Mac you can use a free application called 0xED that starts with a zero. With that you can type ASCII, the code you get from your keyboard, to change the upper and lower nibbles of your byte.
Title: Re: Neopixel and SD card
Post by: mooop on May 04, 2017, 06:43 pm
Ok, thank you for your help.

However this is not working yet. Now I do get colors, but not the right one.

Here is the file, which is basically the same series of colors (highlighted in red), 80 times, to make it 400 LEDs.

(http://i.imgur.com/EMNGWoH.png)

These are the 5 colors FF15BB6F FF24B5E3 FFE90EE9 FFE3DA11 FFFA8223. I could be mistaken on that as I have added FF before the colors, to make them fit in what appears to be the 8 character "slots" of the 0xED software.

(http://i.imgur.com/c2EQ09Z.png)

And this is what the LED panel outputs:

(http://i.imgur.com/6m1kyjf.png)

So it does somehow seem to be the right colors, we're making progress, but they're not in the right order.

I am getting there, thank you so much for your help, let me know what I am doing wrong this time!


Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 04, 2017, 08:24 pm
Quote
I could be mistaken on that as I have added FF before the colors, to make them fit in what appears to be the 8 character "slots" of the 0xED software.
That is wrong. Just ignore the slots. Each pixel is represented by 3 bytes in Red, Green, Blue order, do not pad it out.

Also I don't get the same colours for those swatches. I get this:-
(https://forum.arduino.cc/index.php?action=dlattach;topic=473476.0;attach=210740)

Quote
These are the 5 colors FF15BB6F FF24B5E3 FFE90EE9 FFE3DA11 FFFA8223.
I think those colours are in the wrong order they seem to be blue, green, red.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 04, 2017, 08:39 pm
Try this file, untested so it might not work. It is a pattern for 400 LEDs in a 20 by 20 matrix, there are 13 frames in an animation. Can you change the code to cope with that?

Change the file type from .txt to .PAT
Title: Re: Neopixel and SD card
Post by: mooop on May 05, 2017, 06:01 pm
I get 6 patterns, so 6 frames, then the panel turns to white, and then it just shuts off. What the hell.

This is the code I am using:

Code: [Select]
// Simple reading neopixels from an SD card by Mike Cook
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      400

#include <SPI.h>
#include <SD.h>

File fileptr;
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {
  Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.
  wipe();
  pixels.show();
   if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    fileptr = SD.open("t4.PAT");
  if (fileptr) {
    Serial.println("test.PAT open");
    // fileptr.close();
  }

}

void nextPattern(){
byte red,green,blue;
int count = 0;
while(count < NUMPIXELS){
    //Serial.print("LED ");
    //Serial.print(count);
    //Serial.print(" - ");
  red = fileptr.read();
    //Serial.print(red); // for seeing what is happening comment out later
    //Serial.print(",");
  green = fileptr.read();
    //Serial.print(green);
    //Serial.print(",");
  blue = fileptr.read();
    //Serial.print(blue);
    //Serial.println("");
pixels.setPixelColor(count, red,green,blue);
count ++;
}
pixels.show();
}

void loop() {
  for(int i=0;i<13;i++){
    Serial.println();
    Serial.print("Pattern ");
    Serial.println(i);
    nextPattern();
    delay(delayval);
  }
  fileptr.close();
  Serial.println("file closed");
  wipe();
  pixels.show();
  Serial.println("Finished patterns press reset to repeat");
  while(true) { } // hang here with the file closed
}

void wipe(){
       for(int i=0;i<pixels.numPixels();i++){
       pixels.setPixelColor(i, pixels.Color(0,0,0));
       }
  }
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 05, 2017, 09:53 pm
OK try this file, I had a bit of a problem writing bytes in the other one.
This should work as I intended.

When the patterns finish all the LEDs will go off. Press the reset on the Arduino to repeat.
Title: Re: Neopixel and SD card
Post by: mooop on May 06, 2017, 12:03 pm
Ok, the problem is now solved. At least the display problem.

The LED panel is built, and it can now display animations with the technique you taught me @Grumpy_Mike. Entering actual hex codes rather completing them with FF to make the data chunks 8 characters long was the solution. I can now run gifs on the LED panel, and the frame rate is really good.

With a little less than 2 months remaining to finish this project, I now need to implement interaction with the computer in between each frame, or every 10 frames, something short. The animation will be divided into clips that will play one after the other, every 5 questions the user answers. The questions are answered with a 5 pad keyboard connected to a computer, which analyzes the inputs, and simply sends a signal to the LED panel Arduino Mega to jump to next clip.

I also have a bit of an issue with the colors of the led panel, which are really different. This picture here does not render well on my LED panel. You can barely see the difference in tint between the 2 blues, and the beige layer is barely visible, so as the contrast between the 2 oranges.

(http://i.imgur.com/CP3nOp7.png)

This is a minor issue, as I only need a palette of 5 different colors for each cell I will do (around 10), I can just live test the color palettes on the panel rather than on my computer screen, and see what looks best.

I do hope the rest of the job will go more smoothly, and I thank you so much @Grumpy_Mike for your help. I hope you're a good person and if you think you are I am willing to pay you a paypal coffee, you helped me a lot so far, thanks so much!

I will get back on here if I run into more trouble, thanks a lot, cheers!
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 06, 2017, 12:55 pm
Quote
I also have a bit of an issue with the colors of the led panel, which are really different.
To correct for this you need to use gamma correction. You can use this either when you generate the pattern or when you read it back from the SD card. The latter will slow things down a small tad but might be simpler to implement.
Here is a link telling you about it:- https://learn.adafruit.com/led-tricks-gamma-correction/the-longer-fix (https://learn.adafruit.com/led-tricks-gamma-correction/the-longer-fix)

Quote
I hope you're a good person
No wicked evil and grumpy  ;)

Thanks for the offer, but this is my hobby as well, hope your project goes well.  :)
Title: Re: Neopixel and SD card
Post by: mooop on May 06, 2017, 01:38 pm
Quote
No wicked evil and grumpy  ;)

Thanks for the offer, but this is my hobby as well, hope your project goes well.  :)
All right no problem!

I also get some LEDs blinking. Like, if one LED has to stay white for so long it will blink sometimes to yellow. Might be bad soldering of the data line. But overall the light is very stable.

Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 07, 2017, 08:14 am
I would try a few more large capacitors, this time in the middle as well as the corners.
Title: Re: Neopixel and SD card
Post by: mooop on May 15, 2017, 05:16 pm
Hey me again!

Is that the issue you think? I'm going to  try, it's really doing it a lot now. Pixels flashing to yellow.

Hook up the capacitors like so?

(http://i.imgur.com/ZhNKd17.png)
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 15, 2017, 07:23 pm
Quote
Is that the issue you think?
Well it is worth a shot. Does it happen with overall low light levels or does it just happen when things are bright?

This points to a poorly regulated supply or a power distribution problem. Capacitors can help with the power distribution.

Yes they are in the right place.

Is their any pattern to the misbehaving pixels? Like are they on the edge or middle of the display?
Title: Re: Neopixel and SD card
Post by: mooop on May 22, 2017, 02:04 pm
Quote
Does it happen with overall low light levels or does it just happen when things are bright?
It does seem to only happen with bright colors indeed.

Quote
Is their any pattern to the misbehaving pixels? Like are they on the edge or middle of the display?
I don't believe so. It's pretty random, in the center as well.
Title: Re: Neopixel and SD card
Post by: Grumpy_Mike on May 22, 2017, 06:09 pm
Quote
It does seem to only happen with bright colors indeed.
That seems to suggest a power supply problem.

In might be thermal, does this happen when you first switch on or is it only after they have been running for some time?
Sorry I can't be more definitive.