Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. int trigPin = 12;
  2. int echoPin = 11;
  3. long timeperiod, cm;
  4. const int analogOutPin = 9;
  5. int brightness;
  6.  
  7. void setup()
  8. {
  9. Serial.begin(9600);
  10. pinMode(trigPin, OUTPUT);
  11. pinMode(echoPin, INPUT);
  12. }
  13.  
  14. void loop(){
  15.  
  16. if (timeperiod > 50)
  17. analogWrite(analogOutPin,0);
  18. }
  19. {
  20.  
  21. if (timeperiod 10 < 50){
  22. for(brightness = 0; brightness < 256; brightness++){
  23. analogWrite(led,brightness);
  24. delay(10);}
  25. }}
  26. if (timeperiod < 10)
  27. analogWrite(analogOutPin,0);
  28.  
  29. {
  30. digitalWrite(trigPin, LOW);
  31. delayMicroseconds(20);
  32. digitalWrite(trigPin, HIGH);
  33. delayMicroseconds(0);
  34. digitalWrite(trigPin, LOW);
  35.  
  36. timeperiod = pulseIn(echoPin, HIGH);
  37.  
  38. cm = microsecondsToCentimeters(timeperiod);
  39. Serial.print("dist=");
  40. Serial.print(cm);
  41. Serial.println();
  42.  
  43. delay(10);
  44. }
  45. long microsecondsToInches(long microseconds)
  46. {
  47. return microseconds / 74 /2;
  48. }
  49. long microsecondsToCentimeters(long microseconds)
  50. {
  51. return microseconds / 29/ 2;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement