Hi all,
I’m having a weird problem here with my ultrasonic sensors. I’m reading noise off my one of the 4 sensors in my system.
Attached is the code, output and the environment where the system is being tested. The values came out to be within 72 - 120 range where the actual distance is 2.4 to 2.6m
long ds1 = 0;
long ds2 = 0;
long ds3 = 0;
long ds4 = 0;
long duration1, duration2, duration3, duration4;
#define POWER_PIN_1 D4
#define ECHO_PIN_1 D3
#define TRIG_PIN_1 D2
#define ECHO_PIN_2 D1
#define TRIG_PIN_2 D0
#define ECHO_PIN_3 B5
#define TRIG_PIN_3 B4
#define ECHO_PIN_4 A1
#define TRIG_PIN_4 A0
// particle setup function...
void setup() {
/*********************************************************************************************************************************************************************/
pinMode(POWER_PIN_1, OUTPUT);
pinMode(TRIG_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT_PULLDOWN);
pinMode(TRIG_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT_PULLDOWN);
pinMode(TRIG_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT_PULLDOWN);
pinMode(TRIG_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT_PULLDOWN);
Serial.begin(9600);
digitalWrite(POWER_PIN_1, HIGH);
Cellular.off();
}
void loop() {
delay(500);
digitalWrite(TRIG_PIN_1, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_1, LOW);
ds1 = pulseIn(ECHO_PIN_1, HIGH); // gets duration...
ds1 = microsecondsToCentimeters(ds1); // gets distance...
Serial.println(ds1);
delay(500);
digitalWrite(TRIG_PIN_2, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_2, LOW);
ds2 = pulseIn(ECHO_PIN_2, HIGH); // gets duration...
ds2 = microsecondsToCentimeters(ds2); // gets distance...
Serial.println(ds2);
delay(500);
digitalWrite(TRIG_PIN_3, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_3, LOW);
ds3 = pulseIn(ECHO_PIN_3, HIGH); // gets duration...
ds3 = microsecondsToCentimeters(ds3); // gets distance...
Serial.println(ds3);
delay(500);
digitalWrite(TRIG_PIN_4, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_4, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_4, LOW);
ds4 = pulseIn(ECHO_PIN_4, HIGH); // gets duration...
ds4 = microsecondsToCentimeters(ds4); // gets distance...
Serial.println(ds4);
}
long microsecondsToCentimeters(long microseconds)
{
return (microseconds / 58.2);
}