Skip to content

Instantly share code, notes, and snippets.

@ychalier
Created September 20, 2019 10:49
Show Gist options
  • Save ychalier/da3af35dbaa6140a7d2038375f59ce65 to your computer and use it in GitHub Desktop.
Save ychalier/da3af35dbaa6140a7d2038375f59ce65 to your computer and use it in GitHub Desktop.
Piano cocktail backend
/*
* Main script for the controller
* @project Piano Cocktail
* @author Yohan Chalier
* @date 2019-09-19
*
*/
const int nMotors = 4;
const int motorHigh = LOW;
const int motorLow = HIGH;
// PINS
const int buttonPin = 11;
const int motorPins[nMotors] = {2, 3, 4, 5};
const int pumpPin = 8;
const int redLedPin = 12;
const int greenLedPin = 13;
// DURATIONS
const int loopDelay = 100; // ms
const int recipe[nMotors] = {1000, 2000, 4000, 3000};
const unsigned long pumpDelay = 1000; // ms
const unsigned long delayTime = 3000; // ms
const unsigned long serviceTimeout = 60000; // ms
// GLOBAL
int lastButtonState, currentButtonState;
void setup() {
Serial.begin(9600);
logger("Setup", "Setup is starting");
// Setting up the button
lastButtonState = LOW;
currentButtonState = LOW;
pinMode(buttonPin, INPUT_PULLUP);
// Setting up the motors
for (int i = 0; i < nMotors; i++) {
pinMode(motorPins[i], OUTPUT);
}
// Setting up the leds
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
for (int i = 0; i < nMotors; i++) {
digitalWrite(motorPins[i], motorLow);
}
digitalWrite(pumpPin, motorLow);
logger("Setup", "Setup is finished");
}
void loop() {
currentButtonState = digitalRead(buttonPin);
if (lastButtonState == LOW && currentButtonState == HIGH) {
logger("Loop", "The button is pressed");
logger("Loop", "Activating red LED");
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
if (serve()) {
logger("Loop", "Service successfully finished");
} else {
logger("Loop", "Service encountered an error");
}
logger("Loop", "Deactivating red LED");
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
}
lastButtonState = currentButtonState;
delay(loopDelay);
}
void logger(String header, String message) {
Serial.println("[" + header + "]\t" + message);
}
bool serve() {
unsigned long timeStarts[nMotors];
unsigned long timeStart = millis();
bool finishedMotors[nMotors];
bool success = true;
logger("Serve", "Starting the service");
logger("Serve", "Starting the pump");
digitalWrite(pumpPin, motorHigh);
// Starting the motors
for (int i = 0; i < nMotors; i++) {
finishedMotors[i] = false;
logger("Serve", String("Starting motor #" + String(i)));
digitalWrite(motorPins[i], motorHigh);
timeStarts[i] = millis();
}
// Check for the motors to have done their part
while (true) {
// Checking each motor for it to be done
int nFinishedMotors = 0;
for (int i = 0; i < nMotors; i++) {
if (finishedMotors[i]) { // already done
nFinishedMotors++;
} else if (millis() - timeStarts[i] > recipe[i]) { // has just finished
finishedMotors[i] = true;
nFinishedMotors++;
logger("Serve", String("Stopping motor #" + String(i)));
digitalWrite(motorPins[i], motorLow);
}
}
// Checking if all motors are done
if (nFinishedMotors >= nMotors) { // indeed
break;
} else if (millis() - timeStart >= serviceTimeout) {
logger("Serve", "Service timed out");
success = false;
break;
} else { // we need another check
delay(loopDelay);
}
}
// Making sure all motors are stopped
for (int i = 0; i < nMotors; i++) {
digitalWrite(motorPins[i], motorLow);
}
if (success) {
logger("Serve", "Giving more time to the pump");
delay(pumpDelay);
}
logger("Serve", "Stopping the pump");
digitalWrite(pumpPin, motorLow);
logger("Serve", "Service is finished");
return success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment