//  www.robotronix.co.il
// רובוטרוניקס קורס C510
// שיעור 8 - צללים טונים 

#define   speaker  5


#define LedRed  3
#define LedGreen  8



const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
  
  // pin setting  
     pinMode(pingPin, OUTPUT);
     pinMode(echoPin, INPUT);

     pinMode(LedRed , OUTPUT);
     pinMode(LedGreen, OUTPUT);

    digitalWrite(LedRed, HIGH);
    digitalWrite(LedGreen , HIGH);

     Serial.begin(9600); // Starting Serial Terminal
     delay(250);
     digitalWrite(LedRed, LOW);
     digitalWrite(LedGreen , LOW);
     Serial.println("starting ...");
}

void loop() {
   long duration, inches, cm;

  // generate pulse    ___|^^^^^|_______________
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);

  // read pulse 
   duration = pulseIn(echoPin, HIGH);
   cm       = microsecondsToCentimeters(duration);
  // Serial.print(inches);
  // Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
 
      if(cm > 200) // 20 cm
      {
          digitalWrite(LedRed, LOW);
          digitalWrite(LedGreen, HIGH);
  
      }
      else
      {
           digitalWrite(LedRed ,HIGH);
          digitalWrite(LedGreen, LOW);

      } 

   delay(200);


}



long microsecondsToCentimeters(long microseconds) {
   return (microseconds / 29 / 2);
}

/*
In the code, the division by 29 is used to convert the duration of the ultrasonic pulse (measured in microseconds) into a distance measurement in centimeters. This conversion is based on the speed of sound in air and the round-trip travel time of the ultrasonic pulse.

The speed of sound in air is approximately 343 meters per second (or 34300 centimeters per second). To calculate the distance traveled by the ultrasonic pulse, you need to consider the time it takes for the pulse to travel from the sensor to the obstacle and back.

Since the pulse needs to travel the distance twice (to the obstacle and back), the time measured by the pulseIn() function represents the round-trip time. To calculate the one-way distance traveled by the pulse, you divide the round-trip time by 2.

However, the pulseIn() function returns the duration in microseconds, so you need to convert it to seconds by dividing it by 1,000,000. Thus, you have the time taken by the pulse for a one-way trip in seconds.

To convert this time to distance in centimeters, you multiply it by the speed of sound (34300 cm/s). The resulting value is the distance traveled by the pulse in centimeters.

Here's the breakdown of the calculation:

    duration is the pulse duration measured by pulseIn() in microseconds.
    microsecondsToCentimeters(duration) function divides the duration by 29 (approximately the speed of sound divided by 2), giving the one-way distance traveled by the pulse in centimeters.

Keep in mind that this is an approximation as the speed of sound can vary slightly with factors like temperature and humidity.
*/