Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- Serial port;
- int val; // Ovviamente fa un po' cacare, ma funziona
- int x=100, y=100;
- void setup()
- {
- size(200, 200);
- println(Serial.list());
- println("totale porte " + Serial.list().length);
- String portName = Serial.list()[findport(0)];
- port = new Serial(this, portName, 9600);
- port.write('@');
- port.clear();
- }
- void draw()
- {
- if ( port.available() > 0) { // If data is available,
- val = port.read(); // read it and store it in val
- }
- background(255);
- if (val == 42) { // 42 -> *
- fill(0);
- }
- else {
- fill(204);
- }
- rect(180, 10, 10, 10);
- fill(200);
- ellipse(x, y, 20, 20);
- }
- void keyPressed() {
- if (key=='a') x=x+2;
- if (key=='s') x=x-2;
- if (key=='z') y=y+2;
- if (key=='x') y=y-2;
- if (key=='l') System.exit(0);
- }
- int findport(int a) {
- Serial porttemp;
- int nport=-1;
- for (int i = a; i < Serial.list().length; i++) {
- println("Ciclo " + i +" "+ Serial.list()[i]);
- try {
- porttemp = new Serial (this, Serial.list()[i], 9600);
- }
- catch(Exception e ) {
- return findport(i+1);
- }
- porttemp.clear();
- delay(50); // giusto il tempo per aprire la porta, altirmenti trova -1
- if (porttemp.read() == 35) { // 35 -> #
- nport=i;
- println("Arduino trovato sulla porta " + nport + " in " + millis() + "ms");
- i=Serial.list().length;
- }
- porttemp.clear();
- porttemp.stop();
- porttemp = null;
- }
- if(nport==-1){
- System.err.println("\n\tNessun Ardunino trovato");
- System.exit(0);
- }
- return nport;
- }
- //Simple wire code for Arduino
- /*
- void setup() {
- Serial.begin(9600); // Start serial communication at 9600 bps
- while(Serial.read()!=64){
- Serial.write("#");
- }
- Serial.write("\n\n Online..");
- }
- void loop() {
- Serial.write('*');
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement