// Import all you need here first
#include <TM1637Display.h> // Import the TM1637 library
// Imports for the SSD1306 OLED...
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Import Blynk Library ....
// #include <BlynkSimpleEsp32.h>
// Define all definables....
#define LED 2 // Define for Red LED
#define CLK 5 // The ESP32 pin GPIO5 connected to CLK (TM1637)
#define DIO 4 // The ESP32 pin GPIO4 connected to DIO (TM1637)
// create a display object of type TM1637Display
TM1637Display display = TM1637Display(CLK, DIO);
// Define screen size (128 by 32) or (128 by 64)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define BUTTON_PIN 13 // GIOP21 pin connected to button
// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Try and change this value later
Serial.println("Hello, ESP32!");
/* ------- LED INITIALIZATION -------- */
// This initiates the parameters for the Red blinking LED
pinMode(LED, OUTPUT);
/* ------- TM1637Display INITIALIZATION -------- */
// This initiates the parameters for the TM1637Display
display.clear();
display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
/* ------- SSD1306Display INITIALIZATION -------- */
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
// SSD1306 OLED Display Code....
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 10); // set position to display
oled.println("Start Weighing.."); // set text
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
// delay(2000); // wait two seconds for initializing
oled.setTextSize(1); // set text size
oled.setTextColor(BLACK); // set text color
oled.setCursor(0, 10); // set position to display
oled.println(" "); // set to display nothing..
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
/* ------- PUSH-BUTTON INITIALIZATION -------- */
// initialize the pushbutton pin as an pull-down input [HIGH-pressed, LOW-open]
// the pull-down input pin will be LOW when the switch is open and HIGH when the switch is closed(PRESSED).
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation - HMM
/* ------- COUNTER INITIALIZATION -------- */
int counter;
/* ================ STEP 1 ==================== */
// For the Push-Button
// read the state of the switch/button:
int buttonState = digitalRead(BUTTON_PIN);
/* ================ STEP 2 ==================== */
// A state check
if (buttonState == 1) {// That is if the button is pressed..
// print out the button's state
Serial.println(buttonState);
Serial.println("The button is pressed");
// Countinually display numbers on the TM1636 4 Digit 7 Segment Display
display.showNumberDec(counter);
while (buttonState == 1){
// Countinually display numbers
counter++; // Increment Counter
display.showNumberDec(counter);
// delay(0.1);
display.clear();
// Re-read the state
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == 0){
// Change Button State to Zero..
// buttonState = 0;
// break;
if (counter != 10 && buttonState == 0){
counter = counter;
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 10); // set position to display
oled.println("Critical Weight.."); // set text
oled.println(" "); // set text
oled.println(counter); // DISPLAY NUMBER
oled.println(" "); // set text
oled.println("Kilogrammes!"); // set text
oled.display(); // display on OLED
delay(500); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(BLACK); // set text color
oled.setCursor(0, 10); // set position to display
oled.println(" "); // set text
oled.display(); // display on OLED
break;
}
}
if (counter == 10 && buttonState == 1) {
for (int i = 0; i < 3; i++){
// Blinking LED Code
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
// Continually display 10 on OLED
// delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 10); // set position to display
oled.println("Critical Weight.."); // set text
oled.println(" "); // set text
oled.println(counter); // DISPLAY NUMBER
oled.println(" "); // set text
oled.println("Kilogrammes!"); // set text
oled.display(); // display on OLED
delay(500); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(BLACK); // set text color
oled.setCursor(0, 10); // set position to display
oled.println(" "); // set text
oled.display(); // display on OLED
}
break;
}
}
while (true) {
// Re-read the state
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == 0){
// Change Button State to Zero..
// buttonState = 0;
counter = 0;
break;
}
}
}else{
counter = 0;
// print out the button's state
Serial.println(buttonState);
Serial.println("The button is pressed");
while (true) {
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 10); // set position to display
oled.println("Restart Weighing.."); // set text
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
// delay(2000); // wait two seconds for initializing
oled.setTextSize(1); // set text size
oled.setTextColor(BLACK); // set text color
oled.setCursor(0, 10); // set position to display
oled.println(" "); // set to display nothing..
oled.display(); // display on OLED
delay(2000); // wait two seconds for initializing
// Re-read the state
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == 1){
// Change Button State to Zero..
break;
}
}
}
}