# Serial connection of Processing and Arduino

**URL:** https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [June 6, 2022, 3:13am UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344 "2022-06-06T03:13:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jinalilili](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jinalilili](https://discourse.processing.org/u/jinalilili)
#### Post date: [June 6, 2022, 3:13am UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344/1 "2022-06-06T03:13:53Z")

</div>

I’m try to make a serial connection from processing to arduino. The idea is that when I press “a”, the speaker from Arduino will produce a sound and the same goes with another key pressed but with a different note. However, I’m a bit stuck with the codes. Can anyone let me know what is the problem of my codes?

Processing code:

```auto
import processing.serial.*;
Serial myPort; 
int val; // Data received from the serial port

void setup()
{
size(200, 200);
String portName = Serial.list()[0]; 
myPort = new Serial(this, portName, 9600);
}

void draw() {
}

 void keyPressed(){
   if (key == 'a'){
     myPort.write(1); 
   }
   if (key == 'd'){
     myPort.write(2); 
   }  
 }
 void keyReleased(){
   myPort.write(0);
 } 

```

Arduino code:

```auto
char val; // Data received from the serial port
int speakerPin = 8;

void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600); 
}

void loop() {
while (Serial.available()) { 
val = Serial.read(); 
}
if (val == '1') {  
tone(speakerPin, 262, 200); 
} else if (val == '2'){
tone(speakerPin, 523, 200); 
}
delay(100); 
}

```

Many many thanks!

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [June 6, 2022, 4:34am UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344/2 "2022-06-06T04:34:40Z")

</div>

Hi

Try this in Arduino side

```auto
if(Serial.available() > 0) {

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [June 6, 2022, 4:54am UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344/3 "2022-06-06T04:54:33Z")

</div>

Or try this code

```auto
byte x = 0;
int speakerPin = 8;
void setup()
{
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
  
}
 
void loop() {

  if (int(x) == 1) {
    Do something 
  }
  else if (int(x) == 2) {
    Do something 
    
  }
  
  }
 
 
void serialEvent() {
  while (Serial.available()) {
    x = Serial.read();
  } 
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 6, 2022, 10:59am UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344/4 "2022-06-06T10:59:15Z")

</div>

Hello @jinalilili,

A `'1'` (character) and `1` (integer number) are two different things.

Processing:  
`myPort.write(1);`

Arduino:  
`if (val == '1') // This will NEVER be equal to 1`

Reference:  
[https://processing.org/reference/char.html](https://processing.org/reference/char.html)

This is a common error for those new to programming and serial communications.

Keep at it!

```auto
char a = '1';

// This will NOT be equal:
int b = 1;

//These will be equal:

//int b = 49; // ASCII for '1' in decimal
//int b = 0x31; // ASCII for '1' in hexadecimal

if (a == b)
  println("Equal");
else
  println("NOT Equal");

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/7/874524d1d498b97f1976d3ef50cdbeb361f44877.png)

`:)`

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 6, 2022, 5:06pm UTC](https://discourse.processing.org/t/serial-connection-of-processing-and-arduino/37344/5 "2022-06-06T17:06:25Z")

</div>

Was also discussed here: [https://stackoverflow.com/questions/72494671/serial-connection-of-processing-and-arduino](https://stackoverflow.com/questions/72494671/serial-connection-of-processing-and-arduino). Not sure why this answer was not accepted. It would help if there was some feedback.
