[PD] Arduino, Pd and ultrasonics: HELP needed for degree project!

Chris Jack itsmrjack at gmail.com
Mon Sep 27 19:59:10 CEST 2010


Dear Pd'ers… I'm fairly new to Pd and have been using it during my 'Audio
Production' degree.  I'm here to ask for help on my final degree project,
which aims to control objects within Pd by way of proximity sensors
connected to Arduino.

I have had moderate success, first using a Sharp GPD12 IR sensor connected
to an analog port on an Arduino Diecimila, then a Devantech SRF02 connected
via I2C to Arduino. I have managed to get Pure Data reading the sensor
values using the Pure Data example included in SimpleMessageSystem (
http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip) ...
and only altering the number of the comport the Arduino appears on.

[image: SMS pic.jpg]


On the Arduino end, I have added the SimpleMessageSystem library, and am
using the following sketch to run one or both of the sensors depending on
whether or not  I omit the SRF02 or GPD12 parts of the code.


// top //

#include <Wire.h>

#include <Servo.h>

#include <SimpleMessageSystem.h>


int sensorPin = 0;

int sensorValue = 0;

Servo servo1;


#define sensorAddress 0x70

#define readInches 0x50

#define readCentimeters 0x51

#define readMicroseconds 0x52

#define resultRegister 0x02


void setup()

{

  Wire.begin();

  Serial.begin(9600);

}



 void sendCommand (int address, int command) {

   Wire.beginTransmission(address);

   Wire.send(0x00);

   Wire.send(command);

   Wire.endTransmission();

 }



void setRegister(int address, int thisRegister) {

   Wire.beginTransmission(address);

   Wire.send(thisRegister);

      Wire.endTransmission();

 }

 int readData(int address, int numBytes) {



   int result = 0;



   Wire.requestFrom(address, numBytes);

   while (Wire.available() < 2 );{

 }



 result = Wire.receive() * 256;

 result = result + Wire.receive();

 return result;

 }


void loop()

{

  // SRF02 READING //

  sendCommand(sensorAddress, readInches);

  delay(70);

  setRegister(sensorAddress, resultRegister);

  int sensorReading = readData(sensorAddress, 2);

 Serial.println(sensorReading);


  delay (200);



  // GPD12 READING //

  sensorValue = analogRead(sensorPin);

  int range = (6787 / sensorValue);

  Serial.println(range);



  delay(200);

}


// bottom //


Pure data is picking up the sensor's output as a decimal number, through the
serial port, correctly, and showing this as a number in the SPECIAL
CHARACTER (note the '80' value in the Pd patch pic that my screen shot
caught) section of the Pd patch, which I can then use to control various Pd
parameters, like a slider for example.

If printing both sensor readings to the serial, the Pd patch will pick up
the two sensor values alternately, but there seems no way of differentiating
the two sensors within Pd, as they both come through the serial. I have
tried prepending the serial print at the Arduino stage with a tag such as
"IR" or "UL" for each sensor, but this simply ends up in nothing coming
through in Pd.

Using one sensor gives the ability to control parameters with a fairly
narrow detection range, but for the final version I would like to
incorporate 4 or 5 sensors in order that I can cover nearly a full 360
degree range.

After a bit of research I have gone and bought 4 x Maxsonar EZ0's. They've
got a wider range than the Devantech sensors, and can operate via I2C,
serial or PWM. A number of people online seem to mention the improved
stability when operating via PWM, so I thought this could be kinda useful.

I am going to London Hackspace tomorrow to get help with wiring up the
sensors to my Arduino Diecimila, but for now, my main problem seems to be
how to achieve greater communication between Pure Data and the sensors. It's
all very good and well being able to take the decimal readout from an analog
input to give one set of values in Pd, but I would like to know how to
either to read (and trigger) each sensor discretely via PWM, or to somehow
differentiate between each sensor's analog output, so that I can have the
different sensors controlling different objects within Pd.

As well as SimpleMessageSystem, I looked at using the Pduino object. But to
be honest, it is either not working properly (I have noticed reported issues
with bugginess), or I am being stupid, as it has totally boggled my brain.
In theory though, it seems like it should be able to do what I want, ie.
send and receive commands between Arduino and Pd.

So…. my questions for anyone out there with a knowledge of using Arduino +
multiple sensors (preferably Maxsonar EZ's) with Pd are:

What is most appropriate for my project?

- Simple Message System or Pduino?

- analog or PWM?

And how do I get proper communication between the two platforms so I can
discern discrete values from each sensor?

I'd really like to get a discussion going with this as (a) it would really
help me in my degree (which ends in about 4 weeks!!!) and (b) I really want
to share what I am doing, especially as the discussions on Arduino/Pd
communication on the various forums seem a little patchy (at least for noobs
like me). I also hope to continue my research in the future, sharing any
findings I make with the community…


PS. In the sharing spirit, please check out the Radio Tuner patch I've just
posted over  at http://puredata.hurleur.com/sujet-4682-radio-tuner ...it's
my first successful attempt at Pd... I hope you enjoy :)

Many thanks
-- 
Mr Jack

Discus Jockey
Audio Tweeker
Musical Protagonist

itsmrjack at gmail.com
http://soundcloud.com/mrjack
http://www.myspace.com/itsmrjack

http://earshaveeyes.blogspot.com
http://campari0safari.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20100927/48ae1691/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 94224 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20100927/48ae1691/attachment-0001.jpeg>


More information about the Pd-list mailing list