Adafruit_NeoPixel.h library hindering I2C communication

I am trying to print a message on NeoPixel Array using Adafruit_NeoPixel.h library. However, the data to be displayed is received by Arduino Nano via I2C. The problem is that "matrix.show()" command slows down the loop and packets that are supposed to be received on i2c are getting missed. Please help with a solution. Please find my code here:

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
char command;
#define PIN 9

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(30, 7, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);

const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255), matrix.Color(0, 255, 255)
} ;
int x = matrix.width();
int CurrentTime[] = {0, 0, 0, 0};

void setup() {
Serial.begin(9600);
Wire.begin(1);
Wire.onReceive(receiveEvent);
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(100);
matrix.setTextColor(colors[1]);
}

void loop() {
matrix.fillScreen(0);
matrix.setCursor(0, 0);
matrix.print(currentTimeValue[0]);
matrix.print(currentTimeValue[1]);
matrix.print(':');
matrix.print(currentTimeValue[2]);
matrix.print(currentTimeValue[3]);
x = matrix.width();
matrix.show();
}

void receiveEvent(int numBytes)
{
command = Wire.read();
if(command>0){
Serial.println(command);
}

This is not the complete code. But even this code, loses data packets on I2c.
Any help will be apprecaited on this topic.

Thanks
Vishal Mehta

Never call a method that depends on interrupts inside an interrupt handler!

@pylon Thanks for the reply.
Didn't understand what you mean by calling a method inside an interrupt handler. I just need to read what's being sent to slave on I2C. All I am doing is reading the available data on I2C and printing it on Serial monitor.

What's the better way of doing this?
I am a bit new to this and thus, any help would be greatly appreciated.

There is no good solution.
When a Arduino is set into I2C Slave mode, then it has to respond very quickly. Some libraries turn off the interrupts for some time, or take over the Arduino board. Those libraries cause problems (Neopixel, FastLED, OneWire, VirtualWire, RadioHead in ASK mode, SoftwareSerial, and so on).

Removing the Serial.println() from the onReceive interrupt handler will improve things. But I prefer not to combine the Slave mode with the Neopixel library.

Why do you want to set the Arduino board in Slave mode ?
The I2C bus is for retrieving a few bytes from a sensor.
When you want to communicate between Arduino boards, then the I2C bus is a bad idea.

If possible, try to use a single Arduino board.
If that is not possible, then you should still try to use a single Arduino board.

Please tell us more about your project.

@Koepel Thanks for the reply!!
I am trying to diplay messages on the Neopixel Matrix. These messages are sent from a master Arduino to multiple Slave Arduinos which in turn prints the message on to their respective Neo-Pixel Matrix. Thus, it can't be a single Arduino Project.

I can turn that around: Maybe it can't be multiple Arduino boards.

What is your Master board ?
How many Slave boards do you need and how many pixels do the displays have ?
At what distance are the Slave boards ?
A single Arduino board can show message on different displays. If the total of RGB leds is above 300, then it will no longer fit in a Arduino Uno.
If a fading sequence or other sequence is needed on more than one display at the same time, then the sketch may no longer use delay(). It is possible to do that with a single Arduino board. However, if the total of RGB leds is above 1000 or 2000 or so, the update gets slower and slower.

If you have a sequence with delay() which is hard to rewrite without delay(), then it is possible to use a Arduino board that can run a simple Schedular or even a full multitasking system. Each task would control its own Neopixel display.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.