magic-coffee/firmware/firmware.ino

106 lines
2.8 KiB
Arduino
Raw Normal View History

2018-06-08 19:44:55 +02:00
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
2018-06-08 19:44:55 +02:00
#include "button.h"
#include "debounce.h"
using MainButton = Button<150, 150>;
2018-06-08 23:13:42 +02:00
#define SD_ChipSelectPin A0
#define MainButtonPin A3
#define CoffeeLedPin A2
#define EegLedPin A1
2018-06-08 19:44:55 +02:00
Debounce<20> debounce;
MainButton button;
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
void setup(){
2018-06-08 19:44:55 +02:00
tmrpcm.speakerPin = 9;
Serial.begin(9600);
Serial.println("Startshduhsd");
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
} else{
Serial.println("SD ok");
}
pinMode(MainButtonPin, INPUT);
pinMode(CoffeeLedPin, OUTPUT);
pinMode(EegLedPin, OUTPUT);
digitalWrite(CoffeeLedPin, HIGH);
2018-06-08 19:44:55 +02:00
tmrpcm.volume(2);
}
2018-06-08 19:44:55 +02:00
enum class State : uint8_t {
Idle,
MakingCoffee,
Slowing
};
2018-06-08 19:44:55 +02:00
auto state = State::Idle;
uint16_t coffeeLedTimeout = millis();
uint16_t eegLedTimeout = millis();
2018-06-08 19:44:55 +02:00
const uint16_t minEegLedDelay = 50;
const uint16_t maxEegLedDelay = 500;
uint16_t eegLedDelay = maxEegLedDelay;
uint16_t eegLedDelayTimeout = millis();
void loop() {
bool buttonPressed = digitalRead(MainButtonPin);
auto event = button.event(debounce.event(buttonPressed));
if((uint16_t) millis() - eegLedTimeout >= eegLedDelay) {
eegLedTimeout = millis();
digitalWrite(EegLedPin, !digitalRead(EegLedPin));
}
2018-06-08 19:44:55 +02:00
switch(state) {
case State::Idle:
if(event == ButtonEvent::Press || event == ButtonEvent::LongPress) {
tmrpcm.play("coffee.wav");
state = State::MakingCoffee;
coffeeLedTimeout = millis();
}
break;
case State::MakingCoffee:
if(!tmrpcm.isPlaying()) {
state = State::Slowing;
eegLedDelay = minEegLedDelay;
digitalWrite(CoffeeLedPin, HIGH);
} else {
if((uint16_t) millis() - coffeeLedTimeout >= 500) {
coffeeLedTimeout = millis();
digitalWrite(CoffeeLedPin, !digitalRead(CoffeeLedPin));
}
}
break;
case State::Slowing:
if(event == ButtonEvent::Press || event == ButtonEvent::LongPress) {
tmrpcm.play("coffee.wav");
state = State::MakingCoffee;
coffeeLedTimeout = millis();
}
2018-06-08 19:44:55 +02:00
if((uint16_t) millis() - eegLedDelayTimeout >= 300) {
eegLedDelayTimeout = millis();
if(eegLedDelay > maxEegLedDelay) {
eegLedDelay = maxEegLedDelay;
state = State::Idle;
} else {
eegLedDelay = eegLedDelay * 1.1;
}
}
break;
}
}