PulseIn Function in Processing, please help

edited October 2014 in Arduino

Dear all,

i uploaded the firmata software to my arduino board to control it only from processing to have a direct access to incoming data. I do not want to read it out over the Serial port.

Now I connected a HC_SR04 distance sensor to the arduino and it worked fine with arduino code only:

float measureDistance()
{
  //Measure
    digitalWrite(TrigPin, LOW); //Low high and low level take a short time to TrigPin pulse
    delayMicroseconds(2);
    digitalWrite(TrigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(TrigPin, LOW);


    cm = pulseIn(EchoPin, HIGH) / 58.0; //Echo time conversion into cm
    cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places

  //Save Data
    return cm;
}

The above is what I want to translate to processing language. The problem is I use a "PulseIn"-Function (arduino.cc/en/Reference/pulseIn) to measure the distance. This function does not exist in processing language and I would like to know if anyone can tell me if there is a workaround!

It is particularly difficult because there is also no "delayMicroseconds" command for processing. Here is my code, I tried the following:

float measureDistance()
{
  //
    arduino.digitalWrite(TrigPin, Arduino.LOW); //First send LOW to have a clearer HIGH signal thereafter
    delay(1);
    arduino.digitalWrite(TrigPin, Arduino.HIGH); //Send out signal for actually 10 microseconds but not possible without DelayMicroseconds function
    delay(1);
    println("pulseOUT");
    arduino.digitalWrite(TrigPin, Arduino.LOW); 

    //Save Data
    cm = pulseIn(EchoPin, "HIGH") / 58.0; //Echo time conversion into cm, Workaround of PulsIn Function see below
    cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places

   // return distanceInCentimeter;
   return cm;
}

long pulseIn(int pin, String level) 
{
  long startTime = 0;
  long endTime = startTime;

  float pinState = 0.0;

  if (level == "HIGH") 
  {
    println("waiting!"); //Here the programm stalls! (in the while loop, no signal is read out correctly or maybe is already gone... ?)
    while (pinState == 0.0) //Wait till HIGH
    {
      pinState = arduino.digitalRead(pin); 
    }
    startTime = System.nanoTime(); //Start counting  
    println(startTime);
    while (pinState != 0.0) //Wait till LOW
    {
      pinState = arduino.digitalRead(pin);
    }
    endTime = System.nanoTime(); //Stop counting
  }
  else if (level == "LOW")
  {
    while (pinState != 0.0) //Wait till LOW
    {
      pinState = arduino.digitalRead(pin);
    }
    startTime = System.nanoTime(); //Start counting
     while (pinState == 0.0) //Wait till HIGH
    {
      pinState = arduino.digitalRead(pin);
    }
    endTime = System.nanoTime(); //Stop counting
  }

  long PulseDuration = endTime - startTime;
  println(PulseDuration);

  long PulseDurationInMicroseconds = PulseDuration/1000;
  return PulseDurationInMicroseconds;
}

My PulseIn function is probably wrong because the program stalls waiting forever at the indicated point.

Thanks a lot in advance for all help!

PS: Its my first post, please dont be too hard ,-)

Sign In or Register to comment.