#include "User_Setup.h"
#include <SPI.h>
#include <TFT_eSPI.h>
#include "NotoSansBold15.h"
#include "NotoSansBold36.h"
#include "CourierB24.h"
#include "Unicode_Test_72.h"
#include "background.h"
#include "FreeSansBold36.h"
#define SOLENOID_PIN 12
#define Ain 8
#define BUZZER_PIN 15 // Define your buzzer pin (e.g., GPIO 5)
#define RST 40
#define T5 38
#define T10 37
#define T15 36
#define T20 34
#define T25 18
#define T30 17
#define PLUS1 39
//#define SDA 33
//#define SCL 35
#define TEST_LED 9
#define RGB_D 16
#define RATE_X 100
#define RATE_Y 110
#define RATE_W 200
#define RATE_H 56
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);
TFT_eSprite sprT = TFT_eSprite(&tft);
int oldV = -1;
int v;
float vf1;
unsigned long tNow;
unsigned long tRead;
unsigned int timeout = 0;
byte minutes, seconds;
unsigned long debounce;
// State tracking for the buzzer
bool lastSolenoidState = false;
void setup() {
Serial.begin(115200);
pinMode(SOLENOID_PIN, OUTPUT);
digitalWrite(SOLENOID_PIN, LOW);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
pinMode(RST, INPUT_PULLUP);
pinMode(T5, INPUT_PULLUP);
pinMode(T10, INPUT_PULLUP);
pinMode(T15, INPUT_PULLUP);
pinMode(PLUS1, INPUT_PULLUP);
tft.begin();
tft.setRotation(3);
tft.setSwapBytes(true);
tft.pushImage(0, 0, 320, 240, (uint16_t *)image1);
//spr.loadFont(NotoSansBold36);
//spr.loadFont(Unicode_Test_72);
//spr.loadFont(FreeSansBold56pt7bBitmaps);
spr.setFreeFont(&FreeSansBold36pt7b);
spr.setTextColor(TFT_WHITE, TFT_BLACK);
spr.createSprite(RATE_W, RATE_H);
sprT.loadFont(CourierB24);
sprT.setTextColor(TFT_YELLOW, TFT_BLACK);
sprT.createSprite(120, 24);
tNow = millis();
tRead = millis();
}
// Helper function for a quick beep
void beep(int duration) {
//digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 500);
delay(duration);
noTone(BUZZER_PIN);
//delay(duration);
//digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
// --- Button Handling ---
if(millis() - debounce > 250){
if (digitalRead(T5) == LOW) { timeout = 300; debounce = millis(); }
if (digitalRead(T10) == LOW) { timeout = 600; debounce = millis(); }
if (digitalRead(T15) == LOW) { timeout = 900; debounce = millis(); }
if (digitalRead(PLUS1) == LOW) { timeout += 60; debounce = millis(); }
if (digitalRead(RST) == LOW) { timeout = 0; debounce = millis(); }
}
// --- Solenoid & Buzzer Logic ---
bool currentSolenoidState = (timeout > 0);
if (currentSolenoidState != lastSolenoidState) {
if (currentSolenoidState) {
// Logic: Started (0 -> 1)
digitalWrite(SOLENOID_PIN, HIGH);
beep(150); // Single short beep for start
} else {
// Logic: Ended (1 -> 0)
digitalWrite(SOLENOID_PIN, LOW);
beep(500); // Longer beep or double beep for end
delay(100);
beep(500);
}
lastSolenoidState = currentSolenoidState;
}
// --- Timer Update (Every 1 Second) ---
if (millis() - tNow >= 1000) {
tNow = millis();
if (timeout > 0) timeout--;
//sprT.fillSprite(TFT_BLACK);
sprT.pushImage(200, 45, 120, 24, (uint16_t *)image1);
sprT.setCursor(0, 0);
minutes = timeout / 60;
seconds = timeout % 60;
if (minutes < 10) sprT.print("0");
sprT.print(minutes);
sprT.print(":");
if (seconds < 10) sprT.print("0");
sprT.print(seconds);
sprT.pushSprite(200, 45);
}
// --- CO2 Sensor Update (Every 500ms) ---
if (millis() - tRead >= 500) {
tRead = millis();
v = analogRead(8);
if (abs(oldV - v) > 5) {
vf1 = v / 81.92;
oldV = v;
//spr.fillSprite(TFT_BLACK);
//spr.pushImage(-100, -80, 200, 36, (uint16_t *)image1);
//spr.fillSprite(TFT_BLACK);
//spr.pushImage(0, 0, 200, 36, (uint16_t *)image1+640);
//spr.setCursor(0, 0);
//spr.pushImage(0, 0, 200, 36, (uint16_t *)image1);
//spr.setCursor(0,0);
//spr.print(vf1, 1);
//spr.drawFloat(vf1, 1, 0, 0);
//spr.print("%");
//spr.pushSprite(100, 80,TFT_TRANSPARENT);
//spr.pushSprite(100, 80,TFT_BLACK);
//spr.pushSprite(100, 80);
spr.setRotation(1);
spr.setSwapBytes(true);
uint16_t* imgPtr = (uint16_t *)image1 + (RATE_X+ (RATE_Y * 320));
// Draw the background slice into the Sprite
spr.setCursor(0, RATE_H-5);
for(int i = 0;i<RATE_H;i++){
spr.pushImage(0, i, RATE_W, 1, (uint16_t *)imgPtr + i*320);
}
if(vf1 > 99.95)
spr.print("100%");
else
spr.print(vf1,1);
spr.print("%");
spr.pushSprite(RATE_X, RATE_Y);
}
}
}Loading
wemos-s2-mini
wemos-s2-mini