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
}