Go Down

Topic: Problem to get int form arduino into processing (Read 1 time) previous topic - next topic

c.kazah

Hello, I have set up an SRF05 range finder (http://www.robot-electronics.co.uk/htm/srf05tech.htm) with my arduino dicemilia, and got it up and running just fine with this sketch for the arduino:


Code: [Select]

/*
 Ultrasonic Sensor sketch

 This program reads a Devantech SRF04 ultrasonic distance sensor
 The SRF04 sensor's pins are connected as described below.
 Created 9 November 2006
 By Tom Igoe and Neilson Abeel
*/

#define echoPin 2             // the SRF04's echo pin
#define initPin 3             // the SRF04's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse

void setup() {
 // make the init pin an output:
 pinMode(initPin, OUTPUT);
 // make the echo pin an input:
 pinMode(echoPin, INPUT);
 // initialize the serial port:
 Serial.begin(9600);
}

void loop() {
 // send the sensor a 10microsecond pulse:
 digitalWrite(initPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(initPin, LOW);

 // wait for the pulse to return. The pulse
 // goes from low to HIGH to low, so we specify
 // that we want a HIGH-going pulse below:

 pulseTime = pulseIn(echoPin, HIGH);
pulseTime=pulseTime/58;
 // print out that number
 Serial.println(pulseTime,DEC);
 delay(50);
}


I get a perferct reading on the serial monitor window.

But I have a hard time to get this reading of the distance inprocessing (it will be udes to scale an image)

I just want to get this distance reading in an int variable to use it as a multiplier.

I have tried many examples, this one is the closest to working:

Code: [Select]

import processing.serial.*;

Serial port;            // Create object from Serial class
int val;                // Data received from the serial port  
String portname = "/dev/cu.usbserial-A4001LGU";  // find the name of your serial port in your system setup!
 int baudrate = 9600;  //  set baudrate here
void setup() {
 size(200, 200);
 noStroke();
 frameRate(10);                 // Run 10 frames per second
 // Open the port that the board is connected to and use the same speed (9600 bps)
 port = new Serial(this,portname, 9600);
}
void draw() {
 while (port.available() > 0) {
   String inBuffer = port.readString();  
   if (inBuffer != null) {
     println(inBuffer);
   }
 }
 
}



I do get the reading but I can't seem to use this value stored in "inbuffer" to put in an int variable since it is not a number but a string (well this is where I think the problem is...but I am quite a newbee so it is a guess)

I also tried:

Code: [Select]

if (0 < port.available()) {    // If data is available to read,  
   val = port.read();     // read it and store it in val
   println(val);
 }
     println(inBuffer);
   }



But then the numbers are crazy, nothing in common with the sensor reading...

c.kazah

Yes and I also tried this ( and other similar pieces of code) which seems to be the right way

Code: [Select]
 // Process each one of the serial port events
 while (port.available() > 0) {
   serialEvent(port.read());
 }
 background(val);
}

void serialEvent(int serial)
{
 if(serial != 10) {
   buff += char(serial);
 }
 else {
   buff = buff.substring(1, buff.length()-1);
   // Capture the string and print it to the commandline
   // we have to take from position 1 because
   // the Arduino sketch sends EOLN (10) and CR (13)
   if (val == 0) {
     val = 255;
   }
   else {
     val = 0;
   }
   println(buff);
   // Clear the value of "buff"
   buff = "";
 }
}


but than I get no reading, but an error each time:

java.lang.StringIndexOutOfBoundsException: String index out of range: -2
     at java.lang.String.substring(String.java:1768)
     at Temporary_3761_4938.serialEvent(Temporary_3761_4938.java:49)

mellis

Try holding down the reset button on the Arduino while running the Processing program, then letting go of the reset button after Processing starts.  I need to fix the Graph example to be more robust.

c.kazah

I did that and still gets the same error message, we have this exhibition with my students this saturday, and I still can't get it to work, do you have an idea, isn't it possible to "change" the srting to an integer in procesing any other way?

mellis

Check that the buffer is non empty before calling parseInt().

Go Up