Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1.  
  2. // defines pins numbers
  3. const int trigPin = A1;
  4. const int echoPin = A0;
  5. // defines variables
  6. long duration;
  7. int distance;
  8.  
  9.  
  10. void setup() {
  11. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  12. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  13. Serial.begin(9600); // Starts the serial communication
  14. }
  15. void loop() {
  16. // Clears the trigPin
  17. digitalWrite(trigPin, LOW);
  18. delayMicroseconds(2);
  19. // Sets the trigPin on HIGH state for 10 micro seconds
  20. digitalWrite(trigPin, HIGH);
  21. delayMicroseconds(10);
  22. digitalWrite(trigPin, LOW);
  23. // Reads the echoPin, returns the sound wave travel time in microseconds
  24. duration = pulseIn(echoPin, HIGH);
  25. // Calculating the distance
  26. distance= duration*0.034/2;
  27. // Prints the distance on the Serial Monitor
  28. Serial.print("Distance: ");
  29. Serial.println(distance);
  30. delay(500);
  31. }
  32.  
  33.  
  34. ---------------------------
  35. //collects data from an analog sensor
  36.  
  37. int sensorpin = A5; // analog pin used to connect the sharp sensor
  38. int val = 0; // variable to store the values from sensor(initially zero)
  39.  
  40. void setup()
  41. {
  42. Serial.begin(9600); // starts the serial monitor
  43. }
  44.  
  45. void loop()
  46. {
  47. val = analogRead(sensorpin); // reads the value of the sharp sensor
  48. Serial.println(val); // prints the value of the sensor to the serial monitor
  49. delay(400); // wait for this much time before printing next value
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement