I have strange behavior in a addressing LEDs in a neopixel matrix. I made my own 10x6 matrix and control it with a nano.
When i use a matrix in the code and based on if it is 1 or 0 I light a LED, it works just fine (heart in the code). With this code a heart is shown which moves one place to the right.
However when I try to make a matrix with the color codes it goes wrong, no matter what I put in the matrix, all LEDs will be some blueish. (Lips in the code)
When I print the code I fetch from the matrix, it is wrong. And instead of giving the code for magenta, I get 607 6 times, then 647 6 times and each row 40 is added.
I already tried changing the matrix to different values, but that gave no change in the output.
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#define NEO_PIN 12
#define NUM_PIX 60
#define LED_PIN 13
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIX, NEO_PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial mySerial(0, 1);
#define HOR_MAX 10
#define VERT_MAX 6
unsigned long heart_matrix [VERT_MAX][HOR_MAX] =
{ {0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0}
};
uint32_t magenta = pixels.Color(255, 0, 255);
uint32_t lips_closed [VERT_MAX][HOR_MAX] =
{ {magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
{magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
{magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
{magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
{magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
{magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta, magenta},
};
int delayval = 400; // delay for 400ms
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
// Serial.begin(9600);
mySerial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void heart(int x_offset)
{
int pix_id;
for (int i = 0; i < HOR_MAX; i++) {
for (int j = 0; j < VERT_MAX; j++) {
pix_id = ((i + x_offset) * VERT_MAX) + j;
if (heart_matrix[j][i] == 1)
pixels.setPixelColor(pix_id, pixels.Color(60, 0, 10));
else
pixels.setPixelColor(pix_id, pixels.Color(0, 0, 0));
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
}
void lips() {
uint32_t x;
int pix_id;
for (int i = 0; i < HOR_MAX; i++) {
for (int j = 0; j < VERT_MAX; j++) {
pix_id = (i * VERT_MAX + j);
x = lips_closed[j, i];
pixels.setPixelColor(pix_id, x);
mySerial.print(i);
mySerial.print(" ");
mySerial.print(j);
mySerial.print(" ");
mySerial.print(pix_id);
mySerial.print(" ");
mySerial.println(x);
}
}
pixels.show();
}
void line_draw()
{
for (int i = 0; i < VERT_MAX; i++)
pixels.setPixelColor(i, 45000);
pixels.show();
delay(1000);
}
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.
// line_draw();
digitalWrite(LED_BUILTIN, HIGH);
heart(0);
delay(delayval);
heart(1);
delay(delayval);
digitalWrite(LED_BUILTIN, LOW);
mySerial.println("lips");
lips();
delay(5000);
}
this is part of the output:
7 3 45 887 (so row 7, 4th led 7x6 + 3= LED nr 45 in the strip, color is 887)
7 4 46 887
7 5 47 887
8 0 48 927 40 is added
8 1 49 927
8 2 50 927
8 3 51 927
8 4 52 927
8 5 53 927
9 0 54 967 40 is added
9 1 55 967
9 2 56 967
9 3 57 967
9 4 58 967
9 5 59 967