Skip to content

Commit 4fc320c

Browse files
authored
Update HC-SR04.ino
1 parent 4eb84a1 commit 4fc320c

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

examples/CommunitySubmitted/HC-SR04/HC-SR04.ino

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
1. Attach a HC-SR04 to your Arduino:
77
Schematic:
88
HC-SR04 Arduino
9-
[VDD] --- [5V]
9+
[VCC] --- [5V]
1010
[GND] --- [GND]
11-
[TRIGGER] --- [Digital Pin 2]
12-
[ECHO] --- [Digital Pin 3]
11+
[TRIGGER_PIN] --- [Digital Pin 2]
12+
[ECHO_PIN] --- [Digital Pin 3]
1313
2. Set the DISTANCE_VIRTUAL_CHANNEL value below to a free virtual channel.
1414
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
1515
4. Compile and upload this sketch.
@@ -28,17 +28,17 @@ char clientID[] = "CLIENT_ID";
2828

2929
#define DISTANCE_VIRTUAL_CHANNEL 1
3030

31-
// defines pins numbers
32-
#define TRIGPIN 2
33-
#define ECHOPIN 3
31+
// Defines pins numbers for the HC-SR04 connections.
32+
#define TRIGGER_PIN 2
33+
#define ECHO_PIN 3
3434

35-
// defines variables
35+
// Defines variables for storing the calculated values.
3636
long duration;
3737
int distance;
3838

3939
void setup() {
40-
pinMode(TRIGPIN, OUTPUT);
41-
pinMode(ECHOPIN, INPUT);
40+
pinMode(TRIGGER_PIN, OUTPUT);
41+
pinMode(ECHO_PIN, INPUT);
4242
Serial.begin(9600);
4343
Cayenne.begin(username, password, clientID);
4444
}
@@ -50,17 +50,19 @@ void loop() {
5050
// This function is called at intervals to send temperature sensor data to Cayenne.
5151
CAYENNE_OUT(DISTANCE_VIRTUAL_CHANNEL)
5252
{
53-
digitalWrite(TRIGPIN, LOW);// Clears the trigPin
53+
digitalWrite(TRIGGER_PIN, LOW);// Clears the trigPin
5454
delayMicroseconds(2);
5555

56-
digitalWrite(TRIGPIN, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
56+
digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
5757
delayMicroseconds(10);
58-
digitalWrite(TRIGPIN, LOW);
59-
60-
duration = pulseIn(ECHOPIN, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds
58+
digitalWrite(TRIGGER_PIN, LOW);
6159

62-
distance = duration * 0.034 / 2; // Calculating the distance
60+
duration = pulseIn(ECHO_PIN, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds
6361

64-
// Send the distance value to Cayenne on counter widget.
65-
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, "counter", "null");
62+
distance = duration * 0.034 / 2; // Calculating the distance.
63+
64+
// Send the distance value to Cayenne on proximity widget in centimeter.
65+
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, "prox", "cm");
66+
// Send the distance value to Cayenne on proximity widget in meter.
67+
//Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, "prox", "m");
6668
}

0 commit comments

Comments
 (0)