crimson_arc
YaBB Newbies
Offline
Posts: 19
Arduino Ping to Processing
Mar 11th , 2009, 1:34am
Hey i am trying to hook up processing with an arduino ping sensor so i can control a the movement of ball within processing. I have the code from a tutorial site modified slightly and have no compiling errors.Both parts work fine on thier own but they will communicate with each other. http://webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/ultrasound.html Arduino code int ultraSoundSignal = 7; // Ultrasound signal pin int val = 0; int ultrasoundValue = 0; int timecount = 0; // Echo counter int ledPin = 13; // LED connected to digital pin 13 int prntInteger; void setup() { beginSerial(9600); // Sets the baud rate to 9600 pinMode(ledPin, OUTPUT); // Sets the digital pin as output } void loop() { timecount = 0; val = 0; pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output /* Send low-high-low pulse to activate the trigger pulse of the sensor * ------------------------------------------------------------------- */ digitalWrite(ultraSoundSignal, LOW); // Send low pulse delayMicroseconds(2); // Wait for 2 microseconds digitalWrite(ultraSoundSignal, HIGH); // Send high pulse delayMicroseconds(5); // Wait for 5 microseconds digitalWrite(ultraSoundSignal, LOW); // Holdoff /* Listening for echo pulse * ------------------------------------------------------------------- */ pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input val = digitalRead(ultraSoundSignal); // Append signal value to val while(val == LOW) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); } while(val == HIGH) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); timecount = timecount +1; // Count echo pulse time } /* Writing out values to the serial port * ------------------------------------------------------------------- */ ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue serialWrite('A'); // Example identifier for the sensor Serial.print(ultrasoundValue); serialWrite(10); serialWrite(13); /* Lite up LED if any value is passed by the echo pulse * ------------------------------------------------------------------- */ if(timecount > 0){ digitalWrite(ledPin, HIGH); } /* Delay of program * ------------------------------------------------------------------- */ delay(100); } Processing code import processing.serial.*; DisplayItems di; // backgroundcolor, grid, etc. are controlled in the DisplayItems object // width and height should be set here int xWidth = 600; int yHeight = 600; // set framerate int fr = 24; // set up the display items you want by choosing true boolean bck = true; boolean grid = true; boolean g_vert = true; boolean g_horiz = true; boolean g_values = true; boolean output = true; // these variables are for the serial port connection object Serial port; String portname = "COM5"; // find the name of your serial port in your system setup! int baudrate = 9600; // set baudrate here int value; // variables used to store value from serial port String buf=""; // String buffer to store serial values int value1; // value1 is the read value // pressing these keys you can toogle on/off display items void keyPressed(){ if (key == 'b' || key == 'B') bck=!bck; if (key == 'g' || key == 'G') grid=!grid; if (key == 'v' || key == 'V') g_values=!g_values; if (key == 'o' || key == 'O') output=!output; } // the serial event function takes the value of the event and store it in the corresponding variable void serialEvent(int serial){ if(serial!=10) { buf += char(serial); } else { //extract the value from the string 'buf' buf = buf.substring(1,buf.length()); //cast the value to an integer value1 = int(buf); buf=""; } } // setup initializes displayItems and serial port objects void setup(){ size(xWidth, yHeight); frameRate(fr); di = new DisplayItems(); port = new Serial(this, portname, baudrate); println(port); } void createCircle(){ //Adding constraints to keep the circle within the framesize if(value1 > width){ value1 = width; } //Draw the circle ellipse(width/2, height/2, value1, value1); } // draw listens to serial port, draw void draw(){ while(port.available() > 0){ value = port.read(); serialEvent(value); } di.drawBack(); noStroke(); fill(0, 0, 255); createCircle(); di.drawItems(); if(output) println("Distance: "+value1); delay(100); } Left out the display class but you can find all the code up on that site. Any help would be much appriciated