#define LED_PIN 4
#define buttPin 2
#define buttPin2 16
#define buttPin3 17
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 44;
int lastButtState = HIGH; // Raw physical reading from previous loop
int stabilizedButtState = HIGH; // The clean, debounced button state
bool ledState = false;
bool lastLedState = false; // Tracks changes to prevent Serial spamming
unsigned long lastSelectDebounceTime = 0;
const unsigned long selectDebounceTime = 57;
int stabilizedSelect = HIGH;
int tempRead = 34;
int currTempValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(buttPin, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("======================================================================");
Serial.println("==================(TEMPERATURE OPERATION PROTOTYPE)===================");
Serial.println("======================================================================");
}
void loop() {
int currButtState = digitalRead(buttPin);
unsigned long currTime = millis();
// 1. If the physical state changed (due to press or bouncing noise), reset timer
if (currButtState != lastButtState) {
lastDebounceTime = currTime;
}
// 2. If the button has held its new state longer than our debounce delay...
if ((currTime - lastDebounceTime) >= debounceDelay) {
// ...and if that state is a brand new stable state...
if (currButtState != stabilizedButtState) {
stabilizedButtState = currButtState;
// ...and if that new stable state is LOW (meaning it was pressed)
if (stabilizedButtState == LOW) {
ledState = !ledState; // Safely toggle state
}
}
}
// Save the raw current state for the next loop comparison
lastButtState = currButtState;
// 3. Output physical state
if (ledState == true) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
// 4. (BLOCK A) ONLY print to Serial if the machine state actually changed
if (ledState != lastLedState) {
if (ledState == true) {
Serial.println("Machine State is ON...");
} else {
Serial.println("Machine State is OFF"); // TURNS an LED off state when pressed after by every first press
Serial.println();
}
lastLedState = ledState; // Update previous state tracker
}
// (Block D) for Temperature Reading & Temperature Converter
if (ledState == true) {
}
}
ON/OFF
Button 2 (Choose)
Button 3 (Select)