# Serial read() Arduino

**URL:** https://discourse.processing.org/t/serial-read-arduino/42689
**Category:** Electronics (Arduino, etc.)
**Created:** [September 1, 2023, 2:10pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689 "2023-09-01T14:10:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)
#### Post date: [September 1, 2023, 2:10pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/1 "2023-09-01T14:10:23Z")

</div>

Hi!

I have successfully established a Serial connection from Arduino to Processing using the read()-function ([read() / Libraries / Processing.org](https://processing.org/reference/libraries/serial/Serial_read_.html)). This only returns numbers between 0 and 255 though and I was wondering what the best method for receiving higher number would be?

Do I have to use readString() and convert it to an integer?

---

<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: [September 1, 2023, 2:48pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/2 "2023-09-01T14:48:36Z")

</div>

Hi

[Map / Examples / Processing.org](https://processing.org/examples/map.html#:~:text=Use%20the%20map()%20function,or%20color%20of%20a%20shape).

---

<div class="post-metadata">

### Author: ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)
#### Post date: [September 1, 2023, 3:36pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/3 "2023-09-01T15:36:01Z")

</div>

Thanks, but I want to receive let’s say numbers from 0 to 4000 from Arduino and not “map it down” to 0–255…

---

<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: [September 1, 2023, 4:59pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/4 "2023-09-01T16:59:14Z")

</div>

Hi  
You didn’t post your code you can send and receive any value you need depending on your code and need

Look at this example  
[https://senzor.robotika.sk/sensorwiki/index.php/Processing\_7-segment\_display](https://senzor.robotika.sk/sensorwiki/index.php/Processing_7-segment_display)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [September 1, 2023, 7:33pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/5 "2023-09-01T19:33:49Z")

</div>

> [@Map function worked in Processing 2 and early 3.. But broke in 3.5.3. Need guidance](https://discourse.processing.org/t/map-function-worked-in-processing-2-and-early-3-but-broke-in-3-5-3-need-guidance/11371/2):
>
> Mutating the sketch’s main canvas outside the “Animation Thread” crashes the program! boomCallback serialEvent() is run by the Serial’s Thread btW. warningSo even calling something like background() can be fatal! fearfulActually, due to a bad design, any Processing function which depends on color() will get wrong results when they’re multithreading invoked! bugSo any canvas mutating operations should be moved to draw() or called by it. face_with_monocleHere’s a post link for…

---

<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: [September 1, 2023, 8:42pm UTC](https://discourse.processing.org/t/serial-read-arduino/42689/6 "2023-09-01T20:42:10Z")

</div>

Please consider the following demo and see if you can adapt it to your project. The demo will send random integers from 255 to 1024 from Arduino followed by a line feed. The Processing code will put the incoming data into a string and break it at the line feed. The resulting string is then reconverted to an integer and printed to the console.

Processing Code:

```auto
import processing.serial.*;

Serial myPort;
String inStr;

void setup() {
  surface.setVisible(false);
  printArray(Serial.list());
  // Select port for your system
  myPort= new Serial(this, Serial.list()[4], 9600);
}

void draw() {
}

void serialEvent( Serial myPort) {
  //put the incoming data into a String -
  //the '\n' is delimiter indicating the end of a complete packet
  inStr = myPort.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (inStr != null) {
    //trim whitespace and formatting characters (like carriage return)
    inStr = trim(inStr);
    int val = Integer.valueOf(inStr);
    println("value = ", val);
  }
}

```

Arduino Code:

```auto
long randNumber;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // print a random number from 255 to 1024
  randNumber = random(255, 1024);
  Serial.println(randNumber);
  delay(50);
}

```

---

<div class="post-metadata">

### Author: ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)
#### Post date: [September 2, 2023, 7:43am UTC](https://discourse.processing.org/t/serial-read-arduino/42689/7 "2023-09-02T07:43:58Z")

</div>

Yes, I wasn’t very specific sorry. I simply want to send sensor values higher than 255 from Arduino to Processing – I understand now Serial.write() and read() in not the way to go. Thank you for your help!

---

<div class="post-metadata">

### Author: ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)
#### Post date: [September 2, 2023, 7:47am UTC](https://discourse.processing.org/t/serial-read-arduino/42689/8 "2023-09-02T07:47:21Z")

</div>

This is very useful, thank you so much!
