/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/0223570a-302c-4053-9a89-d7aa5e02d637
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float h;
CloudColoredLight x;
CloudColoredLight y;
int z;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
const int ledRedPin = 13;
const int trigPin = 18;
const int echoPin = 2;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
long duration;
float distanceCm;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(ledRedPin, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
ArduinoCloud.update();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Your code here
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float newDistanceCm = duration * SOUND_SPEED / 2;
// Update the h variable with the measured distance if it has changed
if (newDistanceCm != distanceCm) {
distanceCm = newDistanceCm;
onHChange(); }
delay(1500);
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.println(" cm");
}
/*
Since X is READ_WRITE variable, onXChange() is
executed every time a new value is received from IoT Cloud.
*/
void onXChange() {
// Get the new color value from the IoT Cloud
// Add your code here to act upon X change
}
/*
Since Y is READ_WRITE variable, onYChange() is
executed every time a new value is received from IoT Cloud.
*/
void onYChange() {
// Add your code here to act upon Y change
}
/*
Since Z is READ_WRITE variable, onZChange() is
executed every time a new value is received from IoT Cloud.
*/
void onZChange() {
// Add your code here to act upon Z change
}
/*
Since H is READ_WRITE variable, onHChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHChange() {
// Trigger ultrasonic sensor measurement
ArduinoCloud.update();
// Measure the duration of the echo pulse
h = distanceCm;
// Update the h variable with the measured distance
// Update the variable on IoT Cloud
// Print the measured distance to Serial Monitor
// Add your code here to act upon H change
}