Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- Serial serial;
- void setup(){
- println("Starting up!");
- serial = new Serial(this, "COM17", 9600); //remember to set this to the proper port
- 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
- println("Waiting for arduino");
- long startWaiting = millis();
- while(serial.read()!=42){
- delay(50);
- print('.');
- } //wait for the arduino to start sending 42 after reset, the delay is there so we don't use 100% CPU
- println("\nGot a message from the arduino after "+(millis()-startWaiting) + " milliseconds");
- serial.write(1); //send something to the arduino so it knows that communication has been established
- serial.clear(); //clear the buffer just in case there was more that one byte in the buffer
- delay(200); //wait for the arduino to get our message and get to the echo loop
- }
- void waitUntil(Serial port, int byteValue){
- if (byteValue>255) throw new IllegalArgumentException("byte value out of range 0-255");
- int lastValue = -1;
- while (lastValue!=byteValue) {
- if(port.available()>0) {
- lastValue=port.read();
- }
- delay(5);// I added this delay so the sketch doesn't use 100% CPU but it's not essential
- }
- }
- void draw() {
- for (int i=50;i<250;i+=5) {
- serial.write(i);
- println("Sending "+i);
- waitUntil(serial, i);
- println("Received confirmation for "+i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement