GeigerCounterEnclosure/geiger_counter.ino

120 lines
3.8 KiB
Arduino
Raw Normal View History

2021-08-28 16:03:33 +00:00
/*
* Geiger counter Kit could get on: https://www.aliexpress.com search: geiger counter kit
* --------------------------------------------------------------------------------------
* WHAT IS CPM?
* CPM (or counts per minute) is events quantity from Geiger Tube you get during one minute. Usually it used to
* calculate a radiation level. Different GM Tubes has different quantity of CPM for background. Some tubes can produce
* about 10-50 CPM for normal background, other GM Tube models produce 50-100 CPM or 0-5 CPM for same radiation level.
* Please refer your GM Tube datasheet for more information. Just for reference here, J305 and SBM-20 can generate
* about 10-50 CPM for normal background.
* --------------------------------------------------------------------------------------
* HOW TO CONNECT GEIGER KIT?
* The kit 3 wires that should be connected to Arduino UNO board: 5V, GND and INT. PullUp resistor is included on
* kit PCB. Connect INT wire to Digital Pin#2 (INT0), 5V to 5V, GND to GND. Then connect the Arduino with
* USB cable to the computer and upload this sketch.
*
* Author:JiangJie Zhang * If you have any questions, please connect cajoetech@qq.com
* Author:Dejvino
*
* License: MIT License
*
* Please use freely with attribution. Thank you!
*/
// include the Servo library
#include <Servo.h>
Servo myServo; // create a servo object
int const PIN_SERVO = 9;
int angle; // variable to hold the angle for the servo motor
int const PIN_TUBE 2;
int const PIN_FRESH = LED_BUILTIN;
int freshState = 0;
#include <SPI.h>
//#define LOG_PERIOD 15000 //Logging period in milliseconds, recommended value 15000-60000.
#define LOG_PERIOD 30000
#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
int const subDiv = 60;
int const subTimespan = LOG_PERIOD / subDiv;
int subCounts = 0;
int subTime = 0;
void tube_impulse(){ //subprocedure for capturing events from Geiger Kit
counts++;
}
void setup(){ //setup subprocedure
myServo.attach(PIN_SERVO); // attaches the servo on pin 9 to the servo object
myServo.write(angle);
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
pinMode(PIN_FRESH, OUTPUT);
digitalWrite(PIN_FRESH, LOW);
delay(500);
myServo.write(0);
delay(1000);
myServo.write(200);
delay(1000);
myServo.write(100);
delay(1000);
myServo.write(0);
}
void loop(){ //main cycle
unsigned long currentMillis = millis();
unsigned long timeDiff = currentMillis - previousMillis;
if (freshState == 1 && timeDiff > LOG_PERIOD / 4) {
freshState = 0;
digitalWrite(PIN_FRESH, LOW);
}
if(timeDiff > LOG_PERIOD){
previousMillis = currentMillis;
cpm = counts * multiplier;
angle = cpm;
//myServo.write(angle);
Serial.print("CPM: ");
Serial.println(cpm);
counts = 0;
digitalWrite(PIN_FRESH, HIGH);
freshState = 1;
subCounts = 0;
subTime = currentMillis;
}
int subDiff = currentMillis - subTime;
if (subDiff >= subTimespan) {
subTime = currentMillis;
int subTimeSteps = timeDiff / subTimespan;
subCounts = counts;
int change = (counts * multiplier) - (cpm * subTimeSteps / subDiv);
angle = cpm + change;
if (angle < 0) { angle = 0; }
if (angle > 200) { angle = 200; }
myServo.write(angle);
Serial.print("R: ");
Serial.print(angle);
Serial.println();
}
}