Beginner Needs Help

hi,

I am new to this so please bare with me...

I am trying to edit a programme written by somebody else and play around with it but i am confused why its not working .

I keep getting 0 instead of the programme adding my two distances ????

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration , distance, dis1, dis2, total ; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  
  sonicboom();
  distance = dis1;
   sonicboom();
  distance = dis2;
  
  total = dis1 + dis2;
  
  
  
   delay(500);
   Serial.println(total);
  
 
}

  void sonicboom() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 distance == 0;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Turn LED ON to indicate "out of range" */

 digitalWrite(LEDPin, HIGH); 
 
   //Delay 50ms before next reading*****************************.
 delay(5000);
 
 }


  }

any help would be good ......i think i am close but i dont know where i am going wrong ????

i get distance from my sensor

what ever that number is i want to store it in dis1

then i want to call the sonicboom function again to get a distance measurement and then store it in dis 2

and then i want to add both these numbers to give me total

please bare with me

Never, ever on a first date.

o you want to set distance equal to dist1, a number which you haven't given any value to?

But which crt0 set to zero, because dis1 is a global.

Hi,
You have all your variables declared as long, I think it should be float.
long is an integer quantity ie -2 5 55 23 -5 etc.
float allows fractional variables ie 0.01 -4.65 33.78 etc.

Tom... :slight_smile:
Wow, aren't we all up early this morning. (6:55am here)

thank you

"Assignment takes the value of the thing on the right and stores it in the thing on the left. It would appear you have this backwards. This says to set distance equal to whatever is in dist1, which will be 0 in this case. "

this fixed it .....

thank you