Go Down

Topic: comunicating with processing (Read 5735 times) previous topic - next topic

Twogan

Feb 23, 2011, 10:06 pm Last Edit: Feb 25, 2011, 09:14 pm by Twogan Reason: 1
hello all,

I have an srf 05 ultrasound sensor and arduino.

I have hooked it all up, uploaded the relevant stuff and If I open serial monitor I am getting good, acurate readings. Nice.

now, I want to take this number ( from the serial monitor in arduino - the distance reading) and use it in processing, perhaps , say to control the size of a ellipse.

what is the best way forward?

Thank you
Twogan




PaulS

Quote
what is the best way forward?

Learn to use capital letters when  appropriate.

Your next step is to develop an application in processing to read the serial data. Oh, wait, there are several examples that ship with Processing to do that.

I guess your next step is to develop an application in Processing to draw ellipses, using the data you get from the serial port. Since an ellipse has two radii, a center point, and an angle that defines the orientation of the ellipse, which is 6 values, what exactly the single value from the sensor is going to control is not clear.

Twogan

#2
Feb 24, 2011, 12:14 pm Last Edit: Feb 25, 2011, 06:07 pm by Twogan Reason: 1
I find it so disappointing, that time and time again I read replies to honest posts from people starting out in the world of Arduino and programming, only to see a cutting, snide response like the one from Shannon here.

such a shame that you would rather point out my poor literacy skills than help me with my Arduino skills.

And - whats all this - 'Oh, wait' business ?? its just sarcastic and rude.

I have tried the examples and got nowhere. I decided not to list the examples I have tried and I decided not to list the various ways in which I didn't get a results that I wanted.

No, instead I thought I would write an open request for help, in the hope that some kind soul would point me in the right direction. Then perhaps a conversation could take place where I could expand on my explanations, in perhaps an atmosphere of support and learning. silly me to have been so naive.

Perhaps with the ellipse I would convert my incoming number to an integer and use that to effect the drawing of the ellipse. Perhaps you have a better idea ? well we will never know because you are too busy pointing out your own superiority to help.

ok rant over.

I have this example from processing - file - examples - library - serial - simpleread ( sorry I dont know how to do the Quote thing so it comes in a box, please let me know where to find out ***UPDATE - have done this now):
Code: [Select]

/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
 size(200, 200);
 // I know that the first port in the serial list on my mac
 // is always my  FTDI adaptor, so I open Serial.list()[0].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
}

void draw()
{
 if ( myPort.available() > 0) {  // If data is available,
   val = myPort.read();         // read it and store it in val
 }
 background(255);             // Set background to white
 if (val == 0) {              // If the serial value is 0,
   fill(0);                   // set fill to black
 }
 else {                       // If the serial value is not 0,
   fill(204);                 // set fill to light gray
 }
 rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
 pinMode(switchPin, INPUT);             // Set pin 0 as an input
 Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
 if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
   Serial.print(1, BYTE);               // send 1 to Processing
 } else {                               // If the switch is not ON,
   Serial.print(0, BYTE);               // send 0 to Processing
 }
 delay(100);                            // Wait 100 milliseconds
}

*/

***

firstly - I don't understand how to choose the right port-
"Open whatever port is the one you're using." - I know I'm using port COM32 for arduino but how do I tell processing that ?

Secondly - I'm not using this arduino sketch ( as I have an SRF 05 ) so will it work for me ?


PLEASE help, I am willing to put the time into research these things, I have just hit a wall in terms of Arduino to processing communication and I really want to move on.

Thank you people

Peace and love
Twogan


PaulS

Quote
I have tried the examples and got nowhere. I decided not to list the examples I have tried and I decided not to list the various ways in which I didn't get a results that I wanted.

It would have made a big difference in my reply if you had pointed this out initially. Since you didn't, I presumed that you hadn't bothered.

Had you pointed this out, I would have been happy to work with you to figure out why the examples hadn't worked.

If you can't get the simple examples to work, I'm not sure how you expect to get a more complicated example to work.

If I unduly ruffled your feathers, I apologize.

If you'd like to determine why the examples didn't work, I'd be willing to help you with that,

PaulS

Quote
sorry I dont know how to do the Quote thing so it comes in a box, please let me know where to find out

On the second line of icons above the input box, there is a button with a # on it, for posting code, and one next to that for quotes.

Select the buttons before pasting code or quote, or paste the code or quote first, then select the code or quote, and press the button.

You can even modify a post to add the tags. Please do so.

PaulS

Quote
firstly - I don't understand how to choose the right port-
"Open whatever port is the one you're using." - I know I'm using port COM32 for arduino but how do I tell processing that ?

In the code, there is this comment and command:
Code: [Select]
 // I know that the first port in the serial list on my mac
 // is always my  FTDI adaptor, so I open Serial.list()[0].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);

This code selects the first port in the list returned by Serial.list() (the [ 0 ] element).

It seems safe to assume that the reason the example does not work for you is because the first port in your list is NOT the one that the Arduino is connected to.

You can print the list of serial ports that Processing knows about. Add
Code: [Select]
println(Serial.list());
after the comments above, before the String variable statement.

Run the program, and look at the bottom of the Processing window. The list of ports will be printed there.

Stop the program, and change the value in the [] to the correct position of your port in the list.

PaulS

Quote
Secondly - I'm not using this arduino sketch ( as I have an SRF 05 ) so will it work for me ?

If your Arduino sketch sends a single value to the serial port, in the same way that the example Arduino sketch does, yes, it will.

Keep in mind that this is simply a test of the ability of the Arduino and Processing to communicate, and don't expect that the Processing sketch will necessarily do exactly what it was intended to do with data from a different sketch. It will do something, which should prove that communication is possible.

Then, you can change the Processing program to do what you want it to do, with data in the format that you are sending.

Twogan

Firstly, thank you very much for your constructive replies. I very much appreciate you taking the time to help me on this.

I am going to try to get this working using the advice you have given me. I will report back here ASAP.

But just one thing. You say -
Quote
If you can't get the simple examples to work, I'm not sure how you expect to get a more complicated example to work


Thats a harsh but probably fair point. However, perhaps I am missing something - the example I have shown is the most simple I have found in the processing examples. I don't want to over-complicate. Is there a more simple example sketch of Arduino talking to Processing that I have missed?

Many Thanks

Twogan

I have successfully managed to write my srf 05 arduino output to a graph in processing. I'm very happy. Thank you PaulS.

for reference here is the Arduino SRF 05 ultrasound sensor sketch:
Code: [Select]
int duration;                          // Stores duratiuon of pulse in
int distance;                          // Stores distance
int srfPin = 2;                        // Pin for SRF05

void setup(){
Serial.begin(9600);
}

void loop(){
  pinMode(srfPin, OUTPUT);
  digitalWrite(srfPin, LOW);           // Make sure pin is low before sending a short high to trigger ranging
  delayMicroseconds(2);
  digitalWrite(srfPin, HIGH);          // Send a short 10 microsecond high burst on pin to start ranging
  delayMicroseconds(10);
  digitalWrite(srfPin, LOW);           // Send pin low again before waiting for pulse back in
  pinMode(srfPin, INPUT);
  duration = pulseIn(srfPin, HIGH);    // Reads echo pulse in from SRF05 in micro seconds
  distance = duration/58;              // Dividing this by 58 gives us a distance in cm
  Serial.println(distance);
  delay(50);                           // Wait before looping to do it again
}


PaulS

Quote
However, perhaps I am missing something - the example I have shown is the most simple I have found in the processing examples.

There is a level of complexity in getting any two applications to talk to each other. Involving the serial port ups that level of complexity. The example you showed is about as simple as it gets. The only thing you should need to do is plug the Arduino in, select the proper com port, and upload the sketch.

Then start Processing, load the sketch, make sure it is selecting the same port, and watch data stream back and forth.

When that works, you will be able to extend the Arduino sketch and Processing application to send the data that YOU want to Processing, and make Processing do something useful with that data.

I didn't mean to be harsh, but you need to get the simplest example going first. Then, you will understand (better) what each sketch is doing. At that time, you can begin to increase the complexity of the data exchange and the use of that data on either end. All that I was trying to convey was that you must get the example to work, first, before you can use it as a building block for a more complex application.

Twogan

OK yes I understand what you mean now.

I have seen the Arduino SRF05 data in Processing and now it is time to do something with it. In my original post I was trying to keep things simple so I said I was looking to change the size of an ellipse with the value coming from my sensor. However in truth my goal is to control the amplitude of an audio oscillator via the proximity sensor. Its an integral aspect of an art installation I am working on. I think I underestimated the difficulty of this task.

I have downloaded the minim library. I am thinking of trying to use my serial port value to control the amplitude of a sine wave oscillator at a fixed frequency. Any tips much appreciated...

Twogan

I have taken the Graph sketch from the Arduino Examples - Communication - Graph and added in the minim library and now have srf05 serial output represented visually and audibly.

I list the code here in case it is of any use to others. Any comments gratefully accepted...
Code: [Select]
// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return

// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.
import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;
import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {
// set the window size:
size(400, 300);       
  minim = new Minim(this);
  // get a line out from Minim, default bufferSize is 1024, default sample rate is 44100, bit depth is 16
  out = minim.getLineOut(Minim.STEREO);
  // create a sine wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate from line out
  sine = new SineWave(440, 0.5, out.sampleRate());
  // set the portamento speed on the oscillator to 200 milliseconds
  sine.portamento(200);
  // add the oscillator to the line out
  out.addSignal(sine);
// List all the available serial ports
//println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[4], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);

inByte = map(inByte, 0, 1023, 0, height);

// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte*6);


// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;

float amp = (inByte/100);
sine.setAmp(amp); }
}
}


Thanks
Twogan

PaulS

It's great that you were able to get Processing to do what you wanted. I hope that, now, you can see why getting the basic example to work was so important. Modifying the Processing sketch to achieve the exact results you wanted took far less time than getting the example to work. Jumping in and building the resultant Processing sketch first would have entailed endless hours of frustration getting the two devices/applications to talk to each other.

Getting the talking squared away first made the actual use of the communicated data relatively easy.

I'm very happy that you persisted, and achieved your goal.

Go Up