Working on a project which will eventually involve controlling 9 Neopixel LED strips simultaneously through Unity and the Kinect routed through an Arduino Uno. It works exactly how I'd like it to, except when I try anything more than 3 LEDs, it will periodically "freeze" and stop responding to input.
I've done some testing with this and found that I am indeed still sending the signal from Unity as normal. In order to unfreeze it, it must receive another signal first. This fact holds even when my entire loop function is checking the first digit of the received data and an else statement to change the color. When it freezes, it is obviously not receiving new data, but does not default to the else statement either. When doing my normal program (which receives a new color and sends it down the line), the colors stop moving altogether.
As I've said, I've never had problems using only three LEDs, regardless of which LEDs they are. Every unity frame I am sending new data to the Arduino, which then writes new info to the 60 LEDs in each strip (as well as a few additional operations like showing). I know Arduino's write function is sometimes slow, could this be the problem? Would this be fixed by using multiple Arduino's or some other chip?
I'm also still having some connection problems with a few strips which seems to be related to having a stable common ground. However, the problem does not change based on which strips are involved, only the number.
Any ideas for what I could be doing wrong? I've already solved the common problem of using sizeof for arrays. Below is the relevant parts of my code for the basic two color test described above and my normal operation.
Thanks in advance for the help!
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN1 1
#define PIN2 2
#define PIN3 3
#define PIN4 4
#define PIN5 5
#define PIN6 6
#define PIN7 7
#define PIN8 8
#define PIN9 9
int lightNum= 3;
Adafruit_NeoPixel strips[] = {
Adafruit_NeoPixel(60, PIN4, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(60, PIN5, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(60, PIN6, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(60, PIN7, NEO_GRB + NEO_KHZ800),
};
byte readByte[5];
int receiveBytes = 4;
void setup(){
stripsBegin();
showStrips();
Serial.begin(28800);
}
void loopTest(){
Serial.readBytes(readByte, 4);
if(readByte[0] == 5){
colorStrip(strip.Color(0, 255, 0));
r = 200;
} else{
colorStrip(strip.Color(255, 0, 0));
}
}
void loop(){
Serial.readBytes(readByte, 4);
gradient(readByte[1], readByte[2], readByte[3]);
}
void colorStrip(uint32_t c) {
for(uint16_t i=0; i<60;i++){//strip.numPixels(); i++) {
setStripPixels(i, c);
}
showStrips();
}
void showStrips(){
for(int i = 0; i < lightNum; i++){
strips[i].show();
}
}
void stripsBegin(){
for(int i = 0; i < lightNum; i++){
strips[i].begin();
}
}
void gradient(float lpos, float mult, float z){
float sr = 0;
float sg = 0;
float sb = 0;
sr = 30-lpos;
sb = lpos-30;
sg = lpos-abs(lpos-30);
if(z>50){
sb = sb + (z-50)*3;
sr = sr + (z-50)*3;
}
float w = sr*mult;
float k = sg*mult;
float c = sb*mult;
if(w<15){
w = 15;
}
if(k<15){
k = 15;
}
if(c<15){
c = 15;
}
if(mult==0){
w = 0;
k = 0;
c = 0;
}
if(w > 255){
w = 255;
}
if(k > 255){
k = 255;
}
if(c > 255){
c = 255;
}
colorWipe(strip.Color(w, k, c));
}
void colorWipe(uint32_t c) {
setStripPixels(0, c);
for(uint16_t i=0; i<strip.numPixels()-1; i++) {
int j = strip.numPixels()-1-i;
setStripPixels(j, strip.getPixelColor(j-1));
}
showStrips();
}