# ldr to fader in DAW

Thank’s in advance for your time, I’m a relative beginner with this, and this is my first post.

I am trying to get 6 ldrs to control six pots/faders within a chosen DAW.

I’m working with 1 ldr for now with an uno, with a 1k resistor.

With the simple code below I know that everything is working in the hardware, as I can view numbers relating to light in the serial monitor.

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

void loop() {

delay(100);
}
``````

I have a second code I am using (which I’ll show under this paragraph), which runs through the SM and triggers in the DAW the midi value I specify in the top line - it works which is great.

What I am trying to find an answer to though, is that I’d like the numbers not to be on/off, but incrementally rise as the light intensity goes up. And I am at a loss with how to achieve that - the code below seems to trigger within an incredibly tight band of light intensity. Will the code I’m using be a good starting point, or should I look elsewhere? Apologies if I’m asking a silly question, and please let me know if I need to provide more details or am not being clear. Here’s the code I’m using.

``````unsigned char PadNote[40] = {21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60};         // MIDI notes from 0 to 127 (Mid C = 60)
// Imagine a piano roll. 45 55 are keys located on the middle for example.
// Tweak it to what sounds better to you.

int CutOff = 40;   // This is the minimal amount of analog reading before triggering the note!

int MaxPlayTime[6] = {6500,6500,6500,6500};               // Cycles before a 2nd hit is allowed
// Best value after lots of trial and error.

#define  midichannel	0;                              // MIDI channel from 0 to 15 (+1 in "real world")

//*******************************************************************************************************************
// Internal Use Variables
//
// You should tweak here if it's not optimal yet.
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Those values work perfectly when using the small LDR and 1K Resistor. So used it!
//
//*******************************************************************************************************************

boolean activePad[6] = {0,0,0,0};                   // Array of flags of pad currently playing
int PinPlayTime[6] = {0,0,0,0};                     // Counter since pad started to play

int PinPauseTimeDelay = 200;
int PinPauseTime[4] = {0,0,0,0};
int PinAvailable[4] = {1,1,1,1};

unsigned char status;

int pin = 0;
int hitavg = 0;
int debug = false;
int noteOffset = 0;

//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************

void setup()
{
Serial.begin(57600);                                  // connect to the serial port 57600
}

//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************

void loop()
{
for(int pin=0; pin <= 3; pin++)
{

if(debug)
Serial.println(hitavg);

if(hitavg < CutOff)   {
if(debug)Serial.println("hitavg < CutOff");
if(PinAvailable[pin] == true)
{
if(debug) Serial.println("PinAvailable[pin] == true");
// Toca a nota full
PinPauseTime[pin] = 0;

if(debug)   Serial.println("NoteON");
}
}
}

PinPlayTime[pin] += 1;

{
PinAvailable[pin] = 2;
if(debug)
Serial.println("NoteOFF, available is now false. active is false;");

PinPlayTime[pin] = 0;

}
}

if(PinAvailable[pin] == 2 && PinPauseTime[pin] > PinPauseTimeDelay) {
if(debug)Serial.println("PinAvailable[pin] == false && PinPauseTime[pin] > 10");
PinAvailable[pin] = true;
} else if(PinAvailable[pin] == 2) {
if(debug)Serial.println("PinPauseTime[pin] += 1;");
PinPauseTime[pin] += 1;
}

if(debug)delay(1000);
}
}

//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(uint8_t MESSAGE, uint8_t PITCH, uint8_t VELOCITY)
{
//status = MESSAGE + midichannel;
//status = MESSAGE + pin;
digitalWrite(13,HIGH);
Serial.write(MESSAGE);
Serial.write(PITCH);
Serial.write(VELOCITY);
delay(30);
digitalWrite(13,LOW);
}
``````

I’m not completely comprehending your code, but I think you need the [u]map() function[/u]. The example shows how to take the 0-1023 data from the ADC and convert it to 0-255.

IIRC, MIDI standard values should be between 0-127, so you might need to use that range. And, if your input range is not covering the full 0-1023 range, you may need to tweak that too. And, you may need some other if-statements, etc., to otherwise constrain or massage the values.

For example, you can take the LDR reading from the ADC and and convert it to a valid PITCH or VELOCITY value for your MIDI message.