Water monitoring for home

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");    
  }
}

The right direction to what?

simply solution
to add a 3.5inch TFT Display using Adafruit_GFX and MCUFRIEND_kbv lib’s

Here is the information you are looking for

i can write simple sketch, i have knowledge of adding library in any sketch, thing is that need codes to attach a 3.5inch display with my existing code that is attached

I didn’t even notice your code. Read the guide to using the forum at the top of each section which explains how to post your code in code tags

This is a self-help forum. Nobody will write complete code for you. Make an attempt and come back if there are any issues or questions.

okay

can you give an example code for this project

Your code is example code. All you need to add are the display calls. The Adafruit library that was linked to, has lots of great display example sketches to get you started. The quality of the documentation there is above average, so you can read that too.

Thanks for posting the code! Did it work on the display? It looks like it should display 7 different coloured rectangles. Your code selects a text font, but it doesn't try to print any text to the screen. Try that.

Yes prints as per command on lcd screen, Yes font code not added yet, that is for showing digit as per graph height incriment.

Hello Everyone, my code works okay in increasing order, don`t know how to get it in decreasing order and last rectDraw remains in screen and want to attach an ultrasonic sensor so that bar graph works according to sensor data.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include "FreeArial_round_16x24.h"
#define  BLACK    0x0000
#define BLUE      0x001F
#define RED       0xF800
#define GREEN     0x07E0
#define CYAN      0x07FF
#define MAGENTA   0xF81F
#define YELLOW    0xFFE0
#define WHITE     0xFFFF
#define ORANGE    0xFD20
#define PURPLE    0x801F
#define PINK      0xF81F
#define OLIVE     0xBFF7
MCUFRIEND_kbv tft;
int x = 20;
int y = 400;
int w = 50;
int h = 40;

void setup() {
  uint16_t ID = tft.readID();
  Serial.begin(9600);
  Serial.println(ID);
  tft.begin(ID);
  tft.setFont(&FreeArial_round_16x24);
  tft.fillScreen(WHITE);
  delay(500);
}
void loop() {
  graph();

}

void graph() {

  tft.drawRect(x, y, w, h, RED);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, y - i, w, h + 4, RED);
    delay(1);
  }
  tft.drawRect(x, y, w, h, GREEN);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 340 - i, w, h + 64, GREEN);
    delay(1);
  }
  tft.drawRect(x, y, w, h, BLUE);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 280 - i, w, h + 124, BLUE);
    delay(1);
  }
  tft.drawRect(x, y, w, h, WHITE);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 220 - i, w, h + 184, WHITE);
    delay(1);
  }

  tft.drawRect(x, y, w, h, YELLOW);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 160 - i, w, h + 244, YELLOW);
    delay(1);
  }
  tft.drawRect(x, y, w, h, MAGENTA);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 100 - i, w, h + 304, MAGENTA);
    delay(1);
  }
  tft.drawRect(x, y, w, h, BLACK);
  for (int i = 0; i < 60; i++) {
    tft.fillRect(x, 40 - i, w, h + 364, BLACK);
    delay(1);
  }
}

Please explain what is to be ordered, and what you mean by "increasing" and "decreasing" order.

Get the ultrasonic transducer working on its own, using the example programs and make sure you understand the operation, before adding anything to the graphing program.

You're dangerously close to making a duplicate thread. It's not allowed on this forum.

https://forum.arduino.cc/t/water-monitoring-for-home/1019287/11

I advise you to continue your previous thread, rather than create new ones like this. The code and the application are basically the same, so when you create new threads all the context is lost.

It wastes peoples time.

"increasing" and "decreasing" means bar graph that prints on screen

You can get an increasing bar graph by putting value ++ in a loop. Your bar graph will increase by one each time through the loop. To get it to decrease put - -. Be aware that your variable type may eventually overflow.

If you want it to increase or decrease in some other way you need to give some details rather than just state the same thing again

Please do not cross post.

postmaster are focusing in instructions "how to post", threads, warning bla bla bla, instead of giving a solution. this forum is good for nothing.

Can you fix my car, it doesn’t work! I only want a solution!!!