Merging codes

How can I merge following codes

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8

int sound = 250;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);

}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance <= 30) {
digitalWrite(led, HIGH);
sound = 250;
}
else {
digitalWrite(led,LOW);
}
if (distance < 25) {
digitalWrite(led2, HIGH);
sound = 260;
}
else {
digitalWrite(led2, LOW);
}
if (distance < 20) {
digitalWrite(led3, HIGH);
sound = 270;
}
else {
digitalWrite(led3, LOW);
}
if (distance < 15) {
digitalWrite(led4, HIGH);
sound = 280;
}
else {
digitalWrite(led4,LOW);
}
if (distance < 10) {
digitalWrite(led5, HIGH);
sound = 290;
}
else {
digitalWrite(led5,LOW);
}
if (distance < 5) {
digitalWrite(led6, HIGH);
sound = 300;
}
else {
digitalWrite(led6,LOW);
}

if (distance > 30 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");

}
delay(500);
}

And

#include <LiquidCrystal.h> //Please replace the single quote characters ('') with the parenthesis character (<>)

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

void setup() {

lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm= duration0.034/2;
distanceInch = duration
0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print("inch");
delay(10);

}

I m beginner and know nothing about coding but I need to combine this two sketches for my project. Help will be appreciated

I would start by editing your post to use the code tags to make the code appear in nice boxes and a fixed pitch font, so it's easier for the forum to read. Either use the </> icon top left, or roll your own

[code]sketch goes here [/code]

.

Then edit the first sketch to at least make it look compatible with the second, so that the sensor's at least on the same pins, and change the distance variable to match the distanceCm one in the second sketch.

(The first sketch calculation distance = (duration/2) / 29.1; is arithmetically the same as distanceCm= duration*0.034/2; in the second, since 1/29.1 = 0.034, so the first sketch obviously uses cm.)

Once you have that done, and before you try to merge the sketches, you can then check that you can switch between the 2 sketches by re-loading, to check that the sensor works in both (without having to change pins) and gives the same cm result for an obstacle the same distance away, in both.

After that, and taking say the second sketch as your "master" you may be able to see what needs to get copied from the first sketch into the second.

But my advice is definitely to get the first sketch to "look like" the second in terms of pins and variable names for the sensor.


I see a variable sound in the top sketch, but apart from it getting a value depending on the distance, I don't see it actually doing anything. Maybe I'm missing it, but what's it for?

Ok so I had some spare time and did this for you.

Note that because my lcd is on a shield, I have to use certain pins and only certain pins are left vacant. So check all the pin assignments and use whatever pins suit you.

But it works for me...

// https://forum.arduino.cc/index.php?topic=649127

// check the pins: my lcd shield forces me to use certain pins
// the only pins left are 13,12,11,3,2,1,0 and the analog a1-a5 (aka 15-19 on uno)

#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int trigPin = 2;
const int echoPin = 3;
long duration;
int distanceCm, distanceInch;
int sound; //this gets values but no used?

#define led 19
#define led2 18
#define led3 17
#define led4 16
#define led5 15
#define led6 12

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0, 0);
  lcd.print("testing testing"); // print a simple message
  lcd.setCursor(0, 1);
  lcd.print("  123 testing"); // print a simple message
  delay(1000);
}//setup

void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;
  distanceInch = duration * 0.0133 / 2;

  lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
  lcd.print("Distance: "); // Prints string "Distance" on the LCD
  lcd.print(distanceCm); // Prints the distance value from the sensor
  lcd.print("  cm");
  delay(10);
  lcd.setCursor(0, 1);
  lcd.print("Distance: ");
  lcd.print(distanceInch);
  lcd.print("inch");
  delay(10);

  if (distanceCm <= 30) {
    digitalWrite(led, HIGH);
    sound = 250;
  }
  else {
    digitalWrite(led, LOW);
  }
  if (distanceCm < 25) {
    digitalWrite(led2, HIGH);
    sound = 260;
  }
  else {
    digitalWrite(led2, LOW);
  }
  if (distanceCm < 20) {
    digitalWrite(led3, HIGH);
    sound = 270;
  }
  else {
    digitalWrite(led3, LOW);
  }
  if (distanceCm < 15) {
    digitalWrite(led4, HIGH);
    sound = 280;
  }
  else {
    digitalWrite(led4, LOW);
  }
  if (distanceCm < 10) {
    digitalWrite(led5, HIGH);
    sound = 290;
  }
  else {
    digitalWrite(led5, LOW);
  }
  if (distanceCm < 5) {
    digitalWrite(led6, HIGH);
    sound = 300;
  }
  else {
    digitalWrite(led6, LOW);
  }

  if (distanceCm > 30 || distanceCm <= 0) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distanceCm);
    Serial.println(" cm");

  }

}//loop