Probleme avec Processing

Bonjour a tous,

Je continue ma découverte de l'Arduino et du code et je test Processing.

Je commence les tuto du site officiel et ça ne marche pas.

Le but est de lire l’état d'un switch et changer la couleur d'un rectangle en fonction.

code arduino :

const byte switchPinTurnLeft=4;
boolean switchValueTurnLeft=HIGH;

void setup(){

  Serial.begin(9600);

  pinMode(switchPinTurnLeft, INPUT_PULLUP);
}

void loop(){
  switchValueTurnLeft=(digitalRead(switchPinTurnLeft)==LOW);

  if (switchValueTurnLeft==HIGH){
    Serial.write(1);
    Serial.print("YES");
  }
  else  {
    Serial.write(0);
    Serial.print("NO");
  }
  delay(100);
}

Code Processing :

import processing.serial.*; 
 
Serial port;                             
int val;                                 
 
void setup() { 
  size(200, 200); 
  frameRate(10); 
  
  port = new Serial(this, 9600); 
} 

void draw() { 
  if (0 < port.available()) {   
    val = port.read();             
  } 
  background(255);                  
  if (val == 0)  {                   
    fill(0);                       
  } else {                          
    fill(204);                       
  } 
  rect(50, 50, 100, 100);
}

Sur le serial.print Arduino j'ai bien l état du switch qui change, du coté Processing j ai le rectangle avec la première couleur mais elle ne change pas.

Auriez vous une idée ?

Pour info j utilise processing 1.5.1

MERCI

Ce sketch fonctionne avec processing:

Coté Arduino

const byte switchPinTurnLeft=4;
boolean switchValueTurnLeft=HIGH;

void setup(){

  Serial.begin(9600);

  pinMode(switchPinTurnLeft, INPUT_PULLUP);
}

void loop(){
  switchValueTurnLeft=digitalRead(switchPinTurnLeft);
  uint8_t buf;
  buf = 0;
  Serial.write(buf);
  buf = 10;
  delay(1000);
  Serial.write(buf);
  delay(1000);
/*
  if (switchValueTurnLeft==HIGH){
    Serial.print(10);
    //Serial.print("YES");
  }
  else  {
    Serial.print(10);
    //Serial.print("NO");
  }
  */
  delay(100);
}

Coté Processing

import processing.serial.*; 
 
Serial port;                             
int val;                                 
 
void setup() { 
  size(200, 200); 
  frameRate(10); 
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. 
                                      // to match your port
  port = new Serial(this,portName ,9600); 
} 

void draw() { 
  if (0 < port.available()) {   
    val = val = port.read();       
  } 
  background(255);                  
  if (val == 0)  {                   
    fill(0);                       
  } else {                          
    fill(204);                       
  } 
  rect(50, 50, 100, 100);
}

A noter que du coté Processing on attend un int et non une chaine de caractères, pour que ça fonctionne.

Merci, ça y est le rectangle change de couleur :wink:

C'est en rajoutant cette partie, que ça marche, peux tu m'expliquer ce qu'elle fait.

String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc.
                                      // to match your port
  port = new Serial(this,portName ,9600);

J'ai pas "encore" rajouter cette partie, peux tu également m'expliquer ce qu'elle fait.

uint8_t buf;
  buf = 0;
  Serial.write(buf);
  buf = 10;
  delay(1000);
  Serial.write(buf);
  delay(1000);

A noter que du coté Processing on attend un int et non une chaine de caractères, pour que ça fonctionne.

Serial.write(1) ou Serial.write(variable) avec int variable=1; donne la même chose.

Merci

C'est bizarre, globalement c'est super réactif, des que j’appuie sur le switch la couleur change, mais des fois, il y a un temps de latence, sais tu pourquoi ?

Re Merci

Dans votre code d'origine

  • Changez 9600 En 115200 pour la vitesse du port série
  • Attention en plus d'envoyer 0 et 1 vous envoyez Du texte
  • il ne faut metttre aucun delay

String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port

Bonjour,
/* on est ici dans le PC */
en français : choisir un port dans la liste que le programme te propose
si tu as laissé 0, ce sera le premier de la liste
si par la suite tu branches une autre carte arduino, voire si tu rebranches la même quelques temps plus tard, il faudra chercher un autre numéro dans la liste
plug & pray !

port = new Serial(this,portName ,9600);

une fois qu'on a le numéro du port (ils disent portName), on peut créer l'objet Serial() qui servira au programme

9600 c'est la vitesse, par défaut

quant au "this", je ne sais pas, il n'est pas défini dans ton programme, donc je pense par défaut à 0, donc un paramètre de moindre importance, mis à sa valeur par défaut
édit : corrigé par la mise au point de J-M-L (merci !) ci-après

uint8_t buf;
buf = 0;
Serial.write(buf);
buf = 10;
delay(1000);
Serial.write(buf);
delay(1000);

/* on est ici dans l'arduino */

il faut que tu apprennes les signification de :
uint8_t
delay()
Serial.write()

tu verras que :
buf = 10;
delay(1000);
Serial.write(buf);

est peu ou prou équivalent à :

delay(1000);
buf = 10;
Serial.write(buf);

... et tu pourras utiliser l'une ou l'autre formule à ta guise

trimarco232:
quant au "this", je ne sais pas, il n'est pas défini dans ton programme, donc je pense par défaut à 0, donc un paramètre de moindre importance, mis à sa valeur par défaut

Non non c'est important :slight_smile: à comprendre

"this" ça représente dans le langage de programmation l'instance / l'objet courant - ici ce sera l'application (l'applet - type PApplet) qui possèdera le port série

cf la doc

Description
Refers to the current object (i.e., "this object"), which will change depending on the context in which this is referenced. In Processing, it's most common to use this to pass a reference from the current object into one of the libraries.

The keyword this can also be used to reference an object's own method from within itself, but such usage is typically not necessary. For example, if you are calling the filter() method of a PImage object named tree from another object, you would write tree.filter(). To call this method inside the PImage object itself, one could simply write filter() or, more explicitly, this.filter(). The additional level of specificity in this.filter() is not necessary, as it is always implied.

Merci, vous m avez mis sur la bonne piste.

ARF !!!! tout marché nickel,..., et la j'arrive plus a rien.

Je suis revenu a un code simple, j’appuie sur un bouton, sa envoi une valeur aléatoire a processing, en fonction de cette valeur processing dessine un rectangle colorié.

Ca ne marche pas, j'ai un rectangle blanc, pouvez vous me dire ce qu'il va pas.

MERCI

code arduino :

const byte switchPinColor=5;
boolean switchValueColor=HIGH;
byte color=0;

//***************************************
void setup(){

  Serial.begin(9600);

  pinMode(switchPinColor, INPUT_PULLUP);

  randomSeed(analogRead(0));
}

//***************************************
void loop(){
  switchValueColor=(digitalRead(switchPinColor)==LOW);

  if (switchValueColor==HIGH){
    color=random(1,4);
    Serial.print(color);
    delay(250);
  }
}

code Processing :

import processing.serial.*; 

Serial port;                             
int val;                                 

//***************************************
void setup() { 
  size(500, 500);
  stroke(0);
  background(255);  
  frameRate(10); 

  String portName=Serial.list()[0];
  port = new Serial(this, portName, 9600);
} 

//***************************************
void draw() { 

  if (0 < port.available()) {   
    val = port.read(); 
    colorSelection();
  } 
  rect(100, 100, 300, 300);
} 

//***************************************
void colorSelection() {
  switch(val) {
  case 1: 
    fill(22, 245, 7);
    break;
  case 2: 
    fill(7, 56, 245);
    break;
  case 3: 
    fill(240, 245, 7);
  }
}

Coté Processing on attend un nombre en binaire, donc utilise Serial.write() plutôt que Serial.print() dans ton code Arduino.

Edit: Passe à 921600 bauds des 2 cotés

MERCI