
#include <LCD16x2.h>
#include <Wire.h>

LCD16x2 lcd;
const int  inputPin = 2;
int inputstate = HIGH;
int lastinputstate = HIGH;
int buttons;
int button_state = 0;
unsigned long previousTime = 0;
unsigned long time_begin = 0;
int interval=5000;
float bpm = 0.0;
float bpm_old = 0.0;


void setup(){
  Wire.begin();
  Serial.begin(9600);
  
  pinMode(inputPin, INPUT);
  
  lcd.lcdClear();
  
  lcd.lcdGoToXY(1,1);
  lcd.lcdWrite("Welcome!");
  
  lcd.lcdGoToXY(10,1);
  lcd.lcdWrite("MODE:");

  lcd.lcdGoToXY(1,2);
  lcd.lcdWrite("HBR is:");

  lcd.lcdGoToXY(14,2);
  lcd.lcdWrite("bpm");
}
void loop(){
  
  buttons = lcd.readButtons();
  if (buttons == 14)  
    button_state = 14;
  else if (buttons == 13) 
    button_state = 13;
  else if (buttons == 11)  
    button_state = 11;
  else if (buttons == 7)  
    button_state = 7;

  inputstate = digitalRead(inputPin);

  if (inputstate != lastinputstate) {
    // if the state has changed 
    if (inputstate == LOW) { // falling edge
      unsigned long currentTime = millis();
      unsigned long time_diff = currentTime - previousTime;
      previousTime = currentTime;
      bpm =  60000.0/ time_diff;
      lcd.lcdGoToXY(16,1);
      if (button_state == 14)  
       {
          lcd.lcdWrite("1");
          lcd.lcdGoToXY(8,2);
          if (bpm_old != bpm )
            {
              bpm_old = bpm;
              lcd.lcdWrite(bpm,2);
            }
          
       }
      else if (button_state == 7)  
       {
          lcd.lcdWrite("0");
          lcd.lcdGoToXY(8,2);     
          lcd.lcdWrite("      ");
       }
      else if (button_state == 13) 
        {
          lcd.lcdWrite("2");
          lcd.lcdGoToXY(8,2);
          if (bpm_old - bpm > 2 || bpm_old - bpm < -2)
            {
              lcd.lcdWrite(bpm,2);
              bpm_old = bpm;
            }
        }
      else if (button_state == 11)  
        {
          lcd.lcdWrite("3");
          unsigned long time_last=0;
          time_begin = millis();
          int button_p = 11;
          int count=0;
          while((millis()-time_begin)<interval)
          {
            buttons = lcd.readButtons();
            if (buttons == 14)  
            { 
              button_state = 14;
              break;
            }
            else if (buttons == 13) 
            {  
              button_state = 13;
              break;
            }
            else if (buttons == 7)  
            {
              button_state = 7;
              break;
            }
            
            inputstate = digitalRead(inputPin);
      
            if (inputstate != lastinputstate) {
              // if the state has changed 
              if (inputstate == LOW) { // falling edge
                  count++;
                  time_last = millis();
                  button_p = 7;
                } 
                lastinputstate = inputstate;
              }
      
      
             delay(10);
          }
             
          time_last = time_last - time_begin;
          bpm = count * 60 / (time_last/ 1000.0);
          lcd.lcdGoToXY(8,2);
          if (bpm_old != bpm )
            {
              lcd.lcdWrite("      ");
              bpm_old = bpm;
            }
          lcd.lcdWrite(bpm,2);
        }
      else
        {
          lcd.lcdWrite("0");
        }

    } 
   lastinputstate = inputstate;
  }
   
  delay(10);
}
//#include <LCD16x2.h>
//#include <Wire.h>
//
//LCD16x2 lcd;
//const int  inputPin = 2;
//int inputstate = HIGH;
//int lastinputstate = HIGH;
//int buttons;
//int button_state = 0;
//unsigned long previousTime = 0;
//unsigned long time_begin = 0;
//int interval=5000;
//float bpm = 0.0;
//float bpm_old = 0.0;
//
//
//void setup(){
//  Wire.begin();
//  Serial.begin(9600);
//  
//  pinMode(inputPin, INPUT);
//  
//  lcd.lcdClear();
//  
//  lcd.lcdGoToXY(1,1);
//  lcd.lcdWrite("Welcome!");
//  
//  lcd.lcdGoToXY(10,1);
//  lcd.lcdWrite("MODE:");
//
//  lcd.lcdGoToXY(1,2);
//  lcd.lcdWrite("HBR is:");
//
//  lcd.lcdGoToXY(14,2);
//  lcd.lcdWrite("bpm");
//}
//void loop(){
//  
//  buttons = lcd.readButtons();
//  if (buttons == 14)  
//    button_state = 14;
//  else if (buttons == 13) 
//    button_state = 13;
//  else if (buttons == 11)  
//    button_state = 11;
//  else if (buttons == 7)  
//    button_state = 7;
//
//  inputstate = digitalRead(inputPin);
//
//  if (inputstate != lastinputstate) {
//    // if the state has changed 
//    if (inputstate == LOW) { // falling edge
//      unsigned long currentTime = millis();
//      unsigned long time_diff = currentTime - previousTime;
//      previousTime = currentTime;
//      bpm =  60000.0/ time_diff;
//      Serial.print("BPM time: ");
//      Serial.println(bpm);
//      Serial.print('\n');
//    } 
//   lastinputstate = inputstate;
//  }
//  
//    
//  lcd.lcdGoToXY(16,1);
//  if (button_state == 14)  
//   {
//      lcd.lcdWrite("1");
//      lcd.lcdGoToXY(8,2);
//      if (bpm_old != bpm )
//        {
//          lcd.lcdWrite("      ");
//          bpm_old = bpm;
//          lcd.lcdWrite(bpm,2);
//        }
//      
//   }
//  else if (button_state == 7)  
//   {
//      lcd.lcdWrite("0");
//      lcd.lcdGoToXY(8,2);     
//      lcd.lcdWrite("      ");
//   }
//  else if (button_state == 13) 
//    {
//      lcd.lcdWrite("2");
//      lcd.lcdGoToXY(8,2);
//      if (bpm_old - bpm > 2 || bpm_old - bpm < -2)
//        {
//          lcd.lcdWrite(bpm,2);
//          bpm_old = bpm;
//        }
//    }
//  else if (button_state == 11)  
//    {
//      lcd.lcdWrite("3");
//      unsigned long time_last=0;
//      time_begin = millis();
//      int button_p = 11;
//      int count=0;
//      while((millis()-time_begin)<interval)
//      {
//        buttons = lcd.readButtons();
//        if (buttons == 14)  
//          button_state = 14;
//        else if (buttons == 13) 
//          button_state = 13;
//        else if (buttons == 7)  
//          button_state = 7;
//        
//        inputstate = digitalRead(inputPin);
//
//        if (inputstate != lastinputstate) {
//          // if the state has changed 
//          if (inputstate == LOW) { // falling edge
//              count++;
//              time_last = millis();
//              button_p = 7;
//              Serial.print("hit time: ");
//              Serial.println(time_last);
//            } 
//            lastinputstate = inputstate;
//          }
//
// 
//         delay(10);
//      }
//         
//      time_last = time_last - time_begin;
//      Serial.print(" time last: ");
//          Serial.println(time_last);
//      Serial.print(" count: ");
//          Serial.println(count);
//          Serial.print('\n');
//      bpm = count * 60 / (time_last/ 1000.0);
//      lcd.lcdGoToXY(8,2);
//      if (bpm_old != bpm )
//        {
//          lcd.lcdWrite("      ");
//          bpm_old = bpm;
//        }
//      lcd.lcdWrite(bpm,2);
//    }
//  else
//    {
//      lcd.lcdWrite("0");
//    }
//
//  
//  delay(10);
//
//}
