/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/495fceb7-af0e-414a-8fb2-8f8f2a58ccd2
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudLight led;
CloudLength lowlevel;
CloudLength highlevel;
CloudLength tankheight;
CloudLength waterlevel;
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"
#include <HCSR04.h>
// Define Trig and Echo pin: LED Pin
#define trigPin 5
#define echoPin 4
#define MOTOR_CONTROL_PIN 2
// Define variables:
// long duration;
// int distance;
int waterLevelUpCount = 0;
int waterLevelDownCount = 0;
int waterLevelLow = 60;
int waterlevel = 0;
int lowlevel = 50;
int highlevel = 200;
int tankheight = 200;
int led = 1;
int waterLevelHigh = 150;
int waterTankHeight = 180;
float distance;
int maxDistanceCm = 350;
UltraSonicDistanceSensor distanceSensor(trigPin, echoPin,maxDistanceCm); // Initialize sensor that uses digital pins 13 and 12.
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(MOTOR_CONTROL_PIN,OUTPUT);
pinMode(led, OUTPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// 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();
}
void loop() {
// ArduinoCloud.update();
// Your code here
/*
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration*0.034/2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
*/
// Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
distance = distanceSensor.measureDistanceCm();
delay(500);
if ( distance > 1 && distance < 400 )
waterlevel = waterTankHeight - distance;
// if ( waterlevel > waterTankHeight )
// waterlevel = waterTankHeight;
// if ( waterlevel < 0 )
// waterlevel = 0;
Serial.print("Water level = ");
Serial.print(waterlevel);
Serial.println(" cm");
Serial.print("Distance Sensor Output = ");
Serial.print(distance);
Serial.println(" cm");
// temperature = distance;
// delay(100);
if(waterlevel<=waterLevelLow)
waterLevelDownCount++;
else waterLevelDownCount=0;
if(waterlevel>=waterLevelHigh)
waterLevelUpCount++;
else waterLevelUpCount=0;
// waterLevel.publish(liters);
if(waterLevelDownCount==3)
{//TURN ON RELAY
Serial.println("motor turned on");
led=HIGH;
digitalWrite(MOTOR_CONTROL_PIN,HIGH);//Relay is active HIGH
}
if(waterLevelUpCount==3)
{//TURN OFF RELAY
Serial.println("motor turned off");
led=LOW;
digitalWrite(MOTOR_CONTROL_PIN,LOW);//Relay is active HIGH
}
}
void onLedChange() {
// Do something
if (led == 1)
{
digitalWrite(MOTOR_CONTROL_PIN,HIGH);
led=HIGH;
waterLevelUpCount=0;
}
else
{
digitalWrite(MOTOR_CONTROL_PIN,LOW);
led=LOW;
waterLevelDownCount=0;
}
}
void onPressureChange() {
// Do something
}
void onLowlevelChange() {
// Do something
waterLevelLow = lowlevel;
}
void onHighlevelChange() {
// Do something
waterLevelHigh = highlevel;
}
void onTankheightChange() {
// Do something
waterTankHeight = tankheight;
}
void onWaterlevelChange() {
// Do something
}