4 Ultrasonic sensors error

As i am i total noob and i beginner i need some help whit my projekt.

I have downloaded a exemple for 3 ultrasonic sensors, and i have now got them
to show up on my 20x4 lcd.

Im tying to add one more sensor but i cant get in to show up in the lcd.
What i did was copy/paste the code and change some value to match the code.

Anyone know whats wrong? dont get any error uploading the code.

Thanks

David

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;

int trigPin1 = 22;
int echoPin1 = 23;

int trigPin2 = 24;
int echoPin2 = 25;

int trigPin3 = 26;
int echoPin3 = 27;

int trigPin4 = 28;
int echoPin4 = 29;

void setup() {

  
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
 
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
 
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
 
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  
}

void firstsensor(){ // This function is for first sensor.
  Serial.begin (9600);
  lcd.begin(20, 4);
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  lcd.setCursor(0,0);
  lcd.print("Längd  ");
  lcd.print(distance1);
  lcd.print(" cm ");
  delay(250);
  
      
      
      
      

  if (distance1 < 30) {  // Change the number for long or short distances.
    digitalWrite (ledPin1, HIGH);
  } else {
    digitalWrite (ledPin1, LOW);
  }   
}
void secondsensor(){ // This function is for second sensor.
    int duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration2 = pulseIn (echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
 
    lcd.setCursor(0,1);
    lcd.print("Bredd   ");
    lcd.print(distance2);
    lcd.print("cm");
    delay(150);
  
    if (distance2 < 20) {  // Change the number for long or short distances.
      digitalWrite (ledPin2, HIGH);
    }
 else {
      digitalWrite (ledPin2, LOW);
    }   
}
void thirdsensor(){ // This function is for third sensor.
    int duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration3 = pulseIn (echoPin3, HIGH);
    distance3 = (duration3/2) / 29.1;

    lcd.setCursor(0,2);
    lcd.print("Bredd   ");
    lcd.print(distance3);
    lcd.print("cm");
    delay(150);
    
    if (distance3 < 10) {  // Change the number for long or short distances.
      digitalWrite (ledPin3, HIGH);
    }
 else {
      digitalWrite (ledPin3, LOW);
    }  
}
void foursensor(){ // This function is for four sensor
    int duration4, distance4;
    digitalWrite (trigPin4, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin4, LOW);
    duration4 = pulseIn (echoPin4, HIGH);
    distance4 = (duration4/2) / 29.1;

    lcd.setCursor(0,3);
    lcd.print("Bredd   ");
    lcd.print(distance4);
    lcd.print("cm");
    delay(150);
    
    if (distance4 < 10) {  // Change the number for long or short distances.
      digitalWrite (ledPin3, HIGH);
    }
 else {
      digitalWrite (ledPin3, LOW);
      }  
}
      
void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
}

Do you think it might help if you called the foursensor() function ?

Can you please explaine what that is:)? and how to do it?

Sorry but im so new at this.

/David

The loop() function is currently like this

void loop()
{
  Serial.println("\n");
  firstsensor();
  secondsensor();
  thirdsensor();
  delay(100);
}

It calls 3 functions. You have written another function but have not called it.

Oh, that i did forgot, thanks for your help:)

/David

so now its just two more thing i need help whit, have been looking all over the internet on how to get
mm insted of cm.

Do i need to calculate the numbers, or is there an given calculation for this?

And when i use a diffrent code (just 1 sensor) i dident have any delay, the lcd showd the numbers all the time, but now it blinks "delay".
i try to remove the delay but then nothing works, i need to see the numbers like a did whit 1 sensor.
maby its not possible?

/David

(deleted)

spycatcher2k:
distance3 = ((duration3/2) / 29.1)/10);

to get MM

Should be multiplication by 10 to go from cm to mm

(deleted)

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;

Things get weird when you use the same pins (3, 4, and 5) for two different devices (LCD and LED).

I'm also surprised you did not add a ledPin4.

Thanks for all the help.

The prodjekt im workin on is to move outher sensors whit a stepper and messure the distance to the sensors.
But in the maskin im gonna put this prodjekt i dont have alot of space.
So i need to make the sensor messure Wrong, yes wrong.

So if i start at 600mm and move the stepper 50mm, i need the sensor to tell the lcd that i moved to 700mm.

The sensor needs to add 50mm when it messure the distance.

How can i make this work? Any code to make the sensor messure in this way?

Thanks

You could add 50mm to every reading.

How do i do that?

Trigger80:
How do i do that?

Useful reading here

Your add 50mm is not how math works. You want a multiplication scale factor.

Because with your logic, if your sensor moves 1mm, it will read 51mm. If your sensor moves 10000mm, it will read 10050mm.

You are only displaying current distance between the sensor and some object. It sounds like you want to display change in distance.

You could probably get away with using the map() function to scale the 0-600 reading range into whatever distances you want to work with.