hello, im using 3 ultrasonic sensor on my robot. is it possible to get 3 sensor reading in Setup() function?? the problem i get is its only get the reading from the first sensor and the other 2 return 0. Picture below show the code that i made with the serial output.
If you print that picture of the code out, and take a pair of scissors and cut it up, and rearrange the picture so that the sensor instances are called in a different order, do that change the picture of the output?
DO NOT POST PICTURES OF TEXT!
hai Pauls.. thankx for the reply,.. i already rearrange the code in setup() and the other two sensor still return the value 0.
for example:
#include <HCSR04.h>
HCSR04 LeftS = HCSR04(22,23,false);
HCSR04 RightS = HCSR04(24,25,false);
HCSR04 centerS = HCSR04(26,27,false);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Setup()");
int ccc = centerS.Distance();------------------------->>>> Only get reading from this sensor
int aaa = LeftS.Distance(); ------------------------->>>> return 0
int bbb = RightS.Distance();------------------------->>>> return 0
delay(2000);
Serial.println(aaa);
Serial.println(bbb);
Serial.println(ccc);
delay(5000);
}
no problem in loop().. All sensor get readings
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Loop()");
int ccc = centerS.Distance();
int aaa = LeftS.Distance();
int bbb = RightS.Distance();
Serial.println(aaa);
Serial.println(bbb);
Serial.println(ccc);
delay(5000);
}
here is the output
Setup()
0
0
15
Loop()
11
20
15
Loop()
11
13
15
Below is library im using
#include "HCSR04.h"
#include <inttypes.h>
HCSR04::HCSR04(uint8_t echo,uint8_t trigger,bool isInches)
{
_echo = echo;
_trigger = trigger;
if(isInches) _factor = INCHES_FACTOR;
else _factor = CENTIMETERS_FACTOR;
pinMode(_echo,INPUT);
pinMode(_trigger,OUTPUT);
digitalWrite(_echo,LOW);
}
double HCSR04::Distance()
{
digitalWrite(_trigger,LOW);
delayMicroseconds(2);
digitalWrite(_trigger,HIGH);
delayMicroseconds(10);
digitalWrite(_echo,LOW);
long duration = pulseIn(_echo,HIGH,10000);
return duration / _factor / 2;
}
That's some pretty poor software. The constructor for the class is calling pinMode(). It does NOT know if the hardware is ready, so it should not be calling pinMode().
The class needs a begin() method that contains the pinMode() and digitalWrite() calls.
If you make the calls in setup() to the left, center, and right instances, does the order of success/failure change?
Why does it matter that the calls in setup() do not produce the results you expect, when the important ones in loop() seem to?
By the way, stupid variable names and anonymous printing suck.
Serial.print("center distance: ");
Serial.println(centerDistance);
is far better than
Serial.println(aaa);