document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Created by Olivia De Masi - week 16 - Fab Academy 2020
  2.  
  3.  
  4. const int ledPin =  13;      // the number of the LED pin
  5.  
  6. const int trigPin = 9;
  7. const int echoPin = 10;
  8. float duration, distance;
  9.  
  10. int sonarState = 0; // variable for reading the sonar state
  11.  
  12. void setup() {
  13.    pinMode(trigPin, OUTPUT);
  14.  pinMode(echoPin, INPUT);
  15.  Serial.begin(9600);
  16.  
  17.   pinMode(ledPin, OUTPUT);
  18. }
  19.  
  20. void loop() {
  21.  digitalWrite(trigPin, LOW);
  22.   delayMicroseconds(2);
  23.   digitalWrite(trigPin, HIGH);
  24.   delayMicroseconds(10);
  25.   digitalWrite(trigPin, LOW);
  26.  
  27.   duration = pulseIn(echoPin, HIGH);
  28.   distance = (duration*.0343)/2;
  29.    Serial.print("Distance: ");
  30.   Serial.println(distance);
  31.  
  32.  
  33.  
  34.   //check if the sonar is hitting an object.
  35.   //if it is, the sonarState is HIGH:
  36.   if(distance<15.0) {
  37.     //turn LED on:
  38.     digitalWrite(ledPin, HIGH);
  39.   }
  40.  
  41.   else {
  42.     //turn LED off:
  43.     digitalWrite(ledPin, LOW);
  44.   }
  45. }
');