Advertisement
Guest User

ProcessingSerialWaitTest

a guest
Feb 24th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. Serial serial;
  4.  
  5. void setup(){
  6.    println("Starting up!");
  7.    serial = new Serial(this, "COM17", 9600); //remember to set this to the proper port
  8.    serial.clear(); //this is necessary because the arduino keeps sending 42 even before we connect, when we connect the arduino gets reset but since we still see some 42s in our buffer we send it characters to echo too soon (before it reboots) and we will wait forever for it to reply    
  9.    println("Waiting for arduino");
  10.    long startWaiting = millis();
  11.    while(serial.read()!=42){
  12.      delay(50);
  13.      print('.');
  14.    } //wait for the arduino to start sending 42 after reset, the delay is there so we don't use 100% CPU
  15.    println("\nGot a message from the arduino after "+(millis()-startWaiting) + " milliseconds");
  16.    serial.write(1); //send something to the arduino so it knows that communication has been established
  17.    serial.clear(); //clear the buffer just in case there was more that one byte in the buffer
  18.    delay(200); //wait for the arduino to get our message and get to the echo loop
  19. }
  20.  
  21. void waitUntil(Serial port, int byteValue){
  22.  
  23.   if (byteValue>255) throw new IllegalArgumentException("byte value out of range 0-255");
  24.  
  25.   int lastValue = -1;
  26.  
  27.   while (lastValue!=byteValue) {
  28.       if(port.available()>0) {
  29.            lastValue=port.read();
  30.       }
  31.       delay(5);// I added this delay so the sketch doesn't use 100% CPU but it's not essential
  32.   }
  33. }
  34.  
  35. void draw() {
  36.  
  37.   for (int i=50;i<250;i+=5) {
  38.     serial.write(i);
  39.     println("Sending "+i);
  40.    
  41.     waitUntil(serial, i);
  42.     println("Received confirmation for "+i);
  43.    
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement