Pages: [1]   Go Down
Author Topic: Problem to get int form arduino into processing  (Read 1222 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
  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:
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:
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...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
 // 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)
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know I am too late to help, but this may help others with this problem. c. kazah writes "I get a perferct reading on the serial monitor window." And that seems to be the problem. With the graph processing sketch, at least, you cannot have the serial monitor on in the arduino window - it seems to make the serial output of Arduino erratic. Processing starts getting spurious strings that are empty or corrupted concatenations of the right reading. And those spurious strings are reflected in the Arduino serial monitor.

First thing, try disabling the serial monitor, then hold down reset button until Processing is running again. That is the only way I could get it running properly again after I had also turned on the serial monitor.

Mellis, is there a fix for this?

Thanks
Dave
Logged

Pages: [1]   Go Up
Jump to: