Arduino, piezo sensors and Processing animations

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,

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;

}

}

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

keyPressed() {}

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

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

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) but in java and with the sensors instead of the keyboard keys.

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;
  }
  
}