SuRu
SuRu

Reputation: 739

Arduino Adafruit NeoPixel LED matrix is not working properly

I'm new to Arduino development, trying to display alphabets in an 8x8 LED matrix, but the simulation is not working

The code was working fine with one letter, what am I doing wrong in the below example?

How to debug code and is there any way to add print statements in tinkercad simulation?

#include <Adafruit_NeoPixel.h>

#define PIN 4 // input pin Neopixel is attached to

#define NUMPIXELS 64 // number of neopixels in strip

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

const int8_t alphas[26][NUMPIXELS] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
:
: // alphabet values
:
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

uint8_t i = 0;

void setup()
{
  pixels.begin();
}

void loop()
{
  delay(2500);
  if (i == 26)
  {
    i = 0;
  }

  for (uint8_t j = 0; j < 64; j++)
  {
    int8_t pix = alphas[i][j];
    if (pix == 1)
    {
      pixels.setPixelColor(j, pixels.Color(255, 0, 0));
    }
    else
    {
      pixels.setPixelColor(j, pixels.Color(255, 255, 255));
    }
  }
  pixels.show();
  ++i;
}

tinkercad sim

Upvotes: 0

Views: 523

Answers (2)

Sabatech
Sabatech

Reputation: 1

The provided code seems mostly correct but there are a few minor improvements that can be made. Here's the fixed version of the code with explanations for the changes:

#include <Adafruit_NeoPixel.h>
#define PIN 4            // Input pin Neopixel is attached to
#define NUMPIXELS 64     // Number of neopixels in strip

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

const uint8_t alphas[26][NUMPIXELS] = {
     // ... (alphabet values)
};

uint8_t i = 0;

void setup() {
  pixels.begin();
}

void loop() {
 delay(2500);

if (i == 26) {
   i = 0;
}

for (uint8_t j = 0; j < NUMPIXELS; j++) { // Use NUMPIXELS here
  uint8_t pix = alphas[i][j]; // Change data type to uint8_t
  if (pix == 1) {
    pixels.setPixelColor(j, pixels.Color(255, 0, 0));
  } else {
    pixels.setPixelColor(j, pixels.Color(255, 255, 255));
  }
 }

 pixels.show();
 i++;
}

Changes made:

  1. In the loop, you should use NUMPIXELS instead of the literal value, 64. This way, if you change the number of pixels in the future, the loop will still work correctly.

  2. Changed the data type of the pix variable in the loop to uint8_t, because the values in the alphas array are of type uint8_t.

These changes should help improve the code's readability and maintainability.

Upvotes: 0

Eriss69
Eriss69

Reputation: 69

In setup function you should have:

void setup()
{
  pixels.begin();
  pixels.show();
}

Upvotes: 0

Related Questions