Multiplexed LDR inputs controlling individual RGB LEDs on a strip

Hi all,

First time post, so go easy! My background is mainly in design, so the programming aspect of this type of work is my biggest problem.

I'm working on a project using bunch of LDRs (8 at the moment, 60 in the final version) wired into a 4051 analog multiplexer. I'm trying to pair the LDRs/Channels to an LED strip (this one: Adafruit NeoPixel Digital RGB LED Strip - White 60 LED [WHITE] : ID 1138 : $99.80 : Adafruit Industries, Unique & fun DIY electronics and kits with this library: GitHub - adafruit/Adafruit_NeoPixel: Arduino library for controlling single-wire LED pixels (NeoPixel, WS2812, etc.)) so that if LDR 0 is covered then LED 0 turns on, if LDR 1 is covered LED1 turns on etc. There are also 3 pots that control the RGB values and I'm going to use previous and current colour values to allow LED's to turn on and stay one colour while the RGB values are altered.

This is an expansion on a project using 2 RGB LEDs and 2 LDRs, which was pretty easy to make work - the code was:

//Sketch to determine colour of analog RGB LEDS from 3 analog values and then choose an LED determined by an LDR input

//Declarations
//Potentiometers 

const int redpotPin = A0;
const int greenpotPin = A1;
const int bluepotPin = A2;

const boolean invert = true;

int currentColourValueRed1;    //Current loop value
int currentColourValueGreen1;
int currentColourValueBlue1;

int previousColourValueRed1;     //Previous loop valuw   
int previousColourValueGreen1;
int previousColourValueBlue1;

int currentColourValueRed2;    //Current loop value
int currentColourValueGreen2;
int currentColourValueBlue2;

int previousColourValueRed2;     //Previous loop valuw   
int previousColourValueGreen2;
int previousColourValueBlue2;

//Switch for testing

const int switchPin1 = 2;
const int switchPin2 = 4;

//LDR tests declarations

int ldrVal1 = 0;
int ldrVal2 = 0;

const int ldrPin1 = A4;
const int ldrPin2 = A5;


int currentSwitch1state = 0;
int previousSwitch1state = 0;

int currentSwitch2state = 0;
int previousSwitch2state = 0;


//LEDs

const int redLed1 = 3;
const int greenLed1 = 5;
const int blueLed1 = 6;

const int redLed2 = 9;
const int greenLed2 = 10;
const int blueLed2 = 11;


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

  pinMode (switchPin1, INPUT);
  pinMode (switchPin2, INPUT);
   
  
  pinMode (redLed1, OUTPUT);
  pinMode (greenLed1, OUTPUT);
  pinMode (blueLed1, OUTPUT);
  
  pinMode (redLed2, OUTPUT);
  pinMode (greenLed2, OUTPUT);
  pinMode (blueLed2, OUTPUT); 
  
 
}

void loop(){
  
 ldrVal1 = analogRead (ldrPin1);
 ldrVal2 = analogRead (ldrPin2);
 
 //Serial.println('LDR 1 = ');
 //Serial.println (ldrVal1);
 Serial.println('LDR 2 = ');
 Serial.println (ldrVal2);
 
 
 if (ldrVal1 <= 200){ //Switch pressed
   
   analogWrite(redLed1, previousColourValueRed1);          //Write the previous values to the LED
   analogWrite(greenLed1, previousColourValueGreen1);
   analogWrite(blueLed1, previousColourValueBlue1);
   
  }
 
 if (ldrVal1 >= 201){        //  If the switch not pressed
 
   currentColourValueRed1 = map ( analogRead(redpotPin), 0, 1023, 0, 255);     //Read analog in and map to LED brightness of each colour
   currentColourValueGreen1 = map ( analogRead(greenpotPin), 0, 1023, 0, 255);
   currentColourValueBlue1 = map ( analogRead(bluepotPin), 0, 1023, 0, 255);
    
      pinMode (redLed1, LOW);
      pinMode (greenLed1, LOW);
      pinMode (blueLed1, LOW); 

         
 } 
 
 if (ldrVal2  <= 350){ //Switch pressed
   
   analogWrite(redLed2, previousColourValueRed2);          //Write the previous values to the LED
   analogWrite(greenLed2, previousColourValueGreen2);
   analogWrite(blueLed2, previousColourValueBlue2);
   
  }
 
 if (ldrVal2 >= 351){        //  If the switch not pressed
 
   currentColourValueRed2 = map ( analogRead(redpotPin), 0, 1023, 0, 255);     //Read analog in and map to LED brightness of each colour
   currentColourValueGreen2 = map ( analogRead(greenpotPin), 0, 1023, 0, 255);
   currentColourValueBlue2 = map ( analogRead(bluepotPin), 0, 1023, 0, 255);
    
      pinMode (redLed2, LOW);
      pinMode (greenLed2, LOW);
      pinMode (blueLed2, LOW); 

         
 } 
  
  
   previousColourValueRed1 = currentColourValueRed1;
   previousColourValueGreen1 = currentColourValueGreen1;
   previousColourValueBlue1 = currentColourValueBlue1;
      
   
   previousColourValueRed2 = currentColourValueRed2;
   previousColourValueGreen2 = currentColourValueGreen2;
   previousColourValueBlue2 = currentColourValueBlue2;   
      
  
 //FOR DEBUGGING  
 /* Serial.println ("Current Red = ");
  Serial.println (currentColourValueRed);
  Serial.println ("Current Green = ");
  Serial.println (currentColourValueGreen);
  Serial.println ("Current Blue = ");
  Serial.println (currentColourValueBlue);
  
  Serial.println ("Previous Red = ");
  Serial.println (previousColourValueRed);
  Serial.println ("Previous Green = ");
  Serial.println (previousColourValueGreen);
  Serial.println ("Previous Blue = ");
  Serial.println (previousColourValueBlue);
  
  
  Serial.println ("Current switch = ");
  Serial.println (currentSwitchstate);
  Serial.println ("Previous switch = ");
  Serial.println (previousSwitchstate);
 */
  delay (100);
  
}

The complexity of using so many LEDs and LDRs read through a multiplex has baffled me a little. So far I've cut and shut the LED strip code and the Arduino Cookbook's 4051 code, and while I have the 8 LED's turning on when the light levels go below the threshold I can't work out how to identify the values from specific channels of the 4051 and use these to determine which LED to turn on or off.

// Code adapted from Jenna deBoisblanc and Adafruit
//
#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 6, NEO_GRB + NEO_KHZ800);

// array of pins used to select 1 of 8 inputs on multiplexer
const int select[] = {2,3,4}; // 0, 1, 2 pins connected to the 4051 input select lines
const int analogPin = 5;      // the analog pin connected to multiplexer output

int r0 = 0;      //value of select pin at the 4051 (s0)
int r1 = 0;      //value of select pin at the 4051 (s1)
int r2 = 0;      //value of select pin at the 4051 (s2)
int channel = 0;   //which y pin we are selecting



//Potentiometers for test
const int potPin1 = A0;
const int potPin2 = A1;
const int potPin3 = A2;

int potpin1Value = 0;
int potpin2Value = 0;
int potpin3Value = 0;

//Colour Values - to be replaced with previous and current
int redVal = 0;
int greenVal = 0;
int blueVal = 0;

//*****************************
//FUNCTIONS
//*****************************

int getValue( int channel)
{
   // set the selector pins HIGH and LOW to match the binary value of channel
   for(int bit = 0; bit < 3; bit++)
   {
      int pin = select[bit]; // the pin wired to the multiplexer select bit
      int isBitSet =  bitRead(channel, bit); // true if given bit set in channel
      digitalWrite(pin, isBitSet);
   }
   return analogRead(analogPin);    
  }
  
//*************************
//SETUP
//*************************

void setup() {
  
  strip.begin(); //Strip Startup
  strip.show(); // Initialize all pixels to 'off'
  
  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2

  Serial.begin(9600);
}

//*****************************
//LOOP
//******************************

void loop() {
  
    
     
  for(int channel = 0; channel < 8; channel++)
  {
     int value = getValue(channel);
   
     Serial.print("Channel ");
     Serial.print(channel);
     Serial.print(" = ");
     Serial.println(value);
     
  if ( value <= 220)
  
  
    //Read and map analog colour Values
  { int potpin1Value = analogRead (potPin1);
    int potpin2Value = analogRead (potPin2);
    int potpin3Value = analogRead (potPin3); 
    
    redVal = map(potpin1Value, 0, 1023, 0, 255);
    greenVal = map(potpin2Value, 0, 1023, 0, 255);
    blueVal = map(potpin3Value, 0, 1023, 0, 255);
  
    strip.setPixelColor (0, redVal, greenVal, blueVal);
    strip.setPixelColor (1, redVal, greenVal, blueVal);
    strip.setPixelColor (2, redVal, greenVal, blueVal);
    strip.setPixelColor (3, redVal, greenVal, blueVal);
    strip.setPixelColor (4, redVal, greenVal, blueVal);
    strip.setPixelColor (5, redVal, greenVal, blueVal);
    strip.setPixelColor (6, redVal, greenVal, blueVal);
    strip.setPixelColor (7, redVal, greenVal, blueVal);
  
   // delay(100);
  }
  
  else {
     strip.show();
     strip.setPixelColor (0, 0, 0, 0);
     strip.setPixelColor (1, 0, 0, 0);
     strip.setPixelColor (2, 0, 0, 0);
     strip.setPixelColor (3, 0, 0, 0);
     strip.setPixelColor (4, 0, 0, 0);
     strip.setPixelColor (5,  0, 0, 0);
     strip.setPixelColor (6, 0, 0, 0);
     strip.setPixelColor (7, 0, 0, 0);
 //   delay(100);
  }
  
}

}

I realise there's probably an elegant solution to this, but as a relative newcomer to programming I'm stumped! Any advice on this would be great.

Cheers,