Translating Arduino to Processing. - pulseIN

edited April 2015 in Arduino

Hello all,

I have searched the web and I see that this is a common problem. First let me include the code I am working with. This is obviously for Arduino.

        #define echoPin 3 // Echo Pin
        #define trigPin 2 // Trigger Pin
        #define LEDPin 13 // Onboard LED


        int result = 0;
        int maximumRange = 200; // Maximum range needed
        int minimumRange = 0; // Minimum range needed
        long duration, distance; // Duration used to calculate distance
        int relaypin = 8;
        int relay = 0;


        void setup() {
         Serial.begin (9600);
         pinMode(trigPin, OUTPUT);
         pinMode(echoPin, INPUT);
         pinMode(8, OUTPUT);

        }

        void loop() {
        /* The following trigPin/echoPin cycle is used to determine the
         distance of the nearest object by bouncing soundwaves off of it. */ 
         digitalWrite(trigPin, LOW); 
         delayMicroseconds(2); 

         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10); 

         digitalWrite(trigPin, LOW);
         duration = pulseIn(echoPin, HIGH);


         distance = duration/58.2;

         if (distance >= maximumRange || distance <= minimumRange){

         Serial.println("-1");
         if (result == 1){
            relay = 0;
         }

         }

         else {

         Serial.println(distance);

         if (result == 0 ){
           relay = 255;
         }


         }
         Serial.println("relay:");
         Serial.println(relay);
         digitalWrite(relaypin, relay);
         relay = 0;
         delay(50);
         }

Now, I would like to transfer this into Processing, but I am having some compatibility issues. What is the translation of this in Processing? The most difficult to figure out is pulseIN.

Thank you.

New Addition: Here is what I have so far in Processing.

import ddf.minim.*;
import processing.serial.*;
import org.firmata.*;
import cc.arduino.*;

int echoPin = 3; // Echo Pin
int trigPin = 2; // Trigger Pin
int LEDPin = 13;
int result = 0;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int relaypin = 8;
int relay = 0;

Arduino arduino;
Minim minim;
AudioPlayer player;

void setup() {

 arduino = new Arduino(this, Arduino.list()[0], 9600);
 arduino.pinMode(trigPin, Arduino.OUTPUT);
 arduino.pinMode(echoPin, Arduino.INPUT);
 arduino.pinMode(8, Arduino.OUTPUT);
 minim = new Minim(this);
 player = minim.loadFile("marcus_kellis_theme.mp3");
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 arduino.digitalWrite(trigPin, Arduino.LOW); 
 delay(2); 

 arduino.digitalWrite(trigPin, Arduino.HIGH);
 delay(10); 

 arduino.digitalWrite(trigPin, Arduino.LOW);
 duration = pulseIn(echoPin, Arduino.HIGH);


 distance = duration/58.2;

 if (distance >= maximumRange || distance <= minimumRange){

 Serial.println("-1");
 if (result == 1){
    relay = 0;
 }

 }

 else {

 Serial.println(distance);

 if (result == 0 ){
   relay = 255;
   player.play();
 }


 }
 Serial.println("relay:");
 Serial.println(relay);
 digitalWrite(relaypin, relay);
 relay = 0;
 delay(50);
 }

Answers

  • I didn't know we can type arduino code in Processing! Can you link me the tutorial ,if any, where you learnt this? If pulseIn doesn't exist in your libs you can make one yourself :

    int pulseIn(int pin,int state){    
    //Set pin as input (necessary only if both trig and echo are the same pin.)
    arduino.pinMode(pin, Arduino.INPUT);
    //wait till pin is in desired state
    while(arduino.digitalRead(pin)!=state);
    //record initial time
    int t0=millis();
    //wait till pin exits from desired state
    while(arduino.digitalRead(pin)==state);  
    //record final time
    int t1=millis();
    //return time difference
    return t1-t0;
    }
    /*warnings : 
    1) Empty loop may take 100% CPU 
    2) May spiral into infinite loop if distance is very large
    3) I still can't believe one can use Processing. (somebody please confirm in the comments)
    4) You can alternatively(and better) program Arduino to send commands to Processing 
    */
    
Sign In or Register to comment.