# Multiple print values from Arduino and Processing

**URL:** <https://discourse.processing.org/t/multiple-print-values-from-arduino-and-processing/27545>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 2, 2021, 2:48pm UTC](https://discourse.processing.org/t/multiple-print-values-from-arduino-and-processing/27545 "2021-02-02T14:48:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saaqib.m](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@saaqib.m](https://discourse.processing.org/u/saaqib.m)\
**Post date:** [February 2, 2021, 2:48pm UTC](https://discourse.processing.org/t/multiple-print-values-from-arduino-and-processing/27545/1 "2021-02-02T14:48:42Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi, I am trying to send multiple print statements from my arduino to processing but I cant seem to obtain it properly in the processing. Could someone have a look please? Note only parts of the entire code is shown but nevertheless it should be clear. Essentially splitstr[0] should contain ALL the incoming data and should be mapped but splitstr[1] should be only the sampled value stored in the table!

Arduino:

```auto
#include <SoftwareSerial.h> 
int analog_output; //Serial Read output
int sample_output;
int r_pin = A0; //Initialising output pin
unsigned long delayStart = 0; // the time the delay started
unsigned long DELAY_TIME = 1500; //1500 is equal to 1.5 seconds
bool delayRunning = false; // true if still waiting for delay to finish
int value;

void setup() {              
  Serial.begin(9600); // Starting serial communication at 9600 Baud Rate
  delayStart = millis(); //start delay
  delayRunning = true;
}

void loop() {
  if (delayRunning == true){
    analog_output = analogRead (r_pin); // Reading from the resistor
    Serial.println (analog_output);
  }
  else if (delayRunning == true && delayStart % DELAY_TIME == 0) {
    sample_output = analogRead (r_pin);
    Serial.print(analog_output);
    Serial.println(sample_output);      
  }
}

```

Processing:

```auto
Table table;

Table table;

void setup () {
  size(1000, 700); // Set the window size:   
  smooth();
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600); // A serialEvent() is generated when a newline character is received
  myPort.bufferUntil('\n'); // Sets a specific byte to buffer until before calling serialEvent()
  //myPort.bufferUntil(DELAY_TIME);
  background(0); // Set inital background colour
     
  table = new Table();
  table.addColumn("Time Elapsed: ");
  table.addColumn("Signal ADC Value: ");
  table.addColumn("Voltage (V): ");
  table.addColumn("Sampled Value: ");
  
  frameRate(60);
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  inString = myPort.readStringUntil('\n');
  println(inString);
  String [] splitstr = split(inString,' ');
  if (splitstr != null) {
    //inString = trim(inString); // trim off whitespaces.
    inByte = float(trim(splitstr[0])); // convert to a number.
    float inScreen = map(inByte, 0, 1023, 0, height); // map to the screen height.
    println(inScreen);
    newData = true; 
  }

  Vout = Vin*inByte/1023;
  
  TableRow newRow = table.addRow();
  newRow.setString("Time Elapsed: ", txt);
  newRow.setFloat("Signal ADC Value: ", inByte);
  newRow.setFloat("Voltage (V): ",Vout);
  if (splitstr[1] != null){
  newRow.setFloat("Sampled Value: ",float(splitstr[1]));
  };  
}

```

---

<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:** [February 7, 2021, 3:20pm UTC](https://discourse.processing.org/t/multiple-print-values-from-arduino-and-processing/27545/2 "2021-02-07T15:20:47Z")

</div>

see how receiving an sending then mange your code

```auto
// This example code is in the public domain.
import processing.serial.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the
          // microcontroller
void setup() {
 size(256, 256); // Stage size
 noStroke(); // No border on the next thing drawn
 // Set the starting position of the ball (middle of the stage)
 xpos = width/2;
 ypos = height/2;
 // Print a list of the serial ports, for debugging purposes:
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my FTDI adaptor, so I open Serial.list()[0].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
}
void draw() {
 background(bgcolor);
 fill(fgcolor);
 // Draw the shape
 ellipse(xpos, ypos, 20, 20);
}
void serialEvent(Serial myPort) {
 // read a byte from the serial port:
 int inByte = myPort.read();
 // if this is the first byte received, and it's an A,
 // clear the serial buffer and note that you've
 // had first contact from the microcontroller.
 // Otherwise, add the incoming byte to the array:
 if (firstContact == false) {
 if (inByte == 'A') {
  myPort.clear(); // clear the serial port buffer
  firstContact = true; // you've had first contact from the microcontroller
  myPort.write('A'); // ask for more
 }
 }
 else {
 // Add the latest byte from the serial port to array:
 serialInArray[serialCount] = inByte;
 serialCount++;
 // If we have 3 bytes:
 if (serialCount > 2 ) {
  xpos = serialInArray[0];
  ypos = serialInArray[1];
  fgcolor = serialInArray[2];
  // print the values (for debugging purposes only):
  println(xpos + "t" + ypos + "t" + fgcolor);
  // Send a capital A to request new sensor readings:
  myPort.write('A');
  // Reset serialCount:
  serialCount = 0;
 }
 }
}

```

arduino

```auto
/*
 Serial Call and Response
 Language: Wiring/Arduino
 This program sends an ASCII A (byte of value 65) on startup
 and repeats that until it gets some data in.
 Then it waits for a byte in the serial port, and
 sends three sensor values whenever it gets a byte in.
 Thanks to Greg Shakar and Scott Fitzgerald for the improvements
 The circuit:
 * potentiometers attached to analog inputs 0 and 1
 * pushbutton attached to digital I/O 2
 Created 26 Sept. 2005
 by Tom Igoe
 modified 24 April 2012
 by Tom Igoe and Scott Fitzgerald
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/SerialCallResponse
 */
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 }
 pinMode(2, INPUT); // digital sensor is on digital pin 2
 establishContact(); // send a byte to establish contact until receiver
      // responds
}
void loop()
{
 // if we get a valid byte, read analog ins:
 if (Serial.available() > 0) {
 // get incoming byte:
 inByte = Serial.read();
 // read first analog input, divide by 4 to make the range 0-255:
 firstSensor = analogRead(A0)/4;
 // delay 10ms to let the ADC recover:
 delay(10);
 // read second analog input, divide by 4 to make the range 0-255:
 secondSensor = analogRead(1)/4;
 // read switch, map it to 0 or 255L
 thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
 // send sensor values:
 Serial.write(firstSensor);
 Serial.write(secondSensor);
 Serial.write(thirdSensor);
 }
}
void establishContact() {
 while (Serial.available() <= 0) {
 Serial.print('A'); // send a capital A
 delay(300);
 }
}

```
