Nano 33 IOT BLE + NeoMatrix

I'm running this code on Nano 33 IOT with NeoMatrix.

#include <ArduinoBLE.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

#define BLE_UUID_TEST_SERVICE               "9A48ECBA-2E92-082F-C079-9E75AAE428B1"
#define BLE_UUID_FILE_NAME                  "2D2F88C4-F244-5A80-21F1-EE0224E80658"
#define BLE_UUID_TX_CHAR                    "00002A58-0000-1000-8000-00805f9b34fb"

BLEService bleService(BLE_UUID_TEST_SERVICE);
BLEStringCharacteristic strRecCharacteristic(BLE_UUID_FILE_NAME, BLEWriteWithoutResponse | BLEWrite, 20);
BLEByteCharacteristic txChar(BLE_UUID_TX_CHAR, BLERead | BLENotify | BLEBroadcast);

//Pin where DIN is connected 
#define DATA_PIN 6

//Number of LEDs
#define COLUMNS 60
#define ROWS    10

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(COLUMNS, ROWS, DATA_PIN,
  NEO_MATRIX_TOP  + NEO_MATRIX_LEFT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG +
  NEO_GRB         + NEO_KHZ800);

String strRec = "";
unsigned int colors[3];
char dataRec[20];

int pixelPerChar = 4;
int maxDisplacement;
int x = matrix.width();

unsigned int dMode = 0;

void setup()
{
  Serial.begin(9600);
  delay(3000);

  while(!BLE.begin()){
    Serial.print(".");
  }
  Serial.println();
  Serial.println("BLE Initiated...");
  BLE.setDeviceName("Nano 33 BLE");
  BLE.setLocalName("Nano 33 BLE");
  BLE.setAdvertisedService(bleService);
  
  bleService.addCharacteristic(strRecCharacteristic);
  BLE.addService(bleService);
  strRecCharacteristic.writeValue(strRec);

  BLE.advertise();
  Serial.println("Started!!!");

  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(80);
  matrix.setTextColor(matrix.Color(255, 0, 0));
}

unsigned int timeX = 0;
uint16_t i=0, j=0, c=0;

void loop()
{
  BLEDevice central = BLE.central();
  if (central)
  {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected())
    {
      if (strRecCharacteristic.written())
      {
        strRec = strRecCharacteristic.value();
        Serial.print("Received from mobile: ");
        Serial.println(strRec);
        char fChar = strRec[0];
        switch(fChar){
          case 'c':
          {
            strRec[0] = ' ';
            strRec.toCharArray(dataRec, sizeof(dataRec));
            char* pch = strtok(dataRec, ";");
            int b = 0;
            while (pch != NULL)
            { 
              colors[b] = atoi(pch);
              Serial.println(colors[b]);
              pch = strtok(NULL, ";");
              b++;
            }
            matrix.fillScreen(matrix.Color(colors[0], colors[1], colors[2]));
            matrix.show();
            Serial.println("Display a single color");
            dMode = 1;
          }
          break;
          case 'r':
          {
            matrix.fillScreen(0);
            Serial.println("Starting Rainbow");
            dMode = 2;
            timeX = millis();
          }
          break;
          case 'd':
          {
            Serial.println("Start displaying the text");
            matrix.fillScreen(0);
            dMode = 3;
            timeX = millis();
          }
          break;
        }
        txChar.writeValue(1);
      }
      if(dMode == 3) {
        scroll("UpWork SG", 50);
      }
      if(dMode == 2) {
        doRainbow(20);
      }
    }
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

void scroll(char* message,int delays) {
  maxDisplacement = strlen(message) * pixelPerChar + matrix.width();
  matrix.fillScreen(0);
  matrix.setCursor(x, 0);
  matrix.print(String(message));
  if(--x < -maxDisplacement) {
    x = matrix.width();
    matrix.setTextColor(matrix.Color(0, 0, 255));
  }
  matrix.show();
  delay(delays);
}

void doRainbow(uint8_t wait) {
  for(j = 0; j < 256; j++) {
    for(c = 0; c < 10; c++){
      for(i = 0; i < 60; i++) {
        matrix.drawPixel(i, c, Wheel((i*1+j) & 255));
      }
      matrix.show();
      delay(wait);
    }
  }
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
    return matrix.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return matrix.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170;
    return matrix.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

I'm using an android app to send some strings to nano board.

There 3 modes,
1 - Single color for whole matrix
2 - Rainbow cycle
3 - Text scrolling

I can change 1 mode with any color for many times, it will work.

But when I changed into Rainbow or Text Scrolling app get disconnected. Loop doesn't seems to be breaking though, it doesn't show "Disconnected"

This doesn't happen with 1st mode.

Workaround - I re-wrote rainbow code without delay and it doesn't get disconnected, but need to send the command to switch to 1st mode at least 10 times.

Or if I don't run Rainbow and Textscrolling it works well, I receive the message from phone, no disconnect issue.

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