Advertisement
Guest User

SRF05

a guest
Mar 30th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. long duration[2];
  2. float distance[2];
  3. int ECHO_PIN[2] = {4, 15};
  4. int TRIGGER_PIN = 5;
  5.  
  6. void setup() {
  7.   Serial.begin (9600);
  8.   pinMode(TRIGGER_PIN, OUTPUT);
  9.   pinMode(ECHO_PIN[0], INPUT);
  10.   pinMode(ECHO_PIN[1], INPUT);
  11. }
  12.  
  13. void loop() {
  14.   digitalWrite(TRIGGER_PIN, LOW);  // Added this line
  15.   delayMicroseconds(2); // Added this line
  16.   digitalWrite(TRIGGER_PIN, HIGH);
  17.   delayMicroseconds(10); // Added this line
  18.   digitalWrite(TRIGGER_PIN, LOW);
  19.   for (int x = 0; x < 2; x++) {
  20.     duration[x] = pulseIn(ECHO_PIN[x], HIGH);
  21.     distance[x] = (duration[x] / 2) / 29.1;
  22.   }
  23.   Serial.print("SRF Module_1 = ");
  24.   Serial.print(distance[0]);
  25.   Serial.print(" cm\n");
  26.   Serial.print("SRF Module_2 = ");
  27.   Serial.print(distance[1]);
  28.   Serial.print(" cm\n");
  29.   delay(1000);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement