Kinetic Sculpture  3.1
Software to Architecture for the Junior Kinetic Sculpture Project
 All Classes Files Functions Variables Macros Pages
Ultrasonic.h
Go to the documentation of this file.
1 
3 #pragma once
4 
10 class Ultrasonic {
11  public:
12 
19  Ultrasonic(byte trigger, byte echo) {
20  triggerPin = trigger;
21  pinMode(triggerPin, OUTPUT);
22 
23  echoPin = echo;
24  pinMode(echoPin, INPUT);
25 
26  IN_PROGRESS = false;
27  }
28 
35  IN_PROGRESS = true;
36 
37  // Clear triggerPin
38  digitalWrite(triggerPin, LOW);
39  delayMicroseconds(2);
40 
41  // Sets the triggerPin on HIGH state for 10 micro seconds
42  digitalWrite(triggerPin, HIGH);
43  delayMicroseconds(10);
44  digitalWrite(triggerPin, LOW);
45 
46  // Reads the echoPin, returns the sound wave travel time in microseconds (US)
47  duration = pulseIn(echoPin, HIGH);
48 
49  calculateDistance();
50  }
51 
59  return distance;
60  }
61 
69  return distance * 2.54;
70  }
71 
77  bool inProgress(){
78  return IN_PROGRESS;
79  }
80 
81  private:
82  const float speedOfSoundInCMPerUS = 0.034;
83  byte triggerPin;
84  byte echoPin;
85  long duration;
86  int distance;
87  bool IN_PROGRESS;
88 
93  void calculateDistance() {
94  distance = duration * speedOfSoundInCMPerUS / 2;
95  }
96 };
float getDistanceInInches()
Definition: Ultrasonic.h:67
bool inProgress()
Definition: Ultrasonic.h:77
void startMeasurement()
Definition: Ultrasonic.h:34
Definition: Ultrasonic.h:10
int getDistanceInCM()
Definition: Ultrasonic.h:57
Ultrasonic(byte trigger, byte echo)
Definition: Ultrasonic.h:19