Serial communications on two ports

I am new to processing software and Arduino too. I would very much appreciate if any of you can guide me on below project:

Arduino Uno board connected to com port 7 of computer. Using two pins 9 & 10 to turn on two light bulbs (making the pins HIGH) and turn off them (making the pins LOW).

I am using serial communication from processing to send command to turn the bulbs ON/Off. Below is the excerpt of the code on processing:

//import class to set up serial connection with wiring board
import processing.serial.*;.// importing serial library
Serial portOne; // Naming serial port 1
Serial portTwo; // Naming serial port 2

void setup() {
portOne = new Serial(this, “Com7”, 9600); //setting com port for portOne and baud rate
portTwo= new Serial(this, “?”, 9600); //setting com port for portTwo AND baud rate
}

My question, is how do I assign a port to PortTwo, as Com7 is where Arduino board is connected to computer?
Regarding Arduino code, how Arduino code knows that from which comport the serial communication is coming? Below is the Arduino code for one pin (pin 9)

Below is the of Arduino code to activate pin 9:

char val; // variable to receive data from the serial port
int ledpin = 9; // Arduino LED pin 9 (on-board LED)

void setup() {
pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps
}

void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in ‘val’
}
if( val == ‘H’ ) // if ‘H’ was received
{
digitalWrite(ledpin, HIGH); // turn ON the LED
}
else
{
digitalWrite(ledpin, LOW); // otherwise turn it OFF

}

delay(100); // wait 100ms for next reading
}

Hello,

Please format your code:
https://discourse.processing.org/faq#format-your-code

There are many tutorials out there to get you up and running with Arduino, Processing and communicating between the two.

This is one example:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

Processing references:
https://processing.org/reference/libraries/serial/index.html

This is important:
https://processing.org/reference/libraries/serial/Serial_list_.html

This is incorrect; it should be “COM7” if this is the correct serial port.

You already have it assigned (incorrectly) to portOne.

Case is important!

com7 is NOT the same as COM7.
PortTwo is NOT the same as portTwo.

You may not assign COM7 to two serial connections on Processing; only assign it to one.
If you want two Arduinos (two different COM ports) to connect to Processing that is another discussion.
First connect one successfully.

Find a suitable tutorial on the Internet; there are many out there.
Go to the Arduino.cc site and find references there.
https://www.arduino.cc/en/pmwiki.php?n=Reference/serial (one example)

In my case Serial will be the USB serial connection to PC; Serial1, Serial2, Serial3 will be additional serial ports on my Arduino Mega 2560 R3 that are accessible as pins and NOT connected to PC through USB.

I will not provide any additional assistance until you have properly formatted your code and do a little more research.

:)

Hi @Jaguar, In your descriptions I see a confusion which may be resolved if you start ‘Device Manager’ (W7 onwards just type the name in) In there, close up the tree except for ‘Ports (Com and LPT)’. Now plug-in and remove all your Arduini, in turn and together. Watch the port names coming and going.

There is another mess: If you have the cheap Chinese not-Arduini, the port name is not the same each time. It depends on the socket on the PC, not the individual board and is very annoying. You will have to make sure you use the same socket for the same board each time.

The Arduino just receives the serial comms, it doesn’t know about the port name, or whether it’s connected to Windows/Mac/RPi…

//import class to set up serial connection with wiring board
import processing.serial.*;.// importing serial library
Serial portOne; // Naming serial port 1
Serial portTwo; // Naming serial port 2

void setup() {
printArray(Serial.list());
portOne = new Serial(this, “COM7”, 9600); //setting com port for portOne and baud rate
portTwo= new Serial(this, “?”, 9600); //setting com port for portTwo AND baud rate
}

Below is the of Arduino code to activate pin 9:

char val; // variable to receive data from the serial port
int ledpin = 9; // Arduino LED pin 9 (on-board LED)

void setup() {
pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps
}

void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in ‘val’
}
if( val == ‘H’ ) // if ‘H’ was received
{
digitalWrite(ledpin, HIGH); // turn ON the LED
}
else
{
digitalWrite(ledpin, LOW); // otherwise turn it OFF

}


delay(100); // wait 100ms for next reading
}

Thanks glv and Richard for taking your time and responding to my post! This is the first time I posted on this site . I was not aware of code formatting menu. Now I know. I also corrected the case. Thanks once again!
Regarding serial communication between Processing and Arduino, I am little bit familiar with that, and I was not having any issue as I have single load (light bulb). I can easily pass serial command from the processing to the arduino board. It worked, no issue. As I have now a second load (bulb), and want it to act independently of the first load, not sure how to assign the second port and also pass the command to Arduino. How arduino knows that I want to turn the second pin HIGH or LOW (second serial communication from processing to Arduino)? I have Arduino Uno board.
glv, if I understand correctly I have to purchase a USB to serial adapter to physically connect that USB Adpater to my computer and then use jumper wire to connect from adapter to RX and TX pins of the Arduino board (not 0 & 1 pins, which are already taken by Arduino USB connection to my computer).
Richard, mine is a Arduino board (don’t think fake Chinese one):slight_smile:
Any help will be appreciated. I will also go through the serial link as glv suggested.

Hello,

You can use the one serial USB port and send:

‘a’ to turn ON LED1
'b" to run OFF LED1
‘c’ to to turn ON LED2
‘d’ to turn OFF LED2

Or any ASCII characters you choose. :).

It is not clear what you are trying to do:

  • 2 Arduinos? I am just adding this… at first I got the impression it was this.
  • 1 Arduino with 2 serial connections?
  • 1 Arduino with 2 loads that you want to control? Above example will take care of that.
  • 1 Arduino with two serial connections to control 2 loads?

Please be perspicuous in your communication. I like that word!

:)

That seems correct. I do this all the time to monitor things with extra serial ports.

I have a bunch of these and I use the USB to serial adaptor for just that purpose:

The programmer is for other projects.

:)