# Arduino To Processing Error For Capactive Sensor

**URL:** <https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 24, 2019, 9:36pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781 "2019-11-24T21:36:27Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 9:36pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/1 "2019-11-24T21:36:27Z")

</div>

Hello,

I have successfully drafted the Ardunio code for capacitive sensing. The serial Monitor shows right values.

Also, I have successful drafted the processing code which shows the exact same values from Arduino’s serial monitor on processing’s console.

However, on processing the drawing function is not doing by desired drawing when reading values. Moreover, both codes are working fine independently.

Please help.

Moreover, my processing code works fine with other ardunio example code. That is drawings change.

Thank you for helping

**Arduino Code:**

```auto
#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
 */

CapacitiveSensor cs_4_2 = CapacitiveSensor(A4,A2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

void setup()                    
{

   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);

}

void loop()                    
{
    long start = millis();
    long total1 = cs_4_2.capacitiveSensor(30);
  

    //Serial.println(millis() - start); // check on performance in milliseconds
    //Serial.println("\t"); // tab character for debug window spacing

    Serial.println(total1); // print sensor output 1
    Serial.println("\t");

    delay(1000); // arbitrary delay to limit data to serial port 
}

```

**Processing Code**

```auto
import processing.serial.*;

import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
int sensor1;
int sensor2;
int sensor3;
float mappedSensor1;
float mappedSensor2;
float mappedSensor3;
PImage Gifimg;
PImage firstimg;
PImage secondimg;

PImage Frontimg; // Declare variable "a" of type PImage

// Declare variable "a" of type PImage

void setup() {
  size(1080, 800);
  // List all the available serial ports in the console
  printArray(Serial.list());
   Gifimg = loadImage("secondsmall.png"); // Load the image into the program
   Frontimg = loadImage("Frontsmall.png"); // Load the image into the program  
   secondimg = loadImage("finalsmall.png"); // Load the image into the program  

   
   

  // Change the 0 to the appropriate number of the serial port
  // that your microcontroller is attached to.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  // read incoming bytes to a buffer
  // until you get a linefeed (ASCII 10):
  myPort.bufferUntil('\n');
}

void draw() {
  background(255);

  if (sensor1 == 0) {
image(secondimg, 0, 0);
image(Gifimg, 0, 0);

} 
else {
image(Frontimg, 0, 0);

 

  }

}

void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    // println(myString);
    myString = trim(myString);

    // split the string at the commas
    // and convert the sections into integers:
    int sensors[] = int(split(myString, ','));
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
    }
    // add a linefeed at the end:
    println();
    sensor1 = sensors[0];  
    
    mappedSensor1 = map(sensor1, 0, 1023, height, 0);
   
  }
}

```

---

<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:** [November 24, 2019, 10:29pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/2 "2019-11-24T22:29:24Z")

</div>

Hello,

Welcome to the community!

Try this for Arduino code for testing:

```auto
boolean toggle = false;

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

void loop()                    
  {  
  long total1 = 271828182;
  long total2 = 314159265;

  if (toggle)
    {  
    Serial.print(0);
    Serial.print(',');
    Serial.println(total1);
    }
  else
    {
    Serial.print(1);
    Serial.print(',');
    Serial.println(total2);      
    }
    
  delay(1000);
  toggle = !toggle;                          
  }

```

You are sending a **long** as a **String** so receive as a **String** for now; you are converting to an **int** in your code.

```auto
String sensors[] = split(myString, ',');

```

My processing code (modified yours a bit) was able to receive “sensor number” and “data” and print with:

```auto
    sensor1 = int(sensors[0]);
    println(sensor1);
    long data = Long.parseLong(sensors[1]);
    println(data);

```

I then displayed a circle() to keep it simple and changed color depending on “sensor number”.

🙂

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 10:34pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/3 "2019-11-24T22:34:14Z")

</div>

Hi, Thank you so much for suggesting.

I understand you are saying that I’m sending string to processing rather than integers that’s processing is unable to read numbers?

Where in my code within processing, I shall put your modified code?

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 10:35pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/4 "2019-11-24T22:35:01Z")

</div>

I’m new to this. Could you please put changes into my processing code and share it as a whole so i could test it please

---

<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:** [November 24, 2019, 10:38pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/5 "2019-11-24T22:38:55Z")

</div>

> [@SusanAlyse001](#):
>
> Could you please put changes into my processing code and share it as a whole so i could test it please

You can try the suggestions I made:

1. Receive as a String and print as a String for testing
2. When you want to use the received data as a number you have to convert; see comments in my example code "Convert String to… "

I edited some errors in code above…

If you ask me to edit your code in future I will not assist you; this is your project.

🙂

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 10:42pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/6 "2019-11-24T22:42:15Z")

</div>

Hi, sure.  
So I understand that my current code is sending a string to processing and you want me to test string data. Which part of my processing code should i remove so my string doesnt convert into integer?

---

<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:** [November 24, 2019, 10:50pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/7 "2019-11-24T22:50:14Z")

</div>

You are receiving Strings in the array; that is fine and works.  
It is much more complicated to send “numbers” with Serial.write() (only sends a byte) and I would not do this.  
You must convert the string you received (Strings are in array) to numbers if you want to use them as numbers.

I provided enough for you to get started.

🙂

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 10:54pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/8 "2019-11-24T22:54:01Z")

</div>

Sure. So I get these points. Please correct if I m wrong:

1. My arduino is sending string data
2. My processing is converting string into integer
3. You are suggesting that i should not do the second step and receive string on processing as well?

Is this correct

---

<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:** [November 24, 2019, 11:05pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/9 "2019-11-24T23:05:08Z")

</div>

1. You are sending a **long** data type as a **String** with Serial.print()
2. You are receiving a **String** and converting it to an **int** in your code.
3. Consider saving **String** in array initially until you get this working and then convert the **String** to a **long** data type when it is required.

Breaking into steps is easier to debug code.

Keep in mind that print() a String or a number (int, long, etc.) will show the same on the console but they are two different data types! You must keep track of them and use as per your requirements (data or string).

It is your code so you must decide how you want to do this.

🙂

Examples of conversions:

```auto
void setup() 
  {
  int num = 12345678;
  println(num);
  
  String s1 = "12345678";
  println(s1);
  
  int num2 = int(s1);
  println(num2);
  
  num2 = parseInt(s1); //Same as above
  println(num2);
  
  String s2 = "1234567890";
  println(s2);
  
  long num3 = 1234567890;
  println(num3); 
  
  long num4 = Long.parseLong(s2);
  println(num4); 
  }

```

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 11:45pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/10 "2019-11-24T23:45:42Z")

</div>

Thank you.  
So now I’m sending “int” instead of string through Arduino and processing is receiving int without any conversion. But same issue, drawing function now working

Arduino:

```auto
#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10 megohm between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
 */

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil

void setup()                    
{

   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);

}

void loop()                    
{
    int start = millis();
    int total1 = cs_4_2.capacitiveSensor(30);
  

    //Serial.println(millis() - start); // check on performance in milliseconds
    //Serial.println("\t"); // tab character for debug window spacing

    Serial.println(total1); // print sensor output 1
    Serial.println("\t");

    delay(1000); // arbitrary delay to limit data to serial port 
}

```

Processing Code

```auto
/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

void setup() 
{
  size(200, 200);
  // 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()
{
  if ( myPort.available() > 0) { // If data is available,
    val = myPort.read(); // read it and store it in val
  }
  background(255); // Set background to white
  if (val > 1000) { // If the serial value is 0,
    fill(0); // set fill to black
  } 
  else { // If the serial value is not 0,
    fill(204); // set fill to light gray
  }
  rect(50, 50, 100, 100);
  println();
}

```

// Wiring / Arduino Code

```auto
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4; // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT); // Set pin 0 as an input
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) { // If switch is ON,
    Serial.write(1); // send 1 to Processing
  } else { // If the switch is not ON,
    Serial.write(0); // send 0 to Processing
  }
  delay(100); // Wait 100 milliseconds
}

```

---

<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:** [November 24, 2019, 11:53pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/11 "2019-11-24T23:53:11Z")

</div>

> [@SusanAlyse001](#):
>
> So now I’m sending “int” instead of string through Arduino and processing is receiving int without any conversion.

You are now sending an **int** instead of a **long** and only sending a byte (0 to 255) with Serial.write()

When you use Serial.print() with Arduino it is sending the data as a string made of human-readable ASCII text and you can easily deal with this on Processing side and convert to an int.

> **[Serial.print() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/communication/serial/print/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

> **[Serial.write() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/communication/serial/write/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

🙂

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 24, 2019, 11:59pm UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/12 "2019-11-24T23:59:45Z")

</div>

Hi,

Thank you.

Now in arduino, I have changed the data type to “int” and instead of serial.print, I’m using serial.write to avoid conversion from Arduino…

But still processing is unable to read and make changes

---

<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:** [November 25, 2019, 12:02am UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/13 "2019-11-25T00:02:57Z")

</div>

Please read the references to understand.

I always send Strings from Arduino using Serial.print() and receive them as Strings on Processing side and convert them to data type I require.

🙂

---

<div class="post-metadata">

**Author:** ![SusanAlyse001](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@SusanAlyse001](https://discourse.processing.org/u/SusanAlyse001)\
**Post date:** [November 25, 2019, 12:04am UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/14 "2019-11-25T00:04:26Z")

</div>

Sure.  
So on arduino side, I should keep it as this

long start = millis();  
long total1 = cs\_4\_2.capacitiveSensor(30);

How should I send string and recieve strings on processing?

---

<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:** [November 25, 2019, 12:12am UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/15 "2019-11-25T00:12:17Z")

</div>

I provided examples for you.

I modified your code as follows and it works with the Arduino example I provided:

```auto
void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    myString = trim(myString);
    println(myString);

    String sensors[] = split(myString, ',');
    printArray(sensors);

// Process data from array here.

    }
  }

```

How you choose to code this is up to you.  
This is your project.

🙂

This is output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d6397a81bc6f2578e8f592f279633718b37ba301.png)

[https://processing.org/reference/printArray\_.html](https://processing.org/reference/printArray_.html)  
Notice the quotes?  
That is because it is a String array.

I converted “1” or “0” received to an int (see my previous example) so I could compare int data (0 or 1) in my code.

🙂

---

<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:** [November 25, 2019, 12:37am UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/16 "2019-11-25T00:37:03Z")

</div>

Hello,

This is one of the more difficult concepts for new programmers.  
You have to work through it.

Keep it as simple as possible and build on that.  
Send “known” data and see if you are receiving that; I did this with my example.

Once you are receiving and can print the data AND know the data type (String, int, long, byte etc) then you can process it and do cool stuff with it!

Happy coding!

🙂

---

<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:** [November 25, 2019, 12:42am UTC](https://discourse.processing.org/t/arduino-to-processing-error-for-capactive-sensor/15781/17 "2019-11-25T00:42:57Z")

</div>

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769):
>
> Hello. I am not sure what to call the value inside the bufferUntil, but as my title implies I need to use two different characters, rather than just the newline (\n). Is it possible that, inside the serialEvent() function, I have something like ... void serialEvent(Serial port){ x = float(port.readStringUntil("X")); y = float(port.readStringUntil("Y")) } //statements ... where the values from serial look something like: (note: for the sake of using the values for Processing, I appende…
