#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 02);

bool mod = true;
bool strong = true;

void setup()
{
  Serial.begin(115200);
  // put your setup code here, to run once:
  lcd.init();  //use lcd.begin();  if failling
  lcd.backlight();
  for (int i = 2 ; i <= 5; i++) { // setting D4 to D13 as led output
    pinMode(i, OUTPUT);

  }
  lcd.setCursor(0, 0);
  lcd.print(" Hi I'm Chucky");
  lcd.setCursor(0, 1);
  lcd.print(" You wanna Play");
  delay(1000);
  lcd.clear();
}

void loop()
{
  float sensorValue = analogRead(A0);
  Serial.print("Analog Value =");
  Serial.println(sensorValue);

  float voltage = (sensorValue / 1023) * 5;
  Serial.print("Voltage =");
  Serial.print(voltage);
  Serial.println(" V");

  float wind_speed = mapfloat(voltage, 0.4, 2, 0, 32.4);
  float speed_mph = ((wind_speed * 3600) / 1609.344);
  Serial.print("Wind Speed =");
  Serial.print(wind_speed);
  Serial.println("m/s");
  Serial.print(speed_mph);
  Serial.println("mph");

  lcd.setCursor(0, 0);
  lcd.print("WS= ");
  lcd.print(wind_speed);
  lcd.setCursor(11, 0);
  lcd.print("m/s");


  if (wind_speed <= 14.9) {
    digitalWrite(2, HIGH);
    lcd.setCursor(4, 1);
    lcd.print("    ");
    lcd.setCursor(0, 1);
    lcd.print("CALM");
    mod = true;
    strong = true;
  } else {
    digitalWrite(2, LOW);
  }

  if (wind_speed >= 15.00 && wind_speed <= 25.9) {
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("MODERATE");
    strong = true;
    if (mod == true) {
      toning();
    }

  } else {
    digitalWrite(3, LOW);
  }
  if (wind_speed >= 26) {
    digitalWrite(4, HIGH);
    digitalWrite(3, LOW);
    lcd.setCursor(6, 1);
    lcd.print("    ");
    lcd.setCursor(0, 1);
    lcd.print("STRONG");
    mod = true;
    if (strong == true) {
      toning();
    }
  } else {
    digitalWrite(4, LOW);
  }


  Serial.println(" ");
  delay(300);
}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void tone1() {

  tone(5, 500, 25);
  delay(25);
  noTone(5);
}


void toning() {
  static word startTimer, waitTimer;  // waitTimer > 0 causes the timer to run, good up to 65 second intervals
  static byte procState;

  if ( waitTimer > 0 )
  {
    if ( word( millis() ) - startTimer < waitTimer )      return;     // time is not up yet
    else waitTimer = 0; // and on to the switch-case
  }

  switch ( procState )
  {
    case 0 :
      tone1();
      procState = 1;      //  run case 1 next time
      waitTimer = 1000;
      startTimer = millis();
      break;

    case 1 :
      tone1();
      procState = 2;      //  run case 2 next time
      waitTimer = 1000;
      startTimer = millis();
      break;

    case 2 :
      tone1();
      procState = 3;      //  run case 3 next time
      waitTimer = 1000;
      startTimer = millis();

      break;

    case 3 :
      procState = 0;
      mod = false;
      strong = false;
      Serial.println("to   procState = 0; ");
      break;

  }

}