# Arduino, piezo sensors and Processing animations

**URL:** <https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 22, 2020, 4:51pm UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255 "2020-01-22T16:51:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EasyJu](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EasyJu](https://discourse.processing.org/u/EasyJu)\
**Post date:** [January 22, 2020, 4:51pm UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/1 "2020-01-22T16:51:55Z")

</div>

Hi everyone,

(french version below)  
I’m working on a project with Arduino, piezo sensors and Processing. I would like to have an animation played on Processing for each time a sensor is activated (an animation for a sensor). The first sensor sends on Arduino: A + the value of the analog voltage. (example: A121). On Processing, I used switch to locate the first sensor A. The program returns well “note A: 128”. But does not want to return an ellipse (or another shape) when the sensor is activated.

Thanks for your help,  
\_\_

Je suis en train de réaliser un projet avec Arduino, des capteurs piezo et Processing. J’aimerais qu’une animation se joue sur Processing à chaque fois qu’un capteur est activé (une animation pour un capteur). Le premier capteur envoie sur Arduino : A + la valeur de la tension analogique. (exemple : A121). Sur Processing, j’ai utilisé switch pour repérer le premier capteur A. Le programme renvoie bien “note A : 128”. Mais ne veut pas renvoyer une ellipse (ou une autre forme) quand le capteur est activé.

Merci pour votre aide,

 ![Capture d’écran 2020-01-22 à 17.51.28](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/69cc91f997c25d428b8085f9ee2d8717e81c612d.jpeg)

---

<div class="post-metadata">

**Author:** ![EasyJu](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EasyJu](https://discourse.processing.org/u/EasyJu)\
**Post date:** [January 22, 2020, 4:53pm UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/3 "2020-01-22T16:53:47Z")

</div>

Here my Processing code :

import processing.serial.\*; // librairie qui permet la communication serie avec Arduino  
Serial myPort; // instance de la classe Serial  
int threshold = 90; // valeur de seuil

void setup() {  
size(displayWidth, displayHeight); // plein écran  
myPort = new Serial(this, “/dev/cu.usbmodem1411” , 9600); // le porte utilisé et la meme valeur en baud du prog Arduino  
myPort.bufferUntil(’\n’); // ne génére pas d’événement en série, sauf si il y a une nouvelle ligne de caractère  
}

void draw() {

}

void serialEvent (Serial myPort) {

String serie = myPort.readString();

switch (serie.substring(0,1)){  
case “A”:  
println("note A : "+serie.substring(1));  
//déclencher animation A  
ellipse(56, 46, 55, 55);  
break;

```
case "B":
println("note B : "+serie.substring(1));
//déclencher animation B
break;

```

}

}

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 23, 2020, 4:11am UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/4 "2020-01-23T04:11:03Z")

</div>

your serial event code might draw a ellipse  
( only 1/60 of a second / see a flicker? )

* * *

so please start  
step\_1  
ignore serial … arduino …  
make that program drawing what you want on

```auto
keyPressed() {}

```

---

<div class="post-metadata">

**Author:** ![EasyJu](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EasyJu](https://discourse.processing.org/u/EasyJu)\
**Post date:** [January 27, 2020, 11:49am UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/5 "2020-01-27T11:49:46Z")

</div>

Thanks for your response. Yes it works with keyPressed ! But what can I do to make it right with piezo sensors and serial port ?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 28, 2020, 2:39am UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/6 "2020-01-28T02:39:30Z")

</div>

please show your latest code with keyPressed() AND serialEvent()

---

<div class="post-metadata">

**Author:** ![EasyJu](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@EasyJu](https://discourse.processing.org/u/EasyJu)\
**Post date:** [January 29, 2020, 1:24pm UTC](https://discourse.processing.org/t/arduino-piezo-sensors-and-processing-animations/17255/7 "2020-01-29T13:24:15Z")

</div>

Hi kll,

Thanks for your reponse. Here my latest code. It shows the animation when I press the sensor but the animation don’t stop. I’d have to create an array but I didn’t succeed. I would like to activate the animations like this program ([https://www.openprocessing.org/sketch/804222](https://www.openprocessing.org/sketch/804222)) but in java and with the sensors instead of the keyboard keys.

```auto
import processing.serial.*; // library that allows serial communication with Arduino
Serial myPort; // instance of the Serial class
String signalA;
// String signaux[];
AnimationA animA;

void setup() {
  size(displayWidth, displayHeight); // full screen
  background(0);
  myPort = new Serial(this, "/dev/cu.usbmodem1411" , 9600); // port used and the same baud value of Arduino program
  myPort.bufferUntil('\n'); // does not generate a serial event unless there is a new character line
  animA = new AnimationA(random(width), random(height), 1); // first animation
}
 
void draw() {

  if (signalA != null){
    animA.display(); // play the first animation
  }
    
}

void serialEvent (Serial myPort) {

  String serie = myPort.readString();
  
  switch (serie.substring(0,1)){
    case "A":
    println("note A : "+serie.substring(1));
    signalA = serie.substring(1);
    //signaux[0] = signalA ;
    break;
    
    case "B":
    println("note B : "+serie.substring(1));
    break;
  }
  
}

void keyPressed() {
  background(100);
}

class AnimationA {
  // parameters declaration
  float x;
  float y;
  float v1;
  float v2;
  float v3;
  float radius;
  

  // constructor
  AnimationA(float nouvX, float nouvY, float nouvRadius) {
    x = nouvX;
    y = nouvY;
    v1 = random(145, 255);
    v2 = random(145, 255);
    v3 = random(145, 255);
    radius = nouvRadius;
  }

  void display(){
    noFill();
    strokeWeight(1);
    stroke(v1, v2, v3);
    circle(x, y, radius+200);
    circle(x, y, radius+100);
    circle(x, y, radius);
    circle(x, y, radius-100);
    radius += 30;
  }
  
}

```
