Read two separate sensor value

I have two light sensor setup to my Arduino Uno, and I want to send the value of two sensors to processing and then separately assign it to val1 and val2 and show it in window. But in my Processing code it is only design on getting 1 value(val1) from one light sensor, is there anyone can help me to take the second value and set it in my val2 in my processing code. Thank you so much…

Arduino code:

int val1;
int val2;
int inputPin1 = A0; // Set the input to analog in pin A0
int inputPin2 = A1; // Set the input to analog in pin A1
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
val1 = analogRead(inputPin1)/4; // Read analog input pin, put in range 0 to 255
val2 = analogRead(inputPin1)/4; // Read analog input pin, put in range 0 to 255
Serial.write(val1); // Send the value1
Serial.write(val2); // Send the value2
delay(100); // Wait 100ms for next reading
}

Processing Code:

import processing.serial.*;

Serial port; // Create object from Serial class
int val1; // Data received from the serial port

void setup() {
size(200, 200);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, 9600);
}

void draw() {
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
}
background(204); // Clear background
fill(val); // Set fill color with the value read
rect(50, 50, 100, 100); // Draw square
}

Please format your code above.

Have a look at these links:

SendingMultipleData \ Learning \ Wiring
Receive Multiple Sensor Information from Arduino to Processing - Processing 2.x and 3.x Forum

Reading multiple sensors is common. You need to implement a first design based on examples that are readily available in previous posts or in the web.

looking at your ino code, you could do:

Serial.write(val1); // Send the value1
Serial.write(',');   //Need to check this or used the int equivalent
Serial.write(val2); // Send the value2
Serial.write(';'); 

So the data that you stream will look like this: val1,val2;. In other words, your values are separated by a ‘,’ char and your data stream ends with a ‘;’ character. In Processing, you read your serial buffer until the ‘;’ character and THEN your processed your data, splitting it at the ‘,’ and ‘;’ character and extracting val1 and val2.

Kf

1 Like

@jbalbarez if you need to make a graph with that data, i would add a timestamp to the stream that @kfrajer told you, adding the value of the “millis()” function of your arduino, so you can make a way more acurrate graph