incorrect reading from multiple ultrasonic sensors

Ditched the last project and after some guidance from some other users I opted to go with a single board for simplicity.Running an arduino nano and two HC-sr04 sensors.

Both serial and LCD are only printing 25905 or 25906, doesn't change if I move anything in front of the sensor or move the sensors away. Code below:

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);


//

void setup() {
  const int trigPin1 = A1;
  const int echoPin1 = A2;
  const int trigPin2 = A3;
  const int echoPin2 = A6;
  const int alarmPin = 2;
  // defines variables
  long duration1;
  int distance1;
  long duration2;
  int distance2;
  distance1 = duration1 * 0.034 / 2;
  distance2 = duration2 * 0.034 / 4;
  pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
  pinMode(4, OUTPUT);
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(500);
  lcd.init();
  lcd.backlight();
  Serial.print("Initializing Startup");
  lcd.setCursor(2, 0);
  lcd.print("Initializing");
  lcd.setCursor(2, 1);
  lcd.print("Start Up...");
  delay(3000);
  Serial.print("Testing Connections");
  lcd.setCursor(2, 0);
  lcd.print("Testing Sensor");
  lcd.setCursor(2, 1);
  lcd.print("connections...");
  delay(3000);
  if (digitalRead(2) == HIGH) {
    lcd.setCursor(2, 0);
    lcd.print("Sensor 1      ");
    lcd.setCursor(2, 1);
    lcd.print("connected     ");

  }
  delay(3000);
  if (digitalRead(6) == HIGH) {
    lcd.setCursor(2, 0);
    lcd.print("Sensor 2      ");
    lcd.setCursor(2, 1);
    lcd.print("connected     ");
    if (digitalRead(3) == LOW)
      lcd.setCursor(2, 0);
    lcd.print("Sensor 2      ");
    lcd.setCursor(2, 1);
    lcd.print("not connected  ");
  }
  delay(3000);
  lcd.setCursor(2, 0);
  lcd.print("Test finished ");
  lcd.setCursor(2, 1);
  lcd.print("                ");
  delay(3000);
  lcd.setCursor(2, 0);
  lcd.print("Starting       ");
  lcd.setCursor(2, 1);
  lcd.print("monitors        ");
  delay(2000);
  lcd.noDisplay();

  // Turn on the display:
  lcd.display();
  delay(500);

  //

}

void loop()
{  
digitalWrite('trigPin1', LOW);;
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite('trigPin1', HIGH);
delayMicroseconds(10);
digitalWrite('trigPin1', LOW);

  lcd.setCursor(2, 0);
  lcd.print('distance1');
  Serial.println('distance1');
  delay(2500);
  lcd.clear();

digitalWrite('trigPin2', LOW);;
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite('trigPin2', HIGH);
delayMicroseconds(10);
digitalWrite('trigPin2', LOW);
  lcd.setCursor(2, 0);
  lcd.print('distance2');
   Serial.println('distance2');
  delay(2500);
  lcd.clear();

}

I see that you have invented a new syntax all of your own. Sorry, that does not work that way.

All of the variables that you declare in setup() go out of scope as soon as setup() finishes. They no longer exist in loop.

digitalWrite('trigPin1', LOW);;

What is 'trigPin1'? Single quotes are for single characters not strings. And digitalWrite() does not take a string as an argument.

lcd.print('distance1');

Same with 'distance'. Single quotes for single characters.

In the IDE go to File and Preferences. Turn on compiler warnings to be able to see the warnings generated.

I fixed the code so that no errors or warnings show up and formatted the code with the auto format tool of the IDE (ctrl-t or Tools, Auto Format).

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

const int trigPin1 = A1;
const int echoPin1 = A2;
const int trigPin2 = A3;
const int echoPin2 = A6;
const int alarmPin = 2;
// defines variables
long duration1;
int distance1;
long duration2;
int distance2;

//

void setup()
{
   distance1 = duration1 * 0.034 / 2;
   distance2 = duration2 * 0.034 / 4;
   pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
   pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
   pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
   pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
   pinMode(4, OUTPUT);
   Serial.begin(9600);
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
   pinMode(13, OUTPUT);
   digitalWrite(13, HIGH);
   delay(500);
   lcd.init();
   lcd.backlight();
   Serial.print("Initializing Startup");
   lcd.setCursor(2, 0);
   lcd.print("Initializing");
   lcd.setCursor(2, 1);
   lcd.print("Start Up...");
   delay(3000);
   Serial.print("Testing Connections");
   lcd.setCursor(2, 0);
   lcd.print("Testing Sensor");
   lcd.setCursor(2, 1);
   lcd.print("connections...");
   delay(3000);
   if (digitalRead(2) == HIGH)
   {
      lcd.setCursor(2, 0);
      lcd.print("Sensor 1      ");
      lcd.setCursor(2, 1);
      lcd.print("connected     ");

   }
   delay(3000);
   if (digitalRead(6) == HIGH)
   {
      lcd.setCursor(2, 0);
      lcd.print("Sensor 2      ");
      lcd.setCursor(2, 1);
      lcd.print("connected     ");
      if (digitalRead(3) == LOW)
         lcd.setCursor(2, 0);
      lcd.print("Sensor 2      ");
      lcd.setCursor(2, 1);
      lcd.print("not connected  ");
   }
   delay(3000);
   lcd.setCursor(2, 0);
   lcd.print("Test finished ");
   lcd.setCursor(2, 1);
   lcd.print("                ");
   delay(3000);
   lcd.setCursor(2, 0);
   lcd.print("Starting       ");
   lcd.setCursor(2, 1);
   lcd.print("monitors        ");
   delay(2000);
   lcd.noDisplay();

   // Turn on the display:
   lcd.display();
   delay(500);

   //

}

void loop()
{
   digitalWrite(trigPin1, LOW);
   delayMicroseconds(2);
   // Sets the trigPin on HIGH state for 10 micro seconds
   digitalWrite(trigPin1, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin1, LOW);

   lcd.setCursor(2, 0);
   lcd.print(distance1);
   Serial.println(distance1);
   delay(2500);
   lcd.clear();

   digitalWrite(trigPin2, LOW);;
   delayMicroseconds(2);
   // Sets the trigPin on HIGH state for 10 micro seconds
   digitalWrite(trigPin2, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin2, LOW);
   lcd.setCursor(2, 0);
   lcd.print(distance2);
   Serial.println(distance2);
   delay(2500);
   lcd.clear();

}

distance1 = duration1 * 0.034 / 2; That's a long-winded way of setting distance1 to zero.

Thanks Fungus, still learning this coding thing, my books are leaving me in the dust apparently.

AWOL, I see that it sets my distance to 0, confirmed with the LED, how do I go about actually getting the distance?

By calculating the distance each time you ping a sensor.

  long duration1;
  int distance1;
  long duration2;
  int distance2;
  distance1 = duration1 * 0.034 / 2;
  distance2 = duration2 * 0.034 / 4;

The distance1 and distance2 variables are integers so cannot have fractional parts (decimal places). If you change their data types to float then they can have fractional parts.

  float distance1;
  float distance2;
distance2 = duration2 * 0.034 / 4;

Why divide by 4?

 long duration1;
 long duration2;

It is better to use unsigned long for variables that deal with time (micros(), millis()).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.