Hi, everyone I wanted to connect 2 photoresistors and creat 2 rectangles which reflects lighting on that photoresistors seperatly.
1 st question is is it even possible 'cause i couldn’t find anything about it on the internet
That is what i tryed (I just created another object from Serial class), but it says port1 = new Serial(this, "COM3", 9600);
on this line that “COM3” is busy
There is my whole code
2 nd question is: if it is possible to connect and read 2 ports what i did wrong
thank you
// Read data from the serial port and assign it to a variable. Set the fill a
// rectangle on the screen using the value read from a light sensor connected
// to the Wiring or Arduino board
import processing.serial.*;
Serial port; // Create object from Serial class
Serial port1;
int val; // Data received from the serial port
int val1;
void setup() {
size(400, 400);
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, "COM3", 9600);
port1 = new Serial(this, "COM3", 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
if (0 < port1.available()) { // If data is available to read,
val1 = port1.read(); // read it and store it in val
}
fill(val1); // Set fill color with the value read
rect(200, 50, 100, 100);
} ```
Regarding your first question, yes it is possible. You either send the two values on the same Serial port or you use two Serial Ports each sending the data from one photoresistor. It depends on your electronic setup.
If you choose to send the two photoresistors state over the same Serial port, you can then, split the information and assign them to your variables.
This is a quick example that I have found in a processing forum to illustrate.
(Receive Multiple Sensor Information from Arduino to Processing - Processing 2.x and 3.x Forum)
There are multiple ways to do send the data. So, take the time to investigate as well
Since your values go from 0 to 255 (I am guessing that you are using analogRead), then you can send one byte per photoresist (byte / Reference / Processing.org), or as Strings in the example above.
If you choose to connect two serial ports, then you will have to identify which port is sending the values. So, you will need to create two Serial Port Objects, assign the Ports and baud rate, and on the Serial Event identify which one is sending data.
Something like this,
import processing.serial.*;
Serial portOne; // Create object from Serial class
Serial portTwo;
void setup() {
size(400, 400);
//Make sure that you are not assign the same COM port to the two Serial objects. Also, the baud rate should be the same!
portOne = new Serial(this, "COM3", 9600);
portTwo = new Serial(this, "COM4", 9600);
}
void serialEvent(Serial thisPort) {
if(thisPort==portOne){
//...GOOD NEWS: data from serial 1
}
if(thisPort==portTwo){
//...GOOD NEWS: data from serial 2
}
}
Once again, take the time to look for resources
Below are some links that I think will be helpful to you in the future
It is possible and there are numerous resources out there on the Internet and in this forum.
One of the best tools in a programmer’s tool chest is knowing the resources available to you and learning to navigate, filter, and use them.
A short list of resources to peruse:
Resources < Click here to expand !
Explore the resources available here:
The Processing website has references, examples, tutorials, links to resources, etc. https://processing.org/
The Processing PDE (IDE) has lots to offer!
An exploration of the menu bar will reveal what is available; libraries, tools, local examples, and other goodies are in there.
Thank you a lot for responding and giving me a valuable suggestions, i will try them on. Also I have only one Arduino is sending info throught two ports good ? This is my electronic setup: https://www.arduino.cc/en/uploads/Tutorial/PhotoCellA0.png
Which part of the solution above was a solution for you?
It will help others that are searching the forum to know.
Please send a quick response with a reference to what was a solution for you.
I ask only because you have questions about two ports.
Can you please elaborate? It helps to understand what you want to do with the additional port.
Which Arduino are you using?
I typically use the USB serial port and add additional serial ports (see details below) as required; usually for testing or debugging so I can also use the serial monitor AND send data (two different ports each with its own purpose).
My Arduino Mega 2560 R3 has 6 pins for 3 additional hardware serial connections.
The Arduino Uno DOES NOT have additional hardware serial pins and you will have to implement a SoftwareSerial (< link!) with digital pins if you choose this path.
You will also require a USB to serial adapter for the additional serial pins if connecting to PC: https://www.pololu.com/product/391 Just one example of this.
I wanted to ask is it available to conntect 2 port to 1 arduino, about solution one i will do it quite later and send you both link (hopefully if it work properly)
With respect Mirzohid.