#include <Wire.h>
#include <LiquidCrystal_I2C.h> //�ӿ����������1602I2C��
#include <SPI.h>
#define LOG_PERIOD 2000  
#define MAX_PERIOD 60000  
 
unsigned long counts;     // GM ����
unsigned long cpm;        // CPM ����
unsigned int multiplier;  //���û������
unsigned long previousMillis;  //����ʱ��
float usv;
 
LiquidCrystal_I2C lcd(0x27, 16, 2); //����LCD��ַΪ 0x27 ��1602��ʾ����
 
void tube_impulse() {      //�Լ�
  counts++;
}
 
void setup()
{
 
pinMode(2, INPUT_PULLUP);
  counts = 0;
  cpm = 0;    //����
  multiplier = MAX_PERIOD / LOG_PERIOD;      //���������ȡ��������
  //Serial.begin(9600);
  attachInterrupt(0, tube_impulse, FALLING); //�ж�Ϊ�½��ش���
 
  //////////////////
  lcd.init();                     
 
  // ������ݵ���Ļ
  lcd.backlight();
  lcd.setCursor(5, 0);
  lcd.print("Boot...");  //дһ������boot���صĻ��棬�ṩһ���Ǹ��������ʱ�䡣
   lcd.setCursor(0, 1);
  for(int i=0;i<16;i++)
  {
  lcd.write(0xff);
  delay(75);
  }
}
 
void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > LOG_PERIOD) {
    previousMillis = currentMillis;
    cpm = counts * 30 * multiplier;//��������
    usv = float(cpm) / 581;// orig 151 ���빫ʽ���������ǿ��
    lcd.clear();
    lcd.print("CPM:");
    lcd.print(cpm);//���cpmֵ
    lcd.setCursor(1, 1);//�ڶ���
    lcd.print(usv);
   // lcd.setCursor(10, 1);
    lcd.print("  uSv/h ");//���ǿ��ֵ
    counts = 0;//��λ
    if (usv >= 10)
    {
      lcd.setCursor(10, 0);
      lcd.print("Danger");//����������10����ʾΣ��
      delay(0.1);
    }
    else if (usv < 10 && usv >= 0.52)
      {
        lcd.setCursor(9, 0);
        lcd.print("Warning");//��0.52-10�����Χ��ʾ����ȫ
        delay(0.1);
      }
      else if (usv < 0.52)
        {
          lcd.setCursor(12, 0);
          lcd.print("Safe");//�����ֵ������ʾ��ȫ
          delay(0.1);
        }
      }
}