#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16,2);

#define LED_RED 13//2
#define LED_GREEN  3
#define LED_BLUE  4

#define SERVO_PIN 7

String voice;

// Create a servo object
Servo servo1;

void right() {
  lcd.clear();
  lcd.print("Right");
  digitalWrite (LED_RED, HIGH);
  delay(500);
  digitalWrite (LED_RED, LOW);
  delay(500);
  lcd.print("");
}
void left() {
  lcd.clear();
  lcd.print("Left");
  digitalWrite (LED_RED, HIGH);
  delay(500);
  digitalWrite (LED_RED, LOW);
  delay(500);
  digitalWrite (LED_RED, HIGH);
  delay(500);
  digitalWrite (LED_RED, LOW);
  lcd.print("");
}
void sound() {
  lcd.clear();
  lcd.print("Sound");
  for(int i=0; i<10; i++){
    digitalWrite (10, HIGH);
    delay(100);
    digitalWrite (10, LOW);
    delay(100);
    lcd.print("");
  }
}

void up() {
  lcd.clear();
  lcd.print("Up");
  for(int i=0; i<3; i++){
    digitalWrite (LED_RED, HIGH);
    delay(500);
    digitalWrite (LED_RED, LOW);
    delay(500);
  }
}
void down() {
  lcd.print("Down");
  digitalWrite (LED_RED, HIGH);
  lcd.print("");
}


void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  Serial.print("pabotaem");
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(10, OUTPUT);


  servo1.attach(SERVO_PIN);
  servo1.write(90);
  delay(10);
}

void loop() {
  while (Serial.available()) {
    delay(10);
    char c = Serial.read();
    if (c == '#')
    {
      break;
    }
    voice += c;
  }
  if (voice.length() > 0) {
    Serial.println(voice);
    if (voice == "right")
    {
      right() ;
    }
    else if (voice == "left")
    {
      left() ;
    }
    else if (voice == "up") {
      up();
    }
    else if (voice == "down") {
      down();
    }
     else if (voice == "sound") {
      sound();
    }
    voice = "";
 
 } }