Reasons for why my code runs so slowly? (arduino uno, neopixels, touch sensors)

Hello,

This code controls a device that consists of various interactive sensors and an array of 127 lights (Adafruit neopixels). The colours of each of the lights are controlled by someone interacting with the sensors. For example, when a person touches one of the sensors, all of the lights are dimmed. When they touch another, the saturation of the colours increases. The sensors are attached to the analogue in pins on the Arduino.

The lights each have a ‘data in’ and a ‘data out’ lead, and are wired in series, i.e. the Arduino pin that sends the instructions to the lights is connected to the data in lead of the first light, then the data out lead of the first light is connected to the data in lead of the second light and so on until the data out lead of the 126th light is connected to data in of the 127th. The instructions for the lights are 24bits per light, so the instruction length is 24 x 127 bits in total.

It takes something like one third of a second for a fresh set of instructions to be delivered to the lights, so the appearance when the sensors are touched is that the display jumps slowly from one set of values to another instead of progressing smoothly. Any suggestions for speeding up the code would be very much appreciated :slight_smile: !

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?

#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      127


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


int a1 = 0;int ana1;
int a2 = 1;int ana2;
int a3 = 2;int ana3;
int a4 = 3;int ana4;
int a5 = 4;int ana5;
int a6 = 5;int ana6;

float greencorrect=1;

byte r[127]={60,56,53,52,53,56,60,56,50,46,
             44,44,46,55,56,53,46,40,36,35,
             36,40,46,53,52,44,36,30,26,26,
             30,36,44,52,53,44,35,26,20,17,
             20,26,35,44,53,56,46,36,26,17,
             10,10,17,26,36,46,56,60,50,40,
             30,20,10,00,10,20,30,40,50,60,
             56,46,36,26,17,10,10,17,26,36,
             46,56,53,44,35,26,20,17,20,26,
             35,44,53,52,44,36,30,26,26,30,
             36,44,52,53,46,40,36,35,36,40,
             46,53,56,50,46,44,44,46,50,56,
             60,56,53,52,53,56,60};

int theta[127]= {300,291,289,270,259,249,240,231,240,251,
                 263,277,289,300,309,319,311,300,286,270,
                 254,240,229,221,210,217,226,240,259,281,
                 300,314,323,330,341,337,330,319,300,270,
                 240,221,210,203,199,189,191,194,199,210,
                 240,300,330,341,346,349,351,0,0,0,
                 0,0,0,0,180,180,180,180,180,180,
                 171,169,166,161,150,120,60,30,19,14,
                 11,9,19,23,30,41,60,90,120,139,
                 150,157,161,150,143,134,120,101,79,060,
                 46,37,30,41,49,60,74,90,106,120,
                 131,139,129,120,109,97,83,71,60,51,
                 60,69,79,90,101,111,120};


             
int bmax=75;
int red[127];
int green[127];
int blue[127];
float b0=30; float s0=1; float rb0=50; float gb0=50; float bb0=50;
float b=30; float s=1;float rb=50;float gb=50;float bb=50;
float R; float G; float B;
void setup() {
 
Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {


  for(int i=0;i<NUMPIXELS;i++){

if (theta[i]<120) 
{  
  red[i]=min(255,b*(1+constrain(s*r[i]/60,-2,1)*(3*(120-float(theta[i]))/120-1))+(rb-rb0)-(gb-gb0)/2-(bb-bb0)/2); 
 green[i]=min(255,b*(1+constrain(s*r[i]/60,-2,1)*(3*(float(theta[i]))/120-1))-(rb-rb0)/2+(gb-gb0)-(bb-bb0)/2);
  blue[i]=min(255,b*(1-constrain(r[i]*s/60,-2,1))-(rb-rb0)/2-(gb-gb0)/2+(bb-bb0)/2);

  }
if (theta[i]<240) {if (theta[i]>=120) {
  red[i]=min(255,b*(1-constrain(r[i]*s/60,-2,1))+(rb-rb0)-(gb-gb0)/2-(bb-bb0)/2); 
 green[i]=min(255,b*(1+constrain(s*r[i]/60,-2,1)*(3*(240-float(theta[i]))/120-1))-(rb-rb0)/2+(gb-gb0)-(bb-bb0)/2);
  blue[i]=min(255,b*(1+constrain(s*r[i]/60,-2,1)*(3*(float(theta[i])-120)/120-1))-(rb-rb0)/2-(gb-gb0)/2+(bb-bb0)/2);
}}

 if (theta[i]<=360) {if (theta[i]>=240) { 
  red[i]=min(255,b*(1+constrain(s*float(r[i])/60,-2,1)*(3*(float(theta[i])-240)/120-1))+(rb-rb0)-(gb-gb0)/2-(bb-bb0)/2); 
 green[i]=min(255,b*(1-constrain(r[i]*s/60,-2,1))-(rb-rb0)/2+(gb-gb0)-(bb-bb0)/2);
  blue[i]=min(255,b*(1+constrain(s*r[i]/60,-2, 1)*(3*(360-float(theta[i]))/120-1))-(rb-rb0)/2-(gb-gb0)/2+(bb-bb0)/2);
  
}} }
for(int i=0;i<NUMPIXELS;i++){
if(red[i]<0){red[i]=0;}
if(green[i]<0){green[i]=0;}
if(blue[i]<0){blue[i]=0;}
}
    for(int j=0;j<NUMPIXELS;j++){

    pixels.setPixelColor(j, pixels.Color(red[j],green[j],blue[j])); 
    pixels.show(); 
    

    }
    ana1=analogRead(a1);
    ana2=analogRead(a2);
    ana3=analogRead(a3);
    ana4=analogRead(a4);
    ana5=analogRead(a5);
    
    if(ana1<180){b=b*0.96;}
   if(ana1>350){b=b*1.04;}
if(ana1>=180){if(ana1<=350){b=b-0.065*(b-b0);}}
if(b>bmax){b=bmax;}
   if(ana2<180){s=s-0.2;}
   else
   {if(ana2>350){s=s+0.2;}
   else
    {s=s-0.15*(s-s0);}}
if(s>5){s=5;}
if(s<-5){s=-5;}
  
   if(ana3>350){rb=min(rb*1.2,rb0*2.7);}
   else
    {rb=rb-0.4*(rb-rb0);} 
    
    if(ana4>350){gb=min(gb*1.2,gb0*2.7);}
   else
    {gb=gb-0.4*(gb-gb0);}

    if(ana5>350){bb=min(bb*1.2,bb0*2.7);}
   else
    {bb=bb-0.4*(bb-bb0);}
}

Please put your code through the auto format tool in the IDE and repost.

Get rid of all the floats.

And while you are at it, change all of the ints that will never have a value larger than 255 into bytes

Generally, you go through the strip and set the color of each pixel. THEN, you show the results. That will be much faster than setting the color of a pixel and showing that change, repeating that for every pixel.