joystick shield & processing

ciao a tutti :slight_smile: .. 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 :slight_smile:

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..

Ma lo sketch su Arduino ti funziona?
Se apri il serial monitor, Arduino si resetta, dopodiché dovrebbe inviarti una A ogni 300millisecondi, giusto?
Se gli invii un qualsiasi carattere dovrebbe uscire dal while e sparati i valori del joystick sulla seriale. Ti funziona?

Si ... il lato Arduino funziona perfettamente

EleCTr0n:
Si ... il lato Arduino funziona perfettamente

Bene. Perchè io sul lato Processing non ti so aiutare. :roll_eyes:

..banalmente:

  • ad Arduino è associata la COM3?
  • ti sei fatto lato processing un print dei dati che ti arrivano da seriale da Arduino?

Nella comunicazione seriale con Arduino, aiuta molto in processing utilizzare la funzione

bufferUntil()
int[] serialInArray = new int[3];    // Where we'll put what we receive

penso che qui devi mettere 5, se i byte sono 5