Hi, no I did delete "-" already but it is not just that
I am going to read about the command "read".
But if you get the mistake please let me know because I would like to use 4 sensor.
I have modified my script moving the code order after your suggestion and it works.
please run it there is just a small issue I think because timing.
New code: (btw I am on Mac and I can't paste the code like you do!!!)
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/
//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx
//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx
#define LEDPin 13 // Onboard LED
int maximumRangelx = 200; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed
int maximumRangerx = 200; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance
long durationrx, distancerx; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
durationlx = pulseIn(echoPin1, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distancelx = durationlx/58.2;
if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.print(" -1 LX ");
//digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(distancelx);
Serial.println( " cm LX ");
// digitalWrite(LEDPin, LOW);
}
delay(25);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
durationrx = pulseIn(echoPin2, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distancerx = durationrx/58.2;
if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println(" -1 RX ");
// digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(distancerx);
Serial.println( " cm RX ");
//digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(25);
}