#include "User_Setup.h"
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Adafruit_NeoPixel.h>
#include "NotoSansBold15.h"
#include "NotoSansBold36.h"
#include "CourierB24.h"
#include "Unicode_Test_72.h"
#include "background.h"
#include "FreeSansBold36.h"
#define BRIGHTNESS 255
// --- Pin Definitions ---
#define SOLENOID_PIN 12
#define Ain 8
#define BUZZER_PIN 15
#define RGB_D 16
#define NUM_LEDS 17
#define RST 40
#define T5 38
#define T10 37
#define T15 36
#define T20 34
#define T25 18
#define T30 17
#define PLUS1 39
// --- UI Constants ---
#define RATE_X 100
#define RATE_Y 110
#define RATE_W 200
#define RATE_H 56
// --- Objects ---
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);
TFT_eSprite sprT = TFT_eSprite(&tft);
Adafruit_NeoPixel strip(NUM_LEDS, RGB_D, NEO_GRB + NEO_KHZ800);
// --- Variables ---
int oldV = -1;
int v;
float vf1;
unsigned long tNow;
unsigned long tRead;
unsigned int timeout = 0;
byte minutes, seconds;
unsigned long debounce;
bool lastSolenoidState = false;
// --- Functions ---
// Quick beep using tone
void beep(int duration) {
tone(BUZZER_PIN, 500);
delay(duration);
noTone(BUZZER_PIN);
}
// LED update logic: 0% to 100% gradient
void updateLEDs(float percent) {
int ledsToLight = map(constrain((int)percent, 0, 100), 0, 100, 0, NUM_LEDS);
// --- ADD THIS DEBUGGING ---
Serial.print("Percent passed to LEDs: ");
Serial.print(percent);
Serial.print(" | ledsToLight calculated: ");
Serial.println(ledsToLight);
// --------------------------
strip.clear();
for (int i = 0; i < ledsToLight; i++) {
// Green (0, 255, 0) to Red (255, 0, 0) gradient
uint8_t red = map(i, 0, NUM_LEDS - 1, 0, 255);
uint8_t green = map(i, 0, NUM_LEDS - 1, 255, 0);
strip.setPixelColor(i, strip.Color(red, green, 0));
}
strip.show();
}
void setup() {
Serial.begin(115200);
// Hardware Init
strip.begin();
strip.show();
strip.setBrightness(BRIGHTNESS); // Set BRIGHTNESS to about 1/5 (max = 255)
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(T20, INPUT_PULLUP);
pinMode(T25, INPUT_PULLUP);
pinMode(T30, INPUT_PULLUP);
pinMode(PLUS1, INPUT_PULLUP);
// TFT Init
tft.begin();
tft.setRotation(3);
tft.setSwapBytes(true);
tft.pushImage(0, 0, 320, 240, (uint16_t *)image1);
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);
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 255, 0), 50); // Green
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
tNow = millis();
tRead = millis();
}
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) {
digitalWrite(SOLENOID_PIN, HIGH);
beep(150);
} else {
digitalWrite(SOLENOID_PIN, LOW);
beep(500);
delay(100);
beep(500);
}
lastSolenoidState = currentSolenoidState;
}
// --- Timer Update (Every 1 Second) ---
if (millis() - tNow >= 1000) {
tNow = millis();
if (timeout > 0) timeout--;
sprT.pushImage(0, 0, 120, 24, (uint16_t *)image1 + 200 + (45 * 320)); // Refresh background
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);
}
// --- Sensor & LED Update (Every 500ms) ---
if (millis() - tRead >= 500) {
tRead = millis();
v = analogRead(Ain);
if (abs(oldV - v) > 5) {
vf1 = v / 81.92;
oldV = v;
// Update LED Strip
updateLEDs(vf1);
// Refresh Sprite
spr.setRotation(1);
spr.setSwapBytes(true);
uint16_t* imgPtr = (uint16_t *)image1 + (RATE_X + (RATE_Y * 320));
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);
}
}
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}Loading
wemos-s2-mini
wemos-s2-mini