Advertisement
Fede

abbozzo

Mar 4th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. Serial port;  
  4. int val;           // Ovviamente fa un po' cacare, ma funziona
  5.  
  6. int x=100, y=100;
  7.  
  8. void setup()
  9. {
  10.   size(200, 200);
  11.  
  12.   println(Serial.list());
  13.   println("totale porte " + Serial.list().length);
  14.  
  15.   String portName = Serial.list()[findport(0)];
  16.   port = new Serial(this, portName, 9600);
  17.   port.write('@');
  18.   port.clear();
  19. }
  20.  
  21. void draw()
  22. {
  23.   if ( port.available() > 0) {  // If data is available,
  24.     val = port.read();         // read it and store it in val
  25.   }
  26.   background(255);            
  27.   if (val == 42) {  // 42 -> *            
  28.     fill(0);
  29.   }
  30.   else {                      
  31.     fill(204);
  32.   }
  33.   rect(180, 10, 10, 10);
  34.   fill(200);
  35.   ellipse(x, y, 20, 20);
  36. }
  37.  
  38. void keyPressed() {
  39.   if (key=='a') x=x+2;
  40.   if (key=='s') x=x-2;
  41.   if (key=='z') y=y+2;
  42.   if (key=='x') y=y-2;
  43.   if (key=='l') System.exit(0);
  44. }
  45.  
  46.  
  47.  
  48. int findport(int a) {  
  49.   Serial porttemp;
  50.   int nport=-1;
  51.   for (int i = a; i < Serial.list().length; i++) {
  52.  
  53.     println("Ciclo " + i +" "+  Serial.list()[i]);
  54.  
  55.     try {
  56.       porttemp = new Serial (this, Serial.list()[i], 9600);
  57.     }
  58.     catch(Exception e ) {
  59.       return findport(i+1);
  60.     }
  61.  
  62.     porttemp.clear();
  63.     delay(50); // giusto il tempo per aprire la porta, altirmenti trova -1
  64.  
  65.     if (porttemp.read() == 35) { // 35 -> #
  66.       nport=i;
  67.       println("Arduino trovato sulla porta " + nport + " in " + millis() + "ms");
  68.       i=Serial.list().length;
  69.     }
  70.  
  71.     porttemp.clear();
  72.     porttemp.stop();
  73.     porttemp = null;
  74.   }
  75.   if(nport==-1){
  76.     System.err.println("\n\tNessun Ardunino trovato");
  77.     System.exit(0);
  78.   }
  79.   return nport;
  80. }
  81.  
  82.  
  83.  
  84. //Simple wire code for Arduino
  85.  
  86. /*
  87. void setup() {
  88.   Serial.begin(9600);  // Start serial communication at 9600 bps
  89.   while(Serial.read()!=64){
  90.     Serial.write("#");
  91.   }
  92.   Serial.write("\n\n Online..");
  93. }
  94.  
  95. void loop() {
  96.   Serial.write('*');
  97. }
  98.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement