#include <LiquidCrystal_I2C.h>
#include "song.h"
#include "disco.h"

// Speaker
#define SPEAKER 3
// Motors
#define WATER 10
#define JUICE 11
#define COFFEE 12
// Joystick
#define SELECT_PIN 2
#define VERTICAL_PIN A0
#define HORIZONTAL_PIN A1
// LCD
LiquidCrystal_I2C lcd(0x27,20,4);

// Drink related variables
String available_drinks[] = {"WATER", "JUICE", "COFFEE"};
int motors[] = {WATER, JUICE, COFFEE};
int selected_drink = 0;

// Keeps track of the music
int music_progress = 0;

// -----------------------------------------------------------

// Plays the given note
void play_note() {
  tone(SPEAKER, ode_to_joy[music_progress]);
  delay(ode_to_joy[music_progress+1]);
  noTone(SPEAKER);
  music_progress = (music_progress + 2) % 124;
}

// Makes lights go crazy
void disco_lights() {
  if (disco_progress < 10) rainbow();
  else if (disco_progress < 20) fade();
  else if (disco_progress < 30) crazy();
  disco_progress = (disco_progress + 1) % 30;
}

// Updates the LCD with the current selected drink
void update_lcd() {
  lcd.clear();
  lcd.print("Selected: ");
  lcd.print(available_drinks[selected_drink]);
}

// Rotates the motors to pour your drink
void pour_drink() {
  lcd.clear();
  lcd.print("Now pouring your    drink: ");
  lcd.print(available_drinks[selected_drink]);
  lcd.print("...");

  for (int i = 0; i < 100; i++) {
    digitalWrite(motors[selected_drink], HIGH);
    digitalWrite(motors[selected_drink], LOW);
    delay(5);
  }

  delay(1000);
  lcd.clear();
  lcd.print("Finished pouring.   Have a nice day!");
}

// Rotates through the drinks
void change_drink_selection() {
  int horz = analogRead(HORIZONTAL_PIN);
  int vert = analogRead(VERTICAL_PIN);
  if (horz > 700) {
    selected_drink = (selected_drink + 1) % 3;
    update_lcd();
  }
  if (horz < 300) {
    selected_drink = (selected_drink + 2) % 3;
    update_lcd();
  }
  if (digitalRead(SELECT_PIN) == LOW) {
    pour_drink();
  }
}

void setup() {
  // Set motors
  pinMode(WATER, OUTPUT);
  pinMode(JUICE, OUTPUT);
  pinMode(COFFEE, OUTPUT);

  pinMode(SPEAKER, OUTPUT);
  // Set joystick
  pinMode(VERTICAL_PIN, INPUT);
  pinMode(HORIZONTAL_PIN, INPUT);
  pinMode(SELECT_PIN, INPUT_PULLUP);

  // Set LCD
  lcd.init();
  lcd.backlight();
  update_lcd();

  // Set LED-ring
  ring.begin();
  ring.show();
  ring.setBrightness(255);
}

void loop() {
  change_drink_selection();
  play_note();
  disco_lights();
  delay(50);
}
A4988
A4988
A4988