Sending sensory data to Processing

Hi all,

I am trying to get a visual display (similar to a radar sweep) from an SRF05 ultransonic sensor mounted on a servo that I have set up to sweep 0-180 degrees taking one reading every degree. I want to send two values to processing, one for the degrees of the servo and one for the measured SRF05 distance. In processing I simply want the length of the line in the radar sweep to relate to the position of the servo in degrees and the measured distance.
I have looked at

http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-to-visualise-sensor-data-from-srf-05-part-2-visualising-the-data/

which is awesome but i don’t require that kind of detail and it is difficult to relate the values he has brought across from Arduino to the ones I need to.

my Arduino code for sweeping the servo and reading the SRF05 is:

#include <Servo.h>           // include the standard servo library

Servo servopos;               // servo position
int servoangle = 0;           // for loop servo position
int echoPin = 2;              // the SRF05's echo pin
int initPin = 3;              // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;   // variable for storing distance
 
void setup() 
{
  servopos.attach(7); // servo pin
  pinMode(initPin, OUTPUT); //SRF05 initialisation pin set to output
  pinMode(echoPin, INPUT); //SRF05 echoPin set to INPUT
  Serial.begin(9600); //initialise the serial port
}
 
void loop() 
{
     for(servoangle = 30; servoangle < 150; servoangle++) //sweep servo from left to right
      {
        servopos.write(servoangle);
        digitalWrite(initPin, HIGH);            // send signal from SRF05
        delayMicroseconds(10);                  // wait 10 microseconds for it to return
        digitalWrite(initPin, LOW);             // stop signal
        pulseTime = pulseIn(echoPin, HIGH);     // calculate time for signal to return
        distance = pulseTime/58;                // convert to centimetres
        delay(10);
        Serial.print("D");                      //print D for "degrees" so recognise the following value
        Serial.print(servoangle);               // current servo position
        Serial.print("M");                      //print M for "measurement"
        Serial.println(distance);                                   // average of sensor readings
  
     }
    
    for(servoangle = 150; servoangle >=30 ; servoangle--) // servo sweep from right to left
     {
      servopos.write(servoangle);
      digitalWrite(initPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(initPin, LOW);
      pulseTime = pulseIn(echoPin, HIGH);
      distance = pulseTime/58;
      delay(10);
      Serial.print("D");
      Serial.print(servoangle);
      Serial.print("M");
      Serial.println(distance);
    }
   
}

and the prcessing code for the radar type display is:

//SONAR sweep program for mapping
//Author: Kerim Morris & Sam Hughes
//Date Created: 24th February 2011


//Program Variables
float xcoord, ycoord, xcoord2, ycoord2; //x and y coordinates
int angle;
int angle2; 
float radius = 500; 
float hyp = 500; // sweep arm length
float frequency = 1; //speed of sweep
int sweep = 0; //sweep direction variable


PFont myFont;// used to create font

void setup(){
  size(1000, 500); //window size
  myFont = createFont("verdana", 20); // generate processing font from system font
  textFont(myFont);
}

void draw()
  {
  background (0); //window background colour
  noStroke();
  fill(255);
  smooth();  
  ellipse(width/2, 500, radius*2, radius*2);
  stroke(180);
  fill(250);
  smooth();
  ellipse(width/2, 500, (radius*2)-100, (radius*2)-100);
  stroke(180);
  fill(245);
  smooth();
  ellipse(width/2, 500, (radius*2)-200, (radius*2)-200);
  stroke(180);
  fill(240);
  smooth();
  ellipse(width/2, 500, (radius*2)-300, (radius*2)-300);
  stroke(180);
  fill(235);
  smooth();
  ellipse(width/2, 500, (radius*2)-400, (radius*2)-400);
  stroke(180);
  fill(230);
  smooth();
  ellipse(width/2, 500, (radius*2)-500, (radius*2)-500);
  stroke(180);
  fill(225);
  smooth();
  ellipse(width/2, 500, (radius*2)-600, (radius*2)-600);
  stroke(180);
  fill(225);
  smooth();
  ellipse(width/2, 500, (radius*2)-700, (radius*2)-700);
  stroke(180);
  fill(220);
  smooth();
  ellipse(width/2, 500, (radius*2)-800, (radius*2)-800);
  stroke(180);
  fill(215);
  smooth();
  ellipse(width/2, 500, (radius*2)-900, (radius*2)-900);
  stroke(180);
  line(width/2, 500, 500, 0);
  stroke(180);
  // rotates rectangle around circle
  xcoord = radius + cos(radians(angle))*hyp;
  ycoord = radius + sin(radians(angle))*hyp;
  stroke(1);
  line(width/2, 500, xcoord, ycoord);
  // keep reinitializing to 0, to avoid
  // flashing during redrawing
  angle2 = 0;
  if(sweep == 0)
  {
  angle -= frequency;
  }
  else if(sweep == 1)
  {
   angle += frequency;
  }
  
  if(angle == 0)
  {
    sweep = 0;
  }
  else if(angle == -180)
  {
    sweep = 1;
  } 
        

  text("Arduino Buggy", 20, 30); 
  text("Sweep Angle = "+angle, 790, 30);
  text("SONAR", 20, 50);
  text("DISPLAY", 20, 70);
  text("x = "+xcoord, 790, 55);
  text("y = "+ycoord, 790, 75);
  
}

I feel as though I am almost there but can’t seem to get the last bit working.
Any help would be much appreciated.
:frowning:

I feel as though I am almost there but can't seem to get the last bit working.

What "last bit" is that?

Hey PaulS, thanks for the reply

The last bit refers to getting the sensory data over to processing and incoporating the values to replace the "angle" of the line with the servo degrees value and the "hyp" value in the coordinates formula for x and y with the SRF05 distance measurement.

How do I send values from Arduino to Processing?

How do I send values from Arduino to Processing?

Using Serial.print() and Serial.println() just like you are doing.

The "problem" is that your Processing code is not opening the serial port, and reading anyhting from it.

Hey thanks PaulS,

Stupid mistake I know, its in there now.

Cheers