Sending mutiple sets of LDR data from Arduino to Max/MSP

Hi,

I'm quite new to Arduino but am currently working on a music project that uses brightness data from multiple LDRs, which then runs through a Max patcher to process sound.

I've previously tested this with a single LDR and it works perfectly, although I'm currently struggling with trying to send two different data sets when using two LDRs. I seem to be able to send two lists to Max, but when these are unpacked in Max, they are very unstable and fluctuate quite significantly despite no physical change in brightness. These two lists also seem to have some sort of mutual dependence (if I shade one of the LDRs, it also affects the data which is outputted by the other LDR, and vice versa). It would be great if anybody has any advice or guidance on how to send two independent sets of data from both LDRs. I have attached the current Arduino code below:

//setup our pins
const int led = 9; // define the pin the LED connects to (9~ is PWM)
const int sensorPin1 = A0; // define the input pin for LDR (analog in 0)
const int sensorPin2 = A1; // define the input pin for LDR (analog in 1)

//define our variables
int sensorValue = 0;
int sensorValue2 = 0; // variable to store the value coming from the LDR sensor
int brightness = 0;
int brightness2 = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

//SETUP elements of our system as it starts
void setup() {
Serial.begin(9600); //sets serial port for communication
pinMode(led, OUTPUT); // declare our LED pin (9~) to be an output:
}

//LOOP elements of our system while it runs
void loop() {
sensorValue = map(analogRead(sensorPin1),0,600,0,255);
sensorValue2 = map(analogRead(sensorPin2),0,600,0,255); // map the values to a useful range 0-255
brightness = constrain(sensorValue,0,255);
brightness2 = constrain(sensorValue2,0,255); // our brightness is limited to values between 0 and 255.

Serial.print(sensorValue, DEC);
Serial.print ("\t"); // separate tabs between values
Serial.println (sensorValue2, DEC); //prints the brightness value to the serial MONITOR (check your screen!) Max will read this with [serial port 9600]
delay(20); // wait 20 ms before starting the LOOP{} again!
}

Thanks,

Kieran

The constrain() calls do nothing after that map().

Code basically looks fine. The problem is therefore either in your wiring or this MAX thing. One thing you may try is to read the analog pins twice, discarding the first reading, this to prevent ghost readings:

brightness = analogRead(sensorPin1);
brightness = map(analogRead(sensorPin1),0,600,0,255);
brightness2 = analogRead(sensorPin2);
brightness2 = map(analogRead(sensorPin2),0,600,0,255);

As you want multiple LDRs, now would be a good time to start reading up on arrays and for loops. That'll help you make your code smaller and easier to write/maintain.

I've attached the wiring schematics as an image below, I don't think the issue lies here. I realise that the serial monitor within the Arduino program shows two different lists of data which are independent of each other. This would suggest then that there might be an issue with the Max patcher, which I've also attached below. This worked fine when I was using a single LDR, but I've not been successful since trying to unpack two sets of data, as the data then becomes very unstable and appears as though it splits a single set of data (therefore when I try to unpack the data, the two different sets seem to be dependent on each other).

The wiring looks correct - for as much as a Fritzing can show this. A proper schematic is a lot easier to read.

So the data you see on the Serial monitor is correct? Then the problem is with the receiver of the data - and I'm afraid you have to look for help on that thing elsewhere.