Ultrasonic sensor add cm

Hi i have a Project going on and i want to put the sensor on my 300 cm high car,
So now i want the 300 to add to the outcome in cm so 300 plus the height i measure how do i add this in the code. EX: 300 plus 150cm i measure so its auto writes 450cm on the sensor outcome.

#define trigPin 10
#define echoPin 13

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println(“Out of range”);
else {
Serial.print(“Distance = “);
Serial.println(” cm”);

Are you asking how to add 150 to a variable?


Ok thank you guys
How do i hide the code from Arduino program.
I just want seriell monitor to show up can it be done

How do i hide the code from Arduino program.


Put it behind the sofa?

Haha so There is no other Way

If you don’t want to do it in the Serial() call just use distance = (duration / 2) * 0.0344 + 300; or distance += 300 (and of course amend your range check as needed).

The main question that remains: why do you even need to ask about something as basic as this?

Im new to this.

We guessed that.