We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Ping Sensors (Read 1476 times)
Ping Sensors
Mar 23rd, 2009, 4:18pm
 
Hey I am working on a project for college and am having touble getting ping sensors to talk to processing. What i am trying to do is
1. When a user steps on a hot spot a default value is taken and saved and a camera starts to stream video.
2. As the user moves left/right a value is taken in and an objct is moved in processing relative to the default position.

I have only been able to get a simple push button talking to arduino. when i run the ultrasound example for some reason the arduino part is working but processing will not pick up the values - Distance 0  - is all that comes up. Am working on it for the next few days so will post up code soon. Any help would be much appreciated.

All the best
Re: Ping Sensors
Reply #1 - Apr 22nd, 2009, 7:29am
 
Heres the first bit of code  Anyone got any ideas how to take the two ping outputs, convert to a useable number and have them move the  2 bubbles along the x axis.There will be one for each user - so maybe set a default position for each user and calculate distance from that.
Code:

   import processing.serial.*;
   Serial port; // The serial port
   int[] serialInArray = new int[1]; // Where we’ll put what we receive
   int serialCount = 0; // A count of how many bytes we receive
  // int xpos, ypos; // Starting position of the ball
   boolean firstContact = false; // Whether we’ve heard from the
   int speed = 15;
   float xpos;  // variables
   float ypos;
   float xspeed;
   
   
   void setup() {
   size(256, 256); // Stage size
   noStroke(); // No border on the next thing drawn
   
   // Set the starting position of the ball (middle of the stage)
   xpos = width/2;
   ypos = height/2;
   
   // Print a list of the serial ports, for debugging purposes:
   println(Serial.list());
   
   // I know that the first port in the serial list on my mac
   // is always my Keyspan adaptor, so I open Serial.list()[0].
   // On Windows machines, this generally opens COM1.
   // Open whatever port is the one you’re using.
   port = new Serial(this, Serial.list()[0], 9600);
   port.write(65); // Send a capital A to start the microcontroller
   
   }
   
   void draw() {
   background(0);
   //fill(fgcolor);
   fill(255);
   // Draw the shape
   ellipse(xpos, ypos, 20, 20);
   
   // If no serial data has beeen received, send again until we get some.
   // (in case you tend to start Processing before you start your
   // external device):
   if (firstContact == false) {
   delay(300);
   port.write(65);
   }
   }
   
   void serialEvent(Serial port) {
   // if this is the first byte received,
   // take note of that fact:
   if (firstContact == false) {
   firstContact = true;
   }
   // Add the latest byte from the serial port to array:
   serialInArray[serialCount] = port.read();
   serialCount++;
   
   // If we have 3 bytes:
   if (serialCount < 35 ) {
   xpos = serialInArray[0];
   xpos++;
   //else (serialCount <
   //ypos = serialInArray[1];
   //fgcolor = serialInArray[2];
   
   // print the values (for debugging purposes only):
   //println(xpos + “\t” + ypos + “\t” + fgcolor);
   //println(xpos + “\t” + ypos);
   
   // Send a capital A to request new sensor readings:
   port.write(65);
   // Reset serialCount:
   serialCount = 0;
   }
   }
Re: Ping Sensors
Reply #2 - Apr 24th, 2009, 6:05pm
 
i'm have trouble getting the serial information to move the bubbles in the sketch. I've modifed a sketch from the making things talk book but can't seem to get the image in and moving anyone got any ideas?

Code:
import processing.serial.*;

int linefeed = 10; // linefeed in ASCII
Serial myPort;    // The serial port
int sensorValue = 0;    // the value from the sensor
int bubblePosition = 100;  // the horizontal position of the latest
PImage cow;   // line to be drawn on the graph

int prevSensorValue = 0;    // the previous sensor reading
boolean fullOfLife = false;  
int threshold = 1;  

int xDirection = 1;
int xPos;


void setup() {

 size(600, 600);

 println("Available serial ports:");
 println(Serial.list());

 // If you know the name of the port used by the Arduino board, you
 // can specify it directly like this.
 myPort = new Serial(this, "COM4", 9600);
 myPort.bufferUntil(linefeed);
}

void draw() {

 if (sensorValue > threshold ) {
   
   if (prevSensorValue <= threshold) {
delay(100);
if (sensorValue > threshold) {
 fullOfLife = true;
 //sendMail();
}
   }
 }
 else {

   if (prevSensorValue >= threshold) {
fullOfLife = true;
   }
 }
 
 prevSensorValue = sensorValue;
}

void serialEvent(Serial myPort) {
 // read the serial buffer:
 String myString = myPort.readStringUntil(linefeed);
 // if you got any bytes other than the linefeed:
 if (myString != null) {
   //trim off the carriage return and convert the string to an integer:
   sensorValue = int(trim(myString));
   // print it:
   // println(sensorValue);
   drawBubble();
 }
}

void drawBubble() {

 cow = loadImage("bubble.png");

 int bubbleHeight = sensorValue * 2;
 // draw the line:
 if (sensorValue >= threshold) {
   xDirection = +xDirection;    
 }

 else {

   if (sensorValue <= threshold) {
xDirection = -xDirection;
   }
 }
}

Page Index Toggle Pages: 1