-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.txt
38 lines (34 loc) · 946 Bytes
/
Code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// C++ code
int count = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Code for sheep counter
if (0.01723 * readUltrasonicDistance(6, 5) < 100) {
count = (count + 1);
Serial.print("Number of sheep inside fence:");
Serial.println(count);
}
if (0.01723 * readUltrasonicDistance(11, 10) < 100) {
count = (count - 1);
Serial.print("Number of sheep inside fence:");
Serial.println(count);
}
delay(500); // Wait for 500 millisecond(s)
}