24 ldr in an arduino mega

Hi there!

I worked on a little project where I trigger midi signals when the light hitting a ldr goes down certain threshold. I am working with 16 ldr´s, each one connected to an Analog Input in an Arduino Mega.

I would love to start using 24 ldr´s but still one Arduino Mega.

I understand that multiplexing is the way to go and I would like to try it.

Could anyone guide me a bit in the process?

thanks!

below is the code of my 16 ldr´s project.

unsigned char status;
#define midichannel 0;

int threshold = 700;

boolean Key1LastState01 = false;
boolean Key1LastState02 = false;
boolean Key1LastState03 = false;
boolean Key1LastState04 = false;
boolean Key1LastState05 = false;
boolean Key1LastState06 = false;
boolean Key1LastState07 = false;
boolean Key1LastState08 = false;
boolean Key1LastState09 = false;
boolean Key1LastState10 = false;
boolean Key1LastState11 = false;
boolean Key1LastState12 = false;
boolean Key1LastState13 = false;
boolean Key1LastState14 = false;
boolean Key1LastState15 = false;
boolean Key1LastState16 = false;

int sensorValue01;
int sensorValue02;
int sensorValue03;
int sensorValue04;
int sensorValue05;
int sensorValue06;
int sensorValue07;
int sensorValue08;
int sensorValue09;
int sensorValue10;
int sensorValue11;
int sensorValue12;
int sensorValue13;
int sensorValue14;
int sensorValue15;
int sensorValue16;

int sensornote01 = 55;
int sensornote02 = 54;
int sensornote03 = 53;
int sensornote04 = 52;
int sensornote05 = 51;
int sensornote06 = 50;
int sensornote07 = 49;
int sensornote08 = 48;
int sensornote09 = 47;
int sensornote10 = 46;
int sensornote11 = 45;
int sensornote12 = 44;
int sensornote13 = 43;
int sensornote14 = 42;
int sensornote15 = 41;
int sensornote16 = 40;

void setup() {
 Serial.begin(57600);
}

void loop() {
   
 sensorValue01 = analogRead(A0);
 sensorValue02 = analogRead(A1);
 sensorValue03 = analogRead(A2);
 sensorValue04 = analogRead(A3);
 sensorValue05 = analogRead(A4);
 sensorValue06 = analogRead(A5);
 sensorValue07 = analogRead(A6);
 sensorValue08 = analogRead(A7);
 sensorValue09 = analogRead(A8);
 sensorValue10 = analogRead(A9);
 sensorValue11 = analogRead(A10);
 sensorValue12 = analogRead(A11);
 sensorValue13 = analogRead(A12);
 sensorValue14 = analogRead(A13);
 sensorValue15 = analogRead(A14);
 sensorValue16 = analogRead(A15);

//SENSOR 01---------------------------------------------------                            
 if (sensorValue01 > threshold && Key1LastState01 == false) {
 MIDI_TX(128,sensornote01,127);
 Key1LastState01 = true;
 }
 else if (sensorValue01 < threshold && Key1LastState01 == true) {
 MIDI_TX(144,sensornote01,127);
 Key1LastState01 = false;
 }
//SENSOR 02---------------------------------------------------
 if (sensorValue02 > threshold && Key1LastState02 == false) {
 MIDI_TX(128,sensornote02,127);
 Key1LastState02 = true;
 }
 else if (sensorValue02 < threshold && Key1LastState02 == true) {
 MIDI_TX(144,sensornote02,127);
 Key1LastState02 = false;
 }
//SENSOR 03---------------------------------------------------
 if (sensorValue03 > threshold && Key1LastState03 == false) {
 MIDI_TX(128,sensornote03,127);
 Key1LastState03 = true;
 }
 else if (sensorValue03 < threshold && Key1LastState03 == true) {
 MIDI_TX(144,sensornote03,127);
 Key1LastState03 = false;
 }
//SENSOR 04---------------------------------------------------
 if (sensorValue04 > threshold && Key1LastState04 == false) {
 MIDI_TX(128,sensornote04,127);
 Key1LastState04 = true;
 }
 else if (sensorValue04 < threshold && Key1LastState04 == true) {
 MIDI_TX(144,sensornote04,127);
 Key1LastState04 = false;
 }  
//SENSOR 05---------------------------------------------------
 if (sensorValue05 > threshold && Key1LastState05 == false) {
 MIDI_TX(128,sensornote05,127);
 Key1LastState05 = true;
 }
 else if (sensorValue05 < threshold && Key1LastState05 == true) {
 MIDI_TX(144,sensornote05,127);
 Key1LastState05 = false;
 }    
//SENSOR 06---------------------------------------------------
 if (sensorValue06 > threshold && Key1LastState06 == false) {
 MIDI_TX(128,sensornote06,127);
 Key1LastState06 = true;
 }
 else if (sensorValue06 < threshold && Key1LastState06 == true) {
 MIDI_TX(144,sensornote06,127);
 Key1LastState06 = false;
 }      
//SENSOR 07---------------------------------------------------
 if (sensorValue07 > threshold && Key1LastState07 == false) {
 MIDI_TX(128,sensornote07,127);
 Key1LastState07 = true;
 }
 else if (sensorValue07 < threshold && Key1LastState07 == true) {
 MIDI_TX(144,sensornote07,127);
 Key1LastState07 = false;
 }  
//SENSOR 08---------------------------------------------------
 if (sensorValue08 > threshold && Key1LastState08 == false) {
 MIDI_TX(128,sensornote08,127);
 Key1LastState08 = true;
 }
 else if (sensorValue08 < threshold && Key1LastState08 == true) {
 MIDI_TX(144,sensornote08,127);
 Key1LastState08 = false;
 }      
//SENSOR 09---------------------------------------------------
 if (sensorValue09 > threshold && Key1LastState09 == false) {
 MIDI_TX(128,sensornote09,127);
 Key1LastState09 = true;
 }
 else if (sensorValue09 < threshold && Key1LastState09 == true) {
 MIDI_TX(144,sensornote09,127);
 Key1LastState09 = false;
 }
//SENSOR 10---------------------------------------------------
 if (sensorValue10 > threshold && Key1LastState10 == false) {
 MIDI_TX(128,sensornote10,127);
 Key1LastState10 = true;
 }
 else if (sensorValue10 < threshold && Key1LastState10 == true) {
 MIDI_TX(144,sensornote10,127);
 Key1LastState10 = false;
 }      
//SENSOR 11---------------------------------------------------
 if (sensorValue11 > threshold && Key1LastState11 == false) {
 MIDI_TX(128,sensornote11,127);
 Key1LastState11 = true;
 }
 else if (sensorValue11 < threshold && Key1LastState11 == true) {
 MIDI_TX(144,sensornote11,127);
 Key1LastState11 = false;
 }     
//SENSOR 12---------------------------------------------------
 if (sensorValue12 > threshold && Key1LastState12 == false) {
 MIDI_TX(128,sensornote12,127);
 Key1LastState12 = true;
 }
 else if (sensorValue12 < threshold && Key1LastState12 == true) {
 MIDI_TX(144,sensornote12,127);
 Key1LastState12 = false;
 }      
//SENSOR 13---------------------------------------------------
 if (sensorValue13 > threshold && Key1LastState13 == false) {
 MIDI_TX(128,sensornote13,127);
 Key1LastState13 = true;
 }
 else if (sensorValue13 < threshold && Key1LastState13 == true) {
 MIDI_TX(144,sensornote13,127);
 Key1LastState13 = false;
 }     
//SENSOR 14---------------------------------------------------
 if (sensorValue14 > threshold && Key1LastState14 == false) {
 MIDI_TX(128,sensornote14,127);
 Key1LastState14 = true;
 }
 else if (sensorValue14 < threshold && Key1LastState14 == true) {
 MIDI_TX(144,sensornote14,127);
 Key1LastState14 = false;
 }      
//SENSOR 15---------------------------------------------------
 if (sensorValue15 > threshold && Key1LastState15 == false) {
 MIDI_TX(128,sensornote15,127);
 Key1LastState15 = true;
 }
 else if (sensorValue15 < threshold && Key1LastState15 == true) {
 MIDI_TX(144,sensornote15,127);
 Key1LastState15 = false;
 }      
//SENSOR 16---------------------------------------------------
 if (sensorValue16 > threshold && Key1LastState16 == false) {
 MIDI_TX(128,sensornote16,127);
 Key1LastState16 = true;
 }
 else if (sensorValue16 < threshold && Key1LastState16 == true) {
 MIDI_TX(144,sensornote16,127);
 Key1LastState16 = false;
 }      
}


 
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
 status = MESSAGE + midichannel;
 Serial.write(status);
 Serial.write(PITCH);
 Serial.write(VELOCITY);
}

Hi and welcome,

A quick search in the Arduino Playground can help find ideas and solutions for questions like this. For example here is a page about the 4051 analog multiplexer chip.

Here is another option, the mcp3208 chip.

By the way, your sketch is almost 16 times longer than it needs to be. Learn about for() loops and arrays to find neat ways to really shrink it down.

Paul

This project shows you how to multiplex inputs.
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html

However you desperately need to learn how to program better than that before trying it.