ciao a tutti .. da un po di tempo sto lavorando su una joystick shield e in questo periodo mi era passato per la testa di fare un progettino con processing in modo da avere in tempo reale su schermo quali fossero i tasti premuti sul joystick in una sorta di joystick virtuale su pc .. caricato il codice su arduino e compilato quello di processing senza apparenti errori di sintassi sorge il problema .. e come se il programma non sia connesso all'arduino ... sono certo che riuscirete a darmi una mano
ecco qui gli sketch :
codice arduino
int x,y;
int a,b,c,d,p;
int inByte = 0;Â Â Â Â // incoming serial byte
void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
Â
 for (int i=2; i<8; i++){
  pinMode (i,INPUT);
 }
 establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
 // if we get a valid byte, read analog ins:
 Â
  inByte = Serial.read();
 Â
  x= map (A0,0,1024,0,254);
  y = map (A1,0,1024,254,0);
 Â
 a=digitalRead(6);
 b=digitalRead(7);
 c=digitalRead(4);
 d=digitalRead(5);
 p=digitalRead(3);Â
Â
 /*Serial.write(x);
 Serial.write(y);*/
 Serial.write (a);
  Serial.write (b);
  Serial.write (c);
   Serial.write (d);
   Serial.write (p);
   Â
Â
 Â
Â
}
void establishContact() {
 while (Serial.available() <= 0) {
  Serial.print('A'); // send a capital A
  delay(300);
 }
}
codice processing
RECT bott1, bott2, bott3, bott4, bott5;
analogico stick;
import processing.serial.*;Â //libreria seriale
Serial myPort;Â Â Â Â Â Â Â //oggetto myPort costruito dalla classe Serial
int[] serialInArray = new int[3];Â Â // Where we'll put what we receive
int serialCount = 0;Â Â Â Â Â Â Â Â // A count of how many bytes we receive
boolean firstContact = false;
int status;
int bott1_s,bott2_s, bott3_s, bott4_s, bott5_s;
void setup (){
 smooth();
 size (1000,500);
 background(50,0,200);
 strokeWeight(8);
Â
 myPort = new Serial(this, "COM3", 9600);
}
void draw (){
  make_bott();
  make_stick();Â
}
void make_bott(){
  bott1=new RECT (150,50,100,100);
  bott2=new RECT (150-100,50+100+20,100,100);
  bott3=new RECT (150+100, 50+100+20,100,100);
  bott4=new RECT (150,50+200+20+20,100,100);
  bott5=new RECT (500,300,100,100);
 Â
  bott1.make("   A");
  bott2.make("   B");
  bott3.make("   C");
  bott4.make("   D");
  bott5.make("   P");
 Â
  bott1.comunication (bott1_s);
  bott2.comunication (bott2_s);
  bott3.comunication (bott3_s);
  bott4.comunication (bott4_s);
  bott5.comunication (bott5_s);
 Â
}
void make_stick(){
 stick=new analogico(700,200,150, 25,25,50);
 stick.make("stick 1");
}
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 > 4 ) {
   bott1_s = serialInArray[0];
   bott2_s = serialInArray[1];
   bott3_s = serialInArray[2];
   bott4_s = serialInArray[3];
   bott5_s = serialInArray[4];  Â
  Â
   // 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;
  }
 }
}
classe analogico ~ processing
class analogico{
 int xfisso_g, yfisso_g, rfisso_g, x_g,y_g,r_g;
Â
 analogico(int xfisso, int yfisso, int rfisso,int x, int y, int r){
    xfisso_g=xfisso;
    yfisso_g=yfisso;
    rfisso_g=rfisso;
   Â
    x_g=x;
    y_g=y;
    r_g=r;
   Â
    textSize(20);
    textAlign(LEFT);  Â
   }
  Â
 void make (String testo){
   fill(255);
   text (testo, xfisso_g+50, yfisso_g+100);
   ellipse(xfisso_g,yfisso_g,rfisso_g,rfisso_g);
   fill (0);
  ellipse(xfisso_g-x_g, yfisso_g-y_g, r_g,r_g);
 }
Â
}
classe bottone ~ processing
class RECT {Â
 int g_x;
 int g_y;
 int g_l;
 int g_a;
Â
   RECT (int x, int y, int l, int a){    Â
   g_x=x;
   g_y=y;
   g_l=l;
   g_a=a;
  Â
   textSize(20);
   textAlign(LEFT);
  }
 Â
  void make(String Testo){Â
  fill(255) ;
  rect(g_x,g_y,g_l,g_a);
  text (Testo,g_x,g_y);
   }
  Â
  void comunication (int status){
  if (status==1){
   fill (0,0,255);
  }Â
  else {}
 Â
 }
Â
}
P.s. ancora l'analogico non e` totalmente implementato..