#include <U8g2lib.h>
int buttonPin = 3;
int relayPin = 4; // Define a pin for the relay
int progress = 0;
int progress2 = 0; // Initialize progress variable
bool relayOn = false;
bool buttonState = false;
bool lastButtonState = false;
// Initialize the U8g2 library for your specific display
// This example is for a 128x64 I2C OLED display with a SSD1306 driver
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
static const unsigned char image_weather_humidity_bits[] = {0x20,0x00,0x20,0x00,0x30,0x00,0x70,0x00,0x78,0x00,0xf8,0x00,0xfc,0x01,0xfc,0x01,0x7e,0x03,0xfe,0x02,0xff,0x06,0xff,0x07,0xfe,0x03,0xfe,0x03,0xfc,0x01,0xf0,0x00};
static const unsigned char image_weather_humidity_bits1[] = {0x20,0x00,0x20,0x00,0x30,0x00,0x70,0x00,0x78,0x00,0xf8,0x00,0xfc,0x01,0xfc,0x01,0x7e,0x03,0xfe,0x02,0xff,0x06,0xff,0x07,0xfe,0x03,0xfe,0x03,0xfc,0x01,0xf0,0x00};
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Configure buttonPin as an input with pull-up resistor
pinMode(relayPin, OUTPUT); // Set relayPin as an output
digitalWrite(relayPin, LOW); // Ensure relay is off at the start
u8g2.begin();
// Display the initial message for 3 seconds
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr); // Choose a suitable font
u8g2.drawStr(30, 32, "Digibrand.lk");
u8g2.sendBuffer();
delay(3000); // Wait for 3 seconds
while (progress <= 100) {
progress2 = map(progress, 0 , 100 , 0 , 97);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setFont(u8g2_font_6x13_tr);
u8g2.drawStr(36, 29, "STARTING..");
u8g2.drawFrame(13, 34, 101, 10);
u8g2.drawBox(15, 36, progress2, 6);
u8g2.sendBuffer();
progress++;
//delay(100);
}
}
void loop() {
int sensorValue = analogRead(A0);
//int sensorValue = 512;
// Map the sensor value to a range of 0 to 100
int mappedValue = map(sensorValue, 0, 1023, 0, 100);
int mappedValue2 = map(sensorValue, 0, 1023, 2, 47);
int mappedValue3 = map(sensorValue, 0, 1023, 55, 10);
// Convert the mapped value to a string with a percentage sign
char mappedValueStr[5]; // Buffer to hold the percentage string
sprintf(mappedValueStr, "%d%%", mappedValue);
// Read the current state of the button
buttonState = digitalRead(buttonPin) == LOW;
// Detect button press (state change from HIGH to LOW)
if (buttonState && !lastButtonState) {
digitalWrite(relayPin, HIGH); // Turn on the relay when button is pressed
relayOn = true;
}
// Update the last button state
lastButtonState = buttonState;
// Control the relay based on the mappedValue
if (mappedValue <= 5) {
digitalWrite(relayPin, HIGH); // Turn on the relay when mappedValue reaches 5
relayOn = true;
} else if (mappedValue >= 100) {
digitalWrite(relayPin, LOW); // Turn off the relay when mappedValue reaches 100
relayOn = false;
}
// Determine what to display
bool displayMotorOn = relayOn;
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
//u8g2.drawFrame(0, 1, 127, 62);
u8g2.drawFrame(11, 8, 32, 51);
//u8g2.drawBox(17, 15, 20, 3);
u8g2.drawBox(13, mappedValue3, 28, mappedValue2);
if (displayMotorOn) {
u8g2.drawXBM(57, 33, 11, 16, image_weather_humidity_bits1);
u8g2.setFont(u8g2_font_t0_22_tr);
u8g2.drawStr(70, 49, mappedValueStr);
u8g2.setFont(u8g2_font_6x13_tr);
u8g2.drawStr(58, 30, "MOTOR ON");
} else {
u8g2.drawXBM(58, 24, 11, 16, image_weather_humidity_bits);
u8g2.setFont(u8g2_font_t0_22_tr);
u8g2.drawStr(71, 40, mappedValueStr);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(57, 50, "WATER LEVEL");
}
u8g2.sendBuffer();
delay(1000); // Update the display every second
}