Addressable RGB strip array processing slow - help to speed things up

Hello,

Looking for some help to see how I can improve the response of my RGB strip. Some details behind the project;

I have an ESP8266 setup to receive a UDP string which is parsed and some simple maths applied to convert this into values which correspond to the power being used by a house, power generated by a solar array and power provided by a battery system.

There is an array of "bins" where i lookup the 3 current powers and determine the points to be lit on the display. As well as this, i have a set of markers which get added in to denote various power levels.

In all the code seeks to generate an RGB array which is written to the LED strip where;

  • The PV power being used by the house forms a green bar
  • Where not all the power is used, only the used power lights green but i place a green marker at the capability/PV output
  • Where there is insufficient PV, the battery supplies power, this is blue again with a blue capability marker
  • Finally, when the PV and battery are not enough, LEDs are red indicating grid power being used.

There is a broadcast UDP packet twice a second, and ive verified this on the network via Wireshark.

The loop is all quite quick (10s of ms) from the start, through the UDP receive and to the start of the array work, but that then takes over 1000ms to loop through but it can be slower. This is my first use of arrays and the programming was firstly aimed to get something working which could then be optimised.

I would appreciate assistance to review the code and see how this can be made more efficient;

/*
   Energy LED Pole

   R0 - initial version, got array reading and some processing of values
   R1 - corrected processing of values, eg
        50 0 0  > 0,5   6,6   6,6
        0 50 0  > 0,0   0,5   6,6
        0 0 50  > 0,0   0,0   0,5
        50 50 50> 0,5   6,11  11,16  ** < need to fix this, should be 12,17
        10000 10000 2000    > 0,137   138,143   143,144 ** same as above, but otherwise OK

  R2 - Added UDP receive to see how it gets on with real data.  Works with Neopixels quite well but...
        Need to blank unused pixels, need to address gaps between colours (may be same issue)

  R3 - Fixed above R1 and R2 issues - was overcomplicating it, no need to add gaps in array, eg
        0,137   137,143   143,144  does actually work correctly
       Changed binning

  R4 - Changed to array building whats getting output and added a final LED dimming function depending on power...
       not sure it was worth it!   Probably needs some gamma/log correction for proper effect and then exclude first counts where it doesnt light etc
       Spotted the zero mark actually used as 10W mark... can probably live with this.
       Need to understand slow update via UDP, possibly need to tidy up the loop - i.e. remove delay
       The graduations work well, TBC if the mixing is good or bad, and TBC if hiding a mark unless showing data is also TBC a keeper


  R5 - Changed to reading JSON from the Solis RS485 MODBUS interface

  R6 - Expand JSON poll to larger instruction set to improve reliability.
       Also split things up into loops rather than a single delay used for testing.
       Not working so well, back to UDP, HTTP server device struggling with too many requests possibly so UDP broadcast should ease the burden there

  R7 - Went back to R4 to remove all the JSON stuff which was too slow
       Now UDP receiving a smaller more regularly updating packet from the MODBUS server

  R7.1 Adding more LED strip information.
       Coincidentally working well with R7.1 of SolisCommes_SGr


*/


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//******************* LIBRARIES  ******************* //
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>


//******************* TIMERS  ******************* //

unsigned long currentMicros = 0;
unsigned long currentMillis = 0;

// program timer
int progTime = 0;
int timeEnd = 0;
int loopDelay = 1;  // delay in main loop to allow background processes
int counter = 0;


//2Hz / 500ms loop
unsigned long prevMillis2Hz = 0;
unsigned long millisPer2Hz = 500; // 200ms


//1Hz / 1000ms loop
unsigned long prevMillis1Hz = 0;
unsigned long millisPer1Hz = 1000; // 1000ms


//display timer / loop
unsigned long prevMillis20Hz = 0;
unsigned long millisPer20Hz = 50; // 50ms

unsigned long delaytime = 500;
unsigned long delayprint = 100;




//============ WiFi ===============
WiFiUDP lanUDP;
char packetBuffer[255];
unsigned int localPort = 1912;
unsigned long pktRx = 0; // Received Packet counter

const char *ssid = "private";
const char *password = "private";

IPAddress local_IP(192, 168, 1, 72);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);   //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

/////////////////////////////////////////////////////////
//**************** UDP RECEIVE DATA  ***************** //

#define UDP_PORT 1912

// UDP
WiFiUDP UDP;
char packet[255];
char reply[] = "Packet received!";


unsigned long pktTxUDP = 0; // Transmit Packet counter
unsigned long pktRxUDP = 0; // Receive Packet counter

/////////////////////////////////////////////////////////
//**************** LED Strip  ***************** //

#define PIN_NEO_PIXEL  4   // Arduino pin that connects to NeoPixel
#define NUM_PIXELS     144  // The number of LEDs (pixels) on NeoPixel

Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);

word liveArray[144][4];   //create array to write to strip  R  |  G  |  B  |  Brightness

word powerArray[144][4];

word identsArray[144][4] = {   //array for the idents/graduation markings

  0, 25, 25, 25,
  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, 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, 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, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 25, 25,
  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, 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, 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, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 25, 25, 25,
  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, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 25, 25,
  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, 0, 0, 0,
  0, 0, 0, 0,
  25, 0, 25, 25,
  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, 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, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 25, 25,
  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, 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, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  25, 0, 25, 25,
  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, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 25, 25,
  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, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 0, 25,
  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, 0, 0, 0,
  0, 0, 0, 0,
  25, 25, 25, 25,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,


};

// input received from UDP
int pvACpwrAct = 0;
int storACpwrAct = 0;
int storDCdir = 0;
int storDCsoc = 0;
int storgACpwrActImpExp = 0;
int storACpwrActChgDis = 0;
unsigned long fastNlivemillis = 0;

// results from the calculation
int powerHouse = 0;
int powerPV = 0;
int powerBattery = 0;
int powerImport = 0;
int powerExport = 0;





int powerBinPV[2]; // PV used by house bin led position from and to
int powerBinPVtot[2]; // PV total bin LED
int powerBinPVbatteryTot[2]; // PV + battery capability
int powerBinBattery[2]; // Battery bin led position from and to
int powerBinImport[2]; // Import bin led position from and to

int inverterCapacity = 2860;   // max power seen from the inverter

// array for the power bin for each LED, i.e. the first LED (0) is 0 to 10 W power
int nBins = 144;
int powerBin[] =
{
  0,
  10,
  20,
  30,
  40,
  50,
  60,
  70,
  80,
  90,
  100,
  110,
  120,
  130,
  140,
  150,
  160,
  170,
  180,
  190,
  200,
  210,
  220,
  230,
  240,
  250,
  260,
  270,
  280,
  290,
  300,
  310,
  320,
  330,
  340,
  350,
  360,
  370,
  380,
  390,
  400,
  410,
  420,
  430,
  440,
  450,
  460,
  470,
  480,
  490,
  500,
  525,
  550,
  575,
  600,
  625,
  650,
  675,
  700,
  725,
  750,
  775,
  800,
  825,
  850,
  875,
  900,
  925,
  950,
  975,
  1000,
  1100,
  1200,
  1300,
  1400,
  1500,
  1600,
  1700,
  1800,
  1900,
  2000,
  2100,
  2200,
  2300,
  2400,
  2500,
  2600,
  2700,
  2800,
  2900,
  3000,
  3100,
  3200,
  3300,
  3400,
  3500,
  3600,
  3700,
  3800,
  3900,
  4000,
  4100,
  4200,
  4300,
  4400,
  4500,
  4600,
  4700,
  4800,
  4900,
  5000,
  5250,
  5500,
  5750,
  6000,
  6250,
  6500,
  6750,
  7000,
  7250,
  7500,
  7750,
  8000,
  8250,
  8500,
  8750,
  9000,
  9250,
  9500,
  9750,
  10000,
  11000,
  12000,
  13000,
  14000,
  15000,
  16000,
  17000,
  18000,
  19000,
  20000,
  21000,
  22000,
  23000
};


void setup()
{
  /////////////////////////////////////////////////////////
  //******************* SERIAL SETUP ******************* //

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  delay(500); // let serial settle etc

  Serial.print("wdtEnable... > ");
  ESP.wdtEnable(5000);
  Serial.println("... done wdtEnable");

  // Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Begin listening to UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Listening on UDP port ");
  Serial.println(UDP_PORT);

  NeoPixel.begin();

  delay(1000);
}

void loop()
{
  delay(loopDelay);

  currentMillis = millis();  // use the same Millis for all timing each loop, i.e. dont end up with many Millisses, apart from...
  currentMicros = micros();  // where we use micros to do the program time
  progTime = currentMicros - timeEnd;  // will use the current micros at the end of the loop


  //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //******************* MAIN LOOP  ******************* //

  //Serial.print("loop; ");
  //Serial.println(micros() - currentMicros);  //10us
  // If packet received...
  int packetSize = UDP.parsePacket();
  if (packetSize)
  {

    int len = UDP.read(packet, 255);
    if (len > 0)
    {
      packet[len] = '\0';
    }
    //Serial.println(packet);
    pktRxUDP = pktRxUDP + 1;

    // = = = = = = fastNlive
    sscanf(packet, "fastNlive, %ld, %ld, %ld, %ld, %ld, %ld"
           , &fastNlivemillis
           , &pvACpwrAct
           , &storACpwrAct
           , &storDCdir
           , &storDCsoc
           , &storgACpwrActImpExp
          );
  }

  Serial.println(fastNlivemillis);
  //Serial.println(pktRxUDP);

  //Serial.print("sscanf; ");
  //Serial.println(micros() - currentMicros);

  //=============== TEST INJECTION OF RECEIVED VALUES ===============
  //pvACpwrAct = 745;
  //storACpwrAct = 122;
  //storDCdir = 0;
  //storSOC = 12;
  //storgACpwrActImpExp = 0;   // negative if importing


  //=============== CALCULATING RESULTS ===============
  // Use the battery direction to make an inverter active power value with direction
  if (storDCdir == 1)
  {
    storACpwrActChgDis = storACpwrAct * -1;   // negative if discharging
  }
  else
  {
    storACpwrActChgDis = storACpwrAct;
  }

  // Total load on the house
  powerHouse = pvACpwrAct + ((storgACpwrActImpExp + storACpwrActChgDis) * -1);
  if (powerHouse < 0)
  {
    powerHouse = 0;
  }

  // Battery power supplying the house
  powerBattery = (powerHouse - pvACpwrAct) + storgACpwrActImpExp;
  if (powerBattery < 0)
  {
    powerBattery = 0;
  }

  // PV supplying the house
  powerPV = pvACpwrAct;

  if (powerHouse < pvACpwrAct)
  {
    powerPV = powerHouse;
  }

  if (powerPV < 0)
  {
    powerPV = 0;
  }

  //Grid import supplying the house
  powerImport = (powerHouse - powerBattery) - powerPV;
  if (powerImport < 0)
  {
    powerImport = 0;
  }

  //Serial.print("calc done; ");
  //Serial.println(micros() - currentMicros);



  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //******************* 20Hz TIMER LOOP  ******************* //


  if ((currentMillis - prevMillis20Hz) >= millisPer20Hz)
  {
    prevMillis20Hz = currentMillis;
    //Serial.print("20Hz loop: ");
    //Serial.println(micros() - currentMicros);

    //===========================================
    //LED mapping to power=======================

    // Assess LED positions for PV power being used by the house
    for ( int j = 0; j < nBins; ++j ) //
    {
      if (powerBin[j] >= powerPV) // for each powerBin value, find if we have found the bin for the current power
      {
        powerBinPV[0] = 0; // as PV will always be used "first" it will start at zero
        powerBinPV[1] = j; // this is the upper bin number, could be zero eg if theres no generation
        break;
      }
    }

    // Assess LED positions for PV power total
    for ( int j = 0; j < nBins; ++j ) //
    {
      if (powerBin[j] >= pvACpwrAct) // for each powerBin value, find if we have found the bin for the current power
      {
        //powerBinPVtot[0] = 0; // as PV will always be used "first" it will start at zero
        powerBinPVtot[1] = j; // this is the upper bin number, could be zero eg if theres no generation
        break;
      }
    }

    // Assess LED positions for total capability PV + inverter rated power
    for ( int j = 0; j < nBins; ++j ) //
    {
      if (powerBin[j] >= (pvACpwrAct + inverterCapacity)) // for each powerBin value, find if we have found the bin for the current power
      {
        //powerBinPVtot[0] = 0; // as PV will always be used "first" it will start at zero
        powerBinPVbatteryTot[1] = j; // this is the upper bin number, could be zero eg if theres no generation
        break;
      }
    }

    // Assess LED positions for battery power
    powerBinBattery[0] = powerBinPV[1];

    for ( int j = 0; j < nBins; ++j ) //
    {
      if (powerBin[j] >= (powerPV + powerBattery) ) // for each powerBin value, find if we have found the bin for the current power
      {
        powerBinBattery[1] = j; // this is the upper bin number, could be zero eg if theres no battery or PV
        break;
      }
    }

    // Assess LED positions for grid import power
    powerBinImport[0] = powerBinBattery[1];

    for ( int j = 0; j < nBins; ++j ) //
    {
      if (powerBin[j] >= (powerPV + powerBattery + powerImport) ) // for each powerBin value, find if we have found the bin for the current power
      {
        powerBinImport[1] = j; // this is the upper bin number, could be zero eg if theres no battery or PV.. or no grid.
        break;
      }
    }

    // here we should know what numbers are being turned on for which power input.






    // need to do some sense checking before writing values.   Eg there is nothing to write if from and to are equal or from is bigger than to etc
    // Check on writing any PV LEDs
    int writePV = 0;
    if (powerBinPV[0] == powerBinPV[1])
    {
      writePV = 0;
    }

    else if (powerBinPV[0] >= powerBinPV[1])
    {
      writePV = 0;
    }

    else
    {
      writePV = 1;
    }

    // Check on writing any battery LEDs
    int writeBattery = 0;
    if (powerBinBattery[0] == powerBinBattery[1])
    {
      writeBattery = 0;
    }

    else if (powerBinBattery[0] >= powerBinBattery[1])
    {
      writeBattery = 0;
    }

    else
    {
      writeBattery = 1;
    }

    // Check on writing any grid import LEDs
    int writeImport = 0;
    if (powerBinImport[0] == powerBinImport[1])
    {
      writeImport = 0;
    }

    else if (powerBinImport[0] >= powerBinImport[1])
    {
      writeImport = 0;
    }

    else
    {
      writeImport = 1;
    }


    // Generate array for PV into the power Array   (sets colour and brightness)
    if (writePV == 1)
    {
      for (int pixel = powerBinPV[0]; pixel < powerBinPV[1]; pixel++)
      { // for each pixel
        powerArray[pixel][0] = 0; // red
        powerArray[pixel][1] = 128; // green
        powerArray[pixel][2] = 0; // blue
        powerArray[pixel][3] = 255; // brightness
      }
    }

    // Generate array for battery into the power Array   (sets colour and brightness)
    if (writeBattery == 1)
    {
      for (int pixel = powerBinBattery[0]; pixel < powerBinBattery[1]; pixel++)
      { // for each pixel
        powerArray[pixel][0] = 0; // red
        powerArray[pixel][1] = 0; // green
        powerArray[pixel][2] = 128; // blue
        powerArray[pixel][3] = 255; // brightness
      }
    }

    // Generate array for import into the power Array   (sets colour and brightness)
    if (writeImport == 1)
    {
      for (int pixel = powerBinImport[0]; pixel < powerBinImport[1]; pixel++)
      { // for each pixel
        powerArray[pixel][0] = 128; // red
        powerArray[pixel][1] = 0; // green
        powerArray[pixel][2] = 0; // blue
        powerArray[pixel][3] = 255; // brightness
      }
    }


    // Fade last pixel based on difference between last full bin and the total power
    int lastBinNo = powerBinImport[1];

    word totalPower = powerPV + powerBattery + powerImport;   // sum of all the power
    word lastBinValue = powerBin[lastBinNo];  // what is the wattage of the last bin which was lit (this is the intensity we need to reduce
    word powerBinStep = powerBin[lastBinNo + 1] - lastBinValue ; // The last lit pixel, whats its wattage step
    word powerBinDelta = totalPower - (lastBinValue - powerBinStep) ;

    word lastRed = powerArray[lastBinNo - 1][0];
    word lastGreen = powerArray[lastBinNo - 1][1];
    word lastBlue = powerArray[lastBinNo - 1][2];


    if (powerBinStep == 0)
    {
      powerBinStep = 1; //avoid devide by zero error
    }

    word partialRed = (powerBinDelta * lastRed) / powerBinStep;
    word partialGreen = (powerBinDelta * lastGreen) / powerBinStep;
    word partialBlue = (powerBinDelta * lastBlue) / powerBinStep;

    powerArray[lastBinNo - 1][0] = partialRed; // red
    powerArray[lastBinNo - 1][1] = partialGreen; // green
    powerArray[lastBinNo - 1][2] = partialBlue; // blue


    // Generate blanking of pixels which shouldnt be lit
    for (int pixel = lastBinNo; pixel < nBins; pixel++)
    { // for each pixel
      powerArray[pixel][0] = 0; // red
      powerArray[pixel][1] = 0; // green
      powerArray[pixel][2] = 0; // blue
      powerArray[pixel][3] = 0; // brightness
    }

    // Generate "maximal" markers for the available power from the PV (eg when house load is less than PV output)
    int totPVpixel = powerBinPVtot[1];
    powerArray[totPVpixel][0] = 8; // red
    powerArray[totPVpixel][1] = 64; // green
    powerArray[totPVpixel][2] = 0; // blue
    powerArray[totPVpixel][3] = 255; // brightness

    int totCapPixel = powerBinPVbatteryTot[1];
    powerArray[totCapPixel][0] = 8; // red
    powerArray[totCapPixel][1] = 0; // green
    powerArray[totCapPixel][2] = 64; // blue
    powerArray[totCapPixel][3] = 255; // brightness

    // Combine the ident markers and power array values to be what should be written to the pixels
    for (int pixel = 0; pixel < nBins; pixel++)
    {
      liveArray[pixel][0] = ( identsArray[pixel][0] + powerArray[pixel][0] ) / 2;
      liveArray[pixel][1] = ( identsArray[pixel][1] + powerArray[pixel][1] ) / 2;
      liveArray[pixel][2] = ( identsArray[pixel][2] + powerArray[pixel][2] ) / 2;
      liveArray[pixel][3] = ( identsArray[pixel][3] + powerArray[pixel][3] ) / 2;
    }
    //Serial.print("arrays done: ");
    //Serial.println(micros() - currentMicros);

    writePowerPixels(); //sub to write the liveArray



  }  //******************* END 20Hz TIMER LOOP  ******************* //
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Serial.print("end main: ");
    Serial.println(micros() - currentMicros);

}




void writePowerPixels()
{
  for (int pixel = 0; pixel < NeoPixel.numPixels(); pixel++)
  {
    word brightness = liveArray[pixel][3];

    word red = ( liveArray[pixel][0] * brightness ) / 255;
    word green = ( liveArray[pixel][1] * brightness ) / 255;
    word blue = ( liveArray[pixel][2] * brightness ) / 255;

    NeoPixel.setPixelColor(pixel, NeoPixel.Color(red, green, blue));

    NeoPixel.show();
  }
}
void writePowerPixels()
{
  for (int pixel = 0; pixel < NeoPixel.numPixels(); pixel++)
  {
    word brightness = liveArray[pixel][3];

    word red = ( liveArray[pixel][0] * brightness ) / 255;
    word green = ( liveArray[pixel][1] * brightness ) / 255;
    word blue = ( liveArray[pixel][2] * brightness ) / 255;

    NeoPixel.setPixelColor(pixel, NeoPixel.Color(red, green, blue));
  }
  NeoPixel.show();
}

Hi, @kolaha , i cant see any changes, are you suggesting that's where im having slow performance?

I may remove the brightness function as ive not used it to date, so the division may be an unnecessary processing delay?

Thanks,
Stuart

NeoPixel.show() is outside the for loop. There is a big difference between doing .show() once or .numPixels() times.

1 Like

Ah - yes, i see it now! Many thanks, i will try that just now and let you know how it goes.

Stuart

Amazing - what a great spot from you on the last line of the code. Simply moving that means the loop is sub 1ms, peaking perhaps 1.5ms so phenomenally better graphical performance.

I appreciate your input,

Stuart

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