0

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

SuRu
  • 739
  • 1
  • 6
  • 19

2 Answers2

0

In setup function you should have:

void setup()
{
  pixels.begin();
  pixels.show();
}
Eriss69
  • 69
  • 5
0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sabatech
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34915930) – Ben A. Sep 01 '23 at 22:50