#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// # define RED_LED 5 // danger led
// # define YELLOW_LED 4 // warning led
// # define Foot_SW 3 //foot switch interrupt for reset counts
# define pin_Tick 2
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// #include <LiquidCrystal_I2C.h> //从库管理器添加1602I2C库
# define LOG_PERIOD 1000 //Logging period in milliseconds, recommended value 15000-60000.
# define MAX_PERIOD 60000 //Maximum logging period
unsigned long counts; // GM 变量
unsigned long cpm; // CPM 变量
unsigned int multiplier; //设置换算变量
unsigned long previousMillis; //测量时间
int conversion_factor;
float usv;
// LiquidCrystal_I2C lcd(0x27, 16, 2); //设置LCD地址为 0x27 (1602显示器)
void tick() // GM tube impulse
{
counts++;
}
//**************************** Setup *************************************//
void setup()
{
Serial.begin(9600);
// pins settings
// pinMode(RED_LED, OUTPUT);
// pinMode(YELLOW_LED, OUTPUT);
// pinMode(Foot_SW, INPUT_PULLUP);
pinMode(pin_Tick, INPUT_PULLUP);
// OLED setting
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
//oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); // OLED display initialize
delay(100); // This delay is needed to let the display to initialize
oled.display();
delay(100);
oled.clearDisplay();
delay(100);
// basic settings
counts = 0;
cpm = 0; //计数
multiplier = MAX_PERIOD / LOG_PERIOD; //计算乘数,取决于周期
attachInterrupt(0, tick, FALLING); //中断为下降沿触发
conversion_factor = 151; // setting GM tube conversion factor
/* // LCD boot animation
lcd.init();
lcd.backlight();
lcd.setCursor(5, 0);
lcd.print("Boot...");
lcd.setCursor(0, 1);
for(int i=0;i<16;i++)
{
lcd.write(0xff);
delay(250);
}
*/
}
//********************************* Loop **********************************//
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > LOG_PERIOD)
{
previousMillis = currentMillis;
cpm = counts * multiplier; //得出技术次数
usv = float(cpm) / conversion_factor; //带入公式计算出辐射强度
/* // 1602 LCD display
lcd.clear();
lcd.print("CPM=");
lcd.print(cpm); //输出cpm值
lcd.setCursor(0, 1); //第二行
lcd.print(usv);
lcd.print(" uSv/h"); //输出强度值
counts = 0; // only uSv range display 复位
*/
//oled.display();
//oled.clearDisplay();
oled.setTextColor(WHITE);
oled.setTextSize(1);
oled.setCursor(4, 10);
oled.print("CPM:");
oled.print(cpm); //输出cpm值
oled.setCursor(2, 1);
oled.print("count:");
oled.print(counts); //输出cpm值
oled.setCursor(1, 20); //第二行
oled.print(usv);
oled.print("uSv/h"); //输出强度值
oled.display();
// counts = 0; // only uSv range display 复位
/*
// (uSv, LCD) normal, warning, danger range display setting
if (usv >= 10)
{
lcd.setCursor(9, 0);
lcd.print("Danger!");//如果辐射大于10则显示危险
delay(0.1);
}
else if (usv < 10 && usv >= 0.52)
{
lcd.setCursor(10, 0);
lcd.print("Unsafe");//在0.52-10这个范围显示不安全
delay(0.1);
}
else if (usv < 0.52)
{
lcd.setCursor(10, 0);
lcd.print("Safety");//在这个值以下显示安全
delay(0.1);
}
*/
// (counts, OLED) normal, warning, danger range display setting
if (counts >= 10000)
{
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(10, 18);
oled.print("Danger!"); //如果辐射大于10则显示危险
delay(0.1);
}
else if (usv < 10000 && usv >= 5000)
{
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(10, 18);
oled.print("Warning"); //如果辐射大于10则显示危险
delay(0.1);
}
else if (usv < 5000)
{
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(10, 18);
oled.print(" "); //如果辐射大于10则显示危险
delay(0.1);
}
counts = 0; // only OLED range display 复位
/*
// (counts, led) normal, warning, danger display setting
if (counts >= 10000)
{
pinMode(RED_LED) HIGH; //danger red led on
delay(0.1);
}
else if (counts < 10000 && counts >= 5000)
{
pinMode(YELLOW_LED) HIGH; //warning yellow led on
delay(0.1);
}
else if (counts < 5000)
{
pinMode(RED_LED) LOW; //danger red led off
pinMode(YELLOW_LED) LOW; //warning yellow led off
}
// counts = 0; // only led range display 复位
*/
}
}