Speeding up simple code

Hi all,

I wrote some simple code for a project to control an Neopixel RGB led panel. It works great but there is a delay when changing colors. Is there an easy way to speed this up so that it will react faster? Any suggestions would be great!

#include <Adafruit_NeoPixel.h>

#define PIN            7 //neopixel signal pin
#define NUMPIXELS      64 //number of neopixels

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

unsigned long ch1;  //Channel input variable
unsigned long ch2;
unsigned long ch3;

void setup(){
  pinMode(4, INPUT);//Setting put pin4 as a input
  pinMode(5, INPUT);//Setting put pin5 as a input
  pinMode(6, INPUT);//Setting put pin6 as a input
  pixels.begin(); // This initializes the NeoPixel library
}

  void loop(){
  ch1 = pulseIn(4, HIGH); // Read the pulse width of channel 1
  ch2 = pulseIn(5, HIGH); // Read the pulse width of channel 2
  ch3 = pulseIn(6, HIGH); // Read the pulse width of channel 3
  
  int Red = map(ch1, 0, 2500, 0, 255);
  int Green = map(ch2, 0, 2500, 0, 255);
  int Blue = map(ch3, 0, 2500, 0, 255);
  
  for(int i=0;i<NUMPIXELS;i++){
      pixels.setPixelColor(i,Red, Green, Blue);
      }
    pixels.show();
 }

Is there an easy way to speed this up so that it will react faster?

Yes don’t use the pulseIn function, it looks like this is timing out and causing a delay.

How are these pulses derived?
Make sure the inputs are connected to pulses.
What period are you expecting each pulse to be?

pulseIn has a default timeout of 1 second, but you can change it easily :

pulseIn(pin, value, timeout)
timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long)

I beleive that using a timeout of up to 25000 µs should work (but this depends on what is plugged to the input pins) (it's a wild guess)