# Multiple Values from Processing to Arduino through Serial Port

**URL:** <https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [October 5, 2018, 1:54am UTC](https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152 "2018-10-05T01:54:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cai](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Cai](https://discourse.processing.org/u/Cai)\
**Post date:** [October 5, 2018, 1:54am UTC](https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152/1 "2018-10-05T01:54:16Z")

</div>

I have managed to send one value from processing to arduino. (which maps mouseX and lights the respective LED on an LED strip of Neopixels) I went through a lot of forums and tried to send Mouse Position(2 values ) from processing to arduino … By trying to separate the numbers using substring and toInt … But then the program doesn’t seem to be working.  
can anyone share an simple example that sends and receives X and Y values, or multiple values.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 5, 2018, 12:41pm UTC](https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152/2 "2018-10-05T12:41:10Z")

</div>

Hi Cai,

It can be quite tricky to communicate with an arduino. I spent 2 days figuring out how to do it for my project: [A universal ambilight](https://discourse.processing.org/t/a-universal-ambilight/1962)

I adapted the basic Serial example form processing. The idea stays the same: when the mouse is over the rectangle then then an LED connected to the pin number 4 light up.

The processing code:

```auto
import processing.serial.*;

Serial myPort;

void setup() 
{
  size(300, 300);
  myPort = new Serial(this, Serial.list()[0], 9600);
  delay(5000);
}

void draw() {
  background(20);
  
  myPort.write(mouseX + ";" + mouseY + ";");

  if (mouseOverRect()) {
    fill(220, 20, 20);
  } else {
    fill(200);
  }
  rect(100, 100, 100, 100);
}

boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 100) && (mouseX <= 200) && (mouseY >= 100) && (mouseY <= 200));
}

```

The arduino code:

```auto
const long BAUD_RATE = 9600;
int x, y;

void setup() {
  Serial.begin(BAUD_RATE);
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  newData = false;
  x = 0;
  y = 0;
}

void loop() {
  receiveData();
  
  if (x >= 100 && x <= 200 && y >= 100 && y <= 200) {
    digitalWrite(4, HIGH);
  } else {
    digitalWrite(4, LOW);
  }
}

void receiveData() {
  if (Serial.available() > 0) {
    x = Serial.parseInt();
    y = Serial.parseInt();
    Serial.read();
  }
}

```

Two things are worth noticing:

- The `delay(5000)` is used to let the time to the arduino mode to correctly make the connection with the computer.
- Be careful at the baud rate, it can be the reason why you can’t read some data. In my project, I had to set the baud rate to **500000** to make it works.

---

<div class="post-metadata">

**Author:** ![Cai](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Cai](https://discourse.processing.org/u/Cai)\
**Post date:** [October 8, 2018, 11:04pm UTC](https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152/3 "2018-10-08T23:04:41Z")

</div>

I got that … thank you , made my day …  
I was wondering , as to how could i replicate a processing sketch on Arduino … in a X, Y grid I have managed to find the index number of the LED and corresponding to (mapped)mouseX and mouseY .  
for example in an grid of 3\*3  
how could I send values such that I can draw an diagonal line ?

0 – 0 – 1  
0 – 1 – 0  
1 – 0 – 0

I can make that arduino … But i want to use interactivity of processing and display it on my arduino XY grid made out of neo pixels …
