Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #15 on: February 07, 2013, 06:43:08 am » |
dovrebbe essere una cosa del genere? while (arduino.available () > 0) { inByte = arduino.readStringUntil('\n'); if ( inByte != null ) { a= arduino.readStringUntil('\n'); synchronized(a){ a = inByte; } fill(125); synchronized(a){ text(a,width/2, height/2); } } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8497
:(){:|:&};:
|
 |
« Reply #16 on: February 07, 2013, 07:14:13 am » |
togli a= arduino.readStringUntil('\n');
e
fill(125);
la text va messa nella funzione draw()
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #17 on: February 07, 2013, 07:49:42 am » |
Così dunque: import processing.serial.*; int width = 800; int height = 600; String inByte; String a; int button1X; int button1Y; int button1Width = width/10; int button1Height = height/20; int button2X; int button2Y; int button3X; int button3Y; int button2Width = button1Width; int button2Height = button1Height; int button3Width = button1Width; int button3Height = button1Height; float buttonFont = button1Height/1.5; float wordFont = button1Height;
Serial arduino; color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false; boolean button2Over = false; boolean button3Over = false; void setup() { size(width,height); smooth(); font = loadFont("CourierNew36.vlw"); buttonColor = color(255); buttonHighlight = color(200); buttonPressed = color(50); button1X = width/10 - button1Width/2; button1Y = height/5 - button1Height/2; button2X = 2*width/10 - button1Width/2; button2Y = height/5 - button1Height/2; button3X = 3*width/10 - button1Width/2; button3Y = height/5 - button1Height/2; arduino = new Serial(this,"COM3", 9600); arduino.bufferUntil('\n'); // SerialEvent for new line arduino.clear(); }
void draw() { update(mouseX,mouseY); background(0); if(button1Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button1X,button1Y, button1Width, button1Height); if(button2Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button2X,button2Y, button2Width, button2Height); if(button3Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button3X,button3Y, button3Width, button3Height); textFont(font, buttonFont); fill(0); text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3); text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3); text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
while (arduino.available () > 0) { inByte = arduino.readStringUntil('\n'); if ( inByte != null ) { synchronized(a){ a = inByte; } synchronized(a){ text(a,width/2, height/2); } } }
textFont(font, wordFont); fill(255); text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2); }
///////////////////////////////////
void update(int x, int y) { if(overButton(button1X,button1Y,button1Width,button1Height)){ button1Over = true; } else { button1Over = false; } if(overButton(button2X,button2Y,button2Width,button2Height)){ button2Over = true; } else { button2Over = false; } if(overButton(button3X,button3Y,button3Width,button3Height)){ button3Over = true; } else { button3Over = false; } }
void mousePressed() { if(button1Over) { println("Button1 pressed"); arduino.write('S'); fill(buttonPressed); rect(button1X,button1Y, button1Width, button1Height); } if(button2Over) { println("Button2 pressed"); arduino.write('N'); fill(buttonPressed); rect(button2X,button2Y, button2Width, button2Height); } if(button2Over) { println("Button3 pressed"); arduino.write('Z'); fill(buttonPressed); rect(button3X,button3Y, button3Width, button3Height); } }
boolean overButton(int x, int y, int width, int height) { if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } void serialEvent (Serial arduino) { // get the byte:
// print it: // println(inByte); // at the edge of the screen, go back to the beginning: }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8497
:(){:|:&};:
|
 |
« Reply #18 on: February 07, 2013, 09:18:25 am » |
no allora chiariamo le cose. Tu hai 2 thread, ovvero 2 "programmi" in parallelo su processing. Uno ha il loop che si chiama appunto loop() come su arduino, che non stai usando (infatti manca mi pare, ma va bene così. Quà vanno messe tutte le operazioni logiche. Uno si chiama draw() e viene chiamato quando bisogna aggiornare la finestra. Qua vanno messe SOLO le operazioni di disegno. Niente letture seriali o boiate del fenere.
In oltre ci sono degli eventi, ovvero funzioni che sono chiamate quando succede qualcosa. In questo caso "void serialEvent (Serial arduino) {"
quindi nell'evento seriale leggi la seriale e metti il risultato in una stringa (nel tuo caso "a").
il draw semplicemente stampa "a", nient'altro. se poi in "a" c'è un valore messo da seriale o il numero seriale di windows la fuunzione draw se ne deve fregare.
PERCHE?
perchè se la seriale si impalla (prova a staccare arduino), se fai come dico io la grafica è INDIPENDENTE dalla seriale e puoi chiudere/continuare ad usare il tuo programma... col tuo sistema anche la grafica si impalla e devi uccidere il programma da taskmanager.
altro esempio è se "a" fosse un'elaborazione pesante, l'immagine della finestra rimarrebbe congelata e non responsiva... non ti è mai capitato? bene, sappi che quello è un errore di strutturazione, o di architettura del programma.
kiss
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #19 on: February 07, 2013, 12:37:29 pm » |
Lesto ti chiederei troppo se ti chiedessi di fare i due pezzi di codice che mi servirebbero a prendere questi dati da arduino? Per piacere ne sto uscendo pazzo 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #20 on: February 07, 2013, 02:01:45 pm » |
Ho provato a modificare il codice in questo modo import processing.serial.*; int width = 800; int height = 600; String inByte = null; String a; int lf = 10; int button1X; int button1Y; int button1Width = width/10; int button1Height = height/20; int button2X; int button2Y; int button3X; int button3Y; int button2Width = button1Width; int button2Height = button1Height; int button3Width = button1Width; int button3Height = button1Height; float buttonFont = button1Height/1.5; float wordFont = button1Height;
Serial arduino; color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false; boolean button2Over = false; boolean button3Over = false; void setup() { size(width,height); smooth(); font = loadFont("CourierNew36.vlw"); buttonColor = color(255); buttonHighlight = color(200); buttonPressed = color(50); button1X = width/10 - button1Width/2; button1Y = height/5 - button1Height/2; button2X = 2*width/10 - button1Width/2; button2Y = height/5 - button1Height/2; button3X = 3*width/10 - button1Width/2; button3Y = height/5 - button1Height/2; arduino = new Serial(this,"COM3", 9600); arduino.bufferUntil('\n'); // SerialEvent for new line arduino.clear(); }
void draw() { update(mouseX,mouseY); background(0); if(button1Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button1X,button1Y, button1Width, button1Height); if(button2Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button2X,button2Y, button2Width, button2Height); if(button3Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button3X,button3Y, button3Width, button3Height); textFont(font, buttonFont); fill(0); text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3); text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3); text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
synchronized(a){ text(a,width/2, height/2); }
textFont(font, wordFont); fill(255); text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2); }
///////////////////////////////////
void update(int x, int y) { if(overButton(button1X,button1Y,button1Width,button1Height)){ button1Over = true; } else { button1Over = false; } if(overButton(button2X,button2Y,button2Width,button2Height)){ button2Over = true; } else { button2Over = false; } if(overButton(button3X,button3Y,button3Width,button3Height)){ button3Over = true; } else { button3Over = false; } }
void mousePressed() { if(button1Over) { println("Button1 pressed"); arduino.write('S'); fill(buttonPressed); rect(button1X,button1Y, button1Width, button1Height); } if(button2Over) { println("Button2 pressed"); arduino.write('N'); fill(buttonPressed); rect(button2X,button2Y, button2Width, button2Height); } if(button2Over) { println("Button3 pressed"); arduino.write('Z'); fill(buttonPressed); rect(button3X,button3Y, button3Width, button3Height); } }
boolean overButton(int x, int y, int width, int height) { if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } void serialEvent (Serial arduino) { inByte = arduino.readStringUntil(lf); // Receive one reading from Arduino if (inByte != null) { synchronized(a){ a = inByte; } println(a); } } leggendo anche un pò il codice di http://scuola.arduino.cc/en/content/how-create-graphical-interface-tinkerkit-gyro-modulecon risultato una schermata grigia senza nulla all'interno. In processing mi da questi errori: Exception in thread "Animation Thread" java.lang.NullPointerException at sketch_130203abbbbbb.draw(sketch_130203abbbbbb.java:107) at processing.core.PApplet.handleDraw(PApplet.java:2142) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193) at processing.core.PApplet.run(PApplet.java:2020) at java.lang.Thread.run(Thread.java:662) error, disabling serialEvent() for //./COM3 java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at processing.serial.Serial.serialEvent(Unknown Source) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575) Caused by: java.lang.NullPointerException at sketch_130203abbbbbb.serialEvent(sketch_130203abbbbbb.java:173) ... 8 more
(Guarda allegato)
|
|
|
|
« Last Edit: February 07, 2013, 02:16:45 pm by francescoprisco »
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8497
:(){:|:&};:
|
 |
« Reply #21 on: February 08, 2013, 08:18:34 am » |
ad occhio String a; in String a="nessun valore ricevuto per ora"; in processing le variabili GLOBALI non inizializzate sono "null" non sai se a verrà prima inizializzata dalla seriale o prima usata dalla grafica, e la grafica se prova a stanapare una stringa "null" va in errore (infatti hai un "null pointer exception")
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #22 on: February 08, 2013, 01:01:59 pm » |
Ho messo il codice come dicevi tu import processing.serial.*; int width = 800; int height = 600; String inByte = null; String a="nessun valore ricevuto per ora"; int lf = 10; int button1X; int button1Y; int button1Width = width/10; int button1Height = height/20; int button2X; int button2Y; int button3X; int button3Y; int button2Width = button1Width; int button2Height = button1Height; int button3Width = button1Width; int button3Height = button1Height; float buttonFont = button1Height/1.5; float wordFont = button1Height;
Serial arduino; color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false; boolean button2Over = false; boolean button3Over = false; void setup() { size(width,height); smooth(); font = loadFont("CourierNew36.vlw"); buttonColor = color(255); buttonHighlight = color(200); buttonPressed = color(50); button1X = width/10 - button1Width/2; button1Y = height/5 - button1Height/2; button2X = 2*width/10 - button1Width/2; button2Y = height/5 - button1Height/2; button3X = 3*width/10 - button1Width/2; button3Y = height/5 - button1Height/2; arduino = new Serial(this,"COM3", 9600); arduino.bufferUntil('\n'); // SerialEvent for new line arduino.clear(); }
void draw() { update(mouseX,mouseY); background(0); if(button1Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button1X,button1Y, button1Width, button1Height); if(button2Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button2X,button2Y, button2Width, button2Height); if(button3Over) { fill(buttonHighlight); } else { fill(buttonColor); } stroke(0); rect(button3X,button3Y, button3Width, button3Height); textFont(font, buttonFont); fill(0); text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3); text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3); text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
synchronized(a){ text(a,width/2, height/2); }
textFont(font, wordFont); fill(255); text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2); }
///////////////////////////////////
void update(int x, int y) { if(overButton(button1X,button1Y,button1Width,button1Height)){ button1Over = true; } else { button1Over = false; } if(overButton(button2X,button2Y,button2Width,button2Height)){ button2Over = true; } else { button2Over = false; } if(overButton(button3X,button3Y,button3Width,button3Height)){ button3Over = true; } else { button3Over = false; } }
void mousePressed() { if(button1Over) { println("Button1 pressed"); arduino.write('S'); fill(buttonPressed); rect(button1X,button1Y, button1Width, button1Height); } if(button2Over) { println("Button2 pressed"); arduino.write('N'); fill(buttonPressed); rect(button2X,button2Y, button2Width, button2Height); } if(button2Over) { println("Button3 pressed"); arduino.write('Z'); fill(buttonPressed); rect(button3X,button3Y, button3Width, button3Height); } }
boolean overButton(int x, int y, int width, int height) { if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } void serialEvent (Serial arduino) { inByte = arduino.readStringUntil(lf); // Receive one reading from Arduino if (inByte != null) { synchronized(a){ a = inByte; } println(a); } } In terminale seriale il valore viene visualizzato ma nella GUI no.Come devo fare? (Guarda immagine in allegato)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #23 on: February 09, 2013, 05:41:06 am » |
Sono riuscito a risolvere il problema usando questo codice qui // Example by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port PFont myFont; // The display font String inString; // Input string from serial port int lf = 10; // ASCII linefeed
void setup() { size(400,200); // Make your own font. It's fun! myFont = loadFont("CourierNew36.vlw"); textFont(myFont, 18); // List all the available serial ports: println(Serial.list()); // Open the port you are using at the rate you want: myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil(lf); }
void draw() { background(0); text("received: " + inString, 10,50); }
void serialEvent(Serial p) { inString = (myPort.readString()); } preso dalle guide di Processing disponibile al link http://www.processing.org/reference/libraries/serial/Serial_bufferUntil_.htmlOra quello che io mi chiedo, Mettiamo caso io debba prelevare + dati da + sensori... come organizzo quel codice?Lesto intanto ti ringrazio per l'aiuto e la tua disponibilità, ho già provveduto ad assegnarti il karma.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #24 on: February 10, 2013, 08:29:24 am » |
Nessuno sa aiutarmi?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8497
:(){:|:&};:
|
 |
« Reply #25 on: February 10, 2013, 03:54:05 pm » |
ora tu prendi una stringa di una riga a la stampi.
Ora, arduino potrebbe stampare tutti i valori su una riga e poi a fine loop andare a capo, così avetsri tutti i valori in una riga.
Poi volendo puoi dividere i valori con una virgola, o con una lettera, in modo che poi lato processing giocando con la stringa (un pò di cerca e taglia, vedi substring e indexOf della classe String), puoi anche estrarre i valori dei vari sensori per poterli visualizzare in posti differenti od animare qualche grafico etc..
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #26 on: February 10, 2013, 04:07:09 pm » |
potresti farmi un esempio
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #27 on: February 12, 2013, 08:16:32 am » |
nessuno sa farmi un esempio?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8497
:(){:|:&};:
|
 |
« Reply #28 on: February 12, 2013, 08:20:37 am » |
fai qualche prova da solo, e se non funziona posta il codice che hai usato, cosa ti apsettavi facesse, e cosa invece ha fatto.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 194
|
 |
« Reply #29 on: February 12, 2013, 09:17:01 am » |
Ho provato a modificare il codice così ma non so se e corretto ora non sono in condizione di poter testare. Arduino: void displayvalue(){ pass = digitalRead(A1); if(pass != 0) { car++; }
Serial.println(mph); Serial.print(","); Serial.println(car); delay(1000); } processing: void serialEvent(Serial p) { inString = (myPort.readString()); int sensors[] = int(split(inString, ',')); println(sensors[0]); println(sensors[1]); } Così dovrebbe funzionare?
|
|
|
|
|
Logged
|
|
|
|
|
|