#include <Toggle.h>
const byte POT_PIN = A1;
const byte errorLedPin = 2;
const byte dishLedPins[] = {3, 4, 5};
const byte startButtonPin = 6;
const byte buzzerPin = 7;
const byte cookingLedPin = 8;
const byte ledCnt = sizeof dishLedPins / sizeof * dishLedPins;
const char * ledText[ledCnt] = {"Chicken", "Vegetables", "Coffee/soup"};
const byte blinkCnt[ledCnt] = {10, 6, 4};
Toggle button;
bool isCooking = false;
int currentlyCookingIndex;
int remainingBlinkCount;
const unsigned long blinkHalfPeriod = 250;
unsigned long blinkChrono = 250;
int oldMenuIndex = -1;
void setup() {
button.begin(startButtonPin);
for (int pinIndex = 0; pinIndex < ledCnt; pinIndex++) pinMode(dishLedPins[pinIndex], OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(cookingLedPin, OUTPUT);
pinMode(errorLedPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
int newMenuIndex = map(analogRead(POT_PIN), 0, 1024, 0, ledCnt); // generate a number between 0 and ledCnt-1 (ledCnt is not possible as 1024 is not possible)
bool s = (digitalRead(startButtonPin) == LOW); // true if pressed
if (newMenuIndex != oldMenuIndex) {
tone(buzzerPin, 2000u, 100);
if (oldMenuIndex >= 0) digitalWrite(dishLedPins[oldMenuIndex], LOW);
digitalWrite(dishLedPins[newMenuIndex], HIGH);
Serial.println(ledText[newMenuIndex]);
oldMenuIndex = newMenuIndex;
}
button.poll();
if (button.onPress()) {
if (isCooking) {
// stop
tone(buzzerPin, 200u, 100);
Serial.println("Cooking stopped");
digitalWrite(cookingLedPin, LOW);
isCooking = false;
} else {
tone(buzzerPin, 200u + 300u * newMenuIndex, 100);
Serial.print("Now cooking : ");
Serial.println(ledText[oldMenuIndex]);
remainingBlinkCount = 2 * blinkCnt[oldMenuIndex] - 1;
currentlyCookingIndex = oldMenuIndex;
digitalWrite(cookingLedPin, HIGH);
isCooking = true;
blinkChrono = millis();
}
}
if (isCooking) {
if (millis() - blinkChrono >= blinkHalfPeriod) {
digitalWrite(cookingLedPin, digitalRead(cookingLedPin) == HIGH ? LOW : HIGH);
blinkChrono = millis();
if (--remainingBlinkCount <= 0) {
tone(buzzerPin, 200u, 50); delay(50);
tone(buzzerPin, 400u, 50);
isCooking = false;
}
}
}
}
CHICKEN
VEGETABLES
SOUP
Error
Cooking
Start