Hello,
I have a water monitoring system IoT using arduino MKR1010, arduino cloud based, system works good enough but might need some correction with the sketch, secondary i wanna add 3.5inch TFT Display. need code for display using Adafruit_GFX and MCUFRIEND_kbv lib’s
i am attaching my screen design, a gradient color bar that grows according to sensor data with number and 2 slide button to turn on/off relay and a reset push button
Can someone please point me in the right direction?
sketch2022.ino (6.2 KB)
Preformatted text
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/1b342344-2381-4ce5-9fc5-cd798b4eb1dc
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudLight led;
CloudSwitch flush;
CloudSwitch manual;
CloudSwitch motor;
CloudLength level;
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"
#define TRIG 7
#define ECHO 6
int temp = 0;
float water_level;
float water_percentage;
int water_bottom = 160;
int water_top = 25;
int water_distance = water_bottom - water_top;
#define RelayPin1 0
#define RelayPin2 1
#define RelayPin3 2
#define RelayPin4 3
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT_PULLUP);
// 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(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
digitalWrite(RelayPin4, HIGH);
}
void loop() {
ArduinoCloud.update();
digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIG, LOW); // Send pin low again
int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse
distance= distance/58; //Convert the pulse duration to distance
//You can add other math functions to calibrate it well
Serial.print("Distance ");
Serial.print(distance);
Serial.println("cm");
delay(50);
water_percentage = (100 * (water_top + water_distance - distance)) / water_distance;
level = water_percentage;
Serial.print("Waterlevel ");
Serial.print(level);
Serial.println(" %");
delay(10);
if (distance < 25 && temp == 0)
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Water Tank Full ");
motor = 0;
temp = 1;
}
else if (distance < 25 && temp == 1)
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Water Tank Full ");
delay(10);
motor = 0;
}
else if (distance > 100)
{
digitalWrite(RelayPin1, LOW);
Serial.println("Low Water Level");
Serial.println("Motor Auto Mode ON");
delay(10);
temp = 0;
motor = 1;
}
manual_control();
}
/*
Since Level is READ_WRITE variable, onLevelChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLevelChange() {
// Add your code here to act upon Level change
}
/*
Since Motor is READ_WRITE variable, onMotorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMotorChange() {
if (motor == 1)
{
digitalWrite(RelayPin1, LOW);
Serial.println("Motor ON");
toggleState_1 = 1;
}
else
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Motor OFF");
toggleState_1 = 0;
}
}
/*
Since Manual is READ_WRITE variable, onManualChange() is
executed every time a new value is received from IoT Cloud.
*/
void onManualChange() {
if (manual == 1)
{
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, LOW);
Serial.println("Motor Manual Mode ON");
toggleState_2 = 1;
}
else
{
digitalWrite(RelayPin2, HIGH);
Serial.println("Motor Manual Mode OFF");
toggleState_2 = 0;
}
}
/*
Since Light is READ_WRITE variable, onLightChange() is
executed every time a new value is received from IoT Cloud.
*/
void manual_control() {
if (digitalRead(motor) == 0) {
relayOnOff(1);
delay(200);
motor = toggleState_1;
}
}
void relayOnOff(int relay) {
switch (relay) {
case 1:
if (toggleState_1 == 0) {
digitalWrite(RelayPin1, LOW);
toggleState_1 = 1;
Serial.println("toggle Motor ON");
}
else {
digitalWrite(RelayPin1, HIGH);
toggleState_1 = 0;
Serial.println("toggle Motor OFF");
}
delay(100);
break;
case 2:
if (toggleState_2 == 0) {
digitalWrite(RelayPin2, LOW);
toggleState_2 = 1;
Serial.println("toggle Manual ON");
}
else {
digitalWrite(RelayPin2, HIGH);
toggleState_2 = 0;
Serial.println("toggle Manual OFF");
}
delay(100);
break;
case 3:
if (toggleState_3 == 0) {
digitalWrite(RelayPin3, LOW);
toggleState_3 = 1;
Serial.println("toggle Light ON");
}
else {
digitalWrite(RelayPin3, HIGH);
toggleState_3 = 0;
Serial.println("toggle Light OFF");
}
delay(100);
break;
case 4:
if (toggleState_4 == 0) {
digitalWrite(RelayPin4, LOW);
toggleState_4 = 1;
Serial.println("toggle Relay4 ON");
}
else {
digitalWrite(RelayPin4, HIGH);
toggleState_4 = 0;
Serial.println("toggle Relay4 OFF");
}
delay(100);
break;
default : break;
}
}
/*
Since Flush is READ_WRITE variable, onFlushChange() is
executed every time a new value is received from IoT Cloud.
*/
void onFlushChange() {
if (flush == 1)
{
digitalWrite(RelayPin3, LOW);
Serial.println("Flush");
toggleState_3 = 1;
delay(15000);
digitalWrite(RelayPin3, HIGH);
Serial.println("Flushing done");
}
}