How to connect and "read" two light sensors (photoresistors)

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

There is original code only for 1 photoresistor which worked just perfectly:
Example 2B: Light sensor (Processing)
(from https://processing.org/tutorials/electronics/ )

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); 
} ```
1 Like

Hi @Mirzohid

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 :slight_smile:
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 :slight_smile:
Below are some links that I think will be helpful to you in the future

Handshake Protocol

(Connecting Arduino to Processing - SparkFun Learn)

Split

split() / Reference / Processing.org
https://www.youtube.com/watch?v=PjxbuSnj8Pk

Best regards!

1 Like

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:

:)

2 Likes

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

Alrigth, i appreciate ur recommendation and keep in mind what you said. Thank you.

Hello,

Serial communications is easy* for me but can be daunting for the uninitiated and requires a learning curve.

Easy* from experience on other platforms but I had to adapt to understanding the Java implementation.

The Processing tutorial gives the simplest of examples only for only a byte.

Do a search in Google with a few appropriate keywords and an answer will be forthcoming. Search for:

Processing Arduino serial

and filter through results.

Even a search in this forum provides some examples.

Do some exploration and come back when you have done some research.

Don’t assume everything will always work out of the box for your hardware or setup…

I had to “adapt” code from the Processing examples to work with my hardware:
https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/5

We are here to help when you hit a wall.

:)

Ok, I will do it :smile:

Hello,

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 use this one:
https://www.pololu.com/product/3172The programmer also features a TTL-level serial port.
https://www.pololu.com/docs/0J36/6

:)

1 Like

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.

I answered that already.

:)

1 Like

Well, i use a arduino usb " USB 2.0 CABLE TYPE A/B" and arduino UNO. Also I am kinda newbie in arduino.

So, I think @glv as already answered.
If you need further help post here or in the arduino forum :slight_smile:
(https://forum.arduino.cc/)

2 Likes

@glv hello, I said that I will send you link that answer of question. Well, if you interested there is link to my project: https://www.youtube.com/watch?v=THG-7JXbuJM

1 Like