We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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 :