#include <Wire.h>
#include <SPI.h>
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SPI.h>
#define LOG_PERIOD 15000 //Logging period in milliseconds, recommended value 15000-60000.
#define MAX_PERIOD 60000 //Maximum logging period without modifying this sketch
unsigned long counts; //variable for GM Tube events
unsigned long cpm; //variable for CPM
unsigned int multiplier; //variable for calculation CPM in this sketch
unsigned long previousMillis; //variable for time measurement
//if you need to read altitude,you need to know the sea level pressure
#define SEALEVELPRESSURE_HPA (1013.25)
//This Macro definition decide whether you use I2C or SPI
//When USEIIC is 1 means use I2C interface, When it is 0,use SPI interface
#define USEIIC 0
/*
This is tested on UNO
SPI:
SPI_SCK: D6
SPI_MISO: D7
SPI_MOSI: D8
SPI_CS: D9
I2C:
I2C_SCL: A5
I2C_SDA: A4
the default I2C address is 0x77, you can change it in Adafruit_BME280.h
*/
#if(USEIIC)
Adafruit_BME280 bme;
#else
#define SPI_SCK 6
#define SPI_MISO 7
#define SPI_MOSI 8
#define SPI_CS 9
Adafruit_BME280 bme(SPI_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
#endif
unsigned long delayTime;
void tube_impulse(){ //subprocedure for capturing events from Geiger Kit
counts++;
}
const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino
void setup() {
counts = 0;
cpm = 0;
multiplier = MAX_PERIOD / LOG_PERIOD; //calculating multiplier, depend on your log period
Serial.begin(9600);
attachInterrupt(0, tube_impulse, FALLING); //define external interrupts
bool rslt;
rslt = bme.begin();
if (!rslt) {
Serial.println("Init Fail,Please Check your address or the wire you connected!!!");
while (1);
}
/* Serial.println("Init Success");
Serial.println("Temperature Pressure Humidity");
delayTime = 40;
*/
}
void loop() {
printValues();
delay(15000);
}
void printValues() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > LOG_PERIOD){
previousMillis = currentMillis;
cpm = counts * multiplier;
Serial.print("Clicks/Minute: ");
Serial.print(cpm);
Serial.println();
counts = 0;
}
Serial.print("temperature:");
Serial.print(bme.readTemperature());
Serial.print("*C ");
Serial.print("pressure:");
Serial.print(bme.readPressure()/100.0F);
Serial.print("hPa ");
Serial.print("humidity:");
Serial.print(bme.readHumidity());
Serial.print("% ");
Serial.print("altitude:");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println("m");
}
void Initialize_PlxDaq()
{
Serial.println("CLEARDATA"); //clears up any data left from previous projects
// Serial.println("LABEL,Date,Time,Temperature,Humidity"); //always write LABEL, to indicate it as first line
}
void Write_SDcard()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// dataFile.print(rtc.getDateStr()); //Store date on SD card
// dataFile.print(","); //Move to next column using a ","
// dataFile.print(rtc.getTimeStr()); //Store date on SD card
// dataFile.print(","); //Move to next column using a ","
dataFile.print(bme.readTemperature()); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(bme.readHumidity()); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.println(); //End of Row move to next row
dataFile.close(); //Close the file
}
else
Serial.println("OOPS!! SD card writing failed");
}
void Initialize_SDcard()
{
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
dataFile.close();
}
}