-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d516791
commit 515b1f3
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
class Ultrasonic { | ||
private: | ||
int trigPin; | ||
int echoPin; | ||
int duration = 0; | ||
float distance = 0.0; | ||
|
||
public: | ||
void begin(int trigPin, int echoPin) { | ||
Serial.begin(9600); | ||
|
||
// Setup trig and echo | ||
pinMode(trigPin, OUTPUT); | ||
pinMode(echoPin, INPUT); | ||
} | ||
|
||
void getData() { | ||
digitalWrite(trigPin, LOW); | ||
delayMicroseconds(2); | ||
|
||
digitalWrite(trigPin, HIGH); | ||
delayMicroseconds(10); | ||
digitalWrite(trigPin, LOW); | ||
|
||
// Get the distance | ||
duration = pulseIn(echoPin, HIGH); | ||
distance = ((float) (duration)) / 58.2; | ||
|
||
// Print out the distance | ||
Serial.print("Distance = "); | ||
Serial.println(distance); | ||
|
||
// Return distance | ||
return distance; | ||
} | ||
}; | ||
|
||
class IR { | ||
private: | ||
int value; | ||
const char* irPin; | ||
|
||
public: | ||
void begin(const char* irPin) { | ||
int value; | ||
|
||
// Setup ir | ||
pinMode(irPin, INPUT); | ||
} | ||
|
||
void getValue() { | ||
// Get the value from the ir pin | ||
value = digitalRead(irPin); | ||
|
||
// Return value | ||
return value; | ||
} | ||
}; |