From 42f9726b6fb134b61816470b0cb5172020030fae Mon Sep 17 00:00:00 2001 From: Toumal Date: Fri, 13 Sep 2013 21:11:55 +0200 Subject: [PATCH] Added interrupt handling and speaker output for radiation detection clicks --- RadiationWatch.cpp | 28 +++++++++++++++------------- RadiationWatch.h | 8 ++++---- readme.md | 8 +++++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/RadiationWatch.cpp b/RadiationWatch.cpp index 7682aa5..fd3b2fe 100644 --- a/RadiationWatch.cpp +++ b/RadiationWatch.cpp @@ -6,12 +6,20 @@ #include "Arduino.h" #include "RadiationWatch.h" -RadiationWatch::RadiationWatch(int signPin, int noisePin) : _signPin(signPin), _noisePin(noisePin) +int signCount; //Counter for Radiation Pulse + +//Called via Interrupt when a radiation pulse is detected +void triggerRadiationPulse() { + signCount++; + tone(8, 800, 1); // Output classic geiger counter tick noise +} + +RadiationWatch::RadiationWatch(int signPin, int noisePin, int signIRQ) : _signPin(signPin), _noisePin(noisePin), _signIRQ(signIRQ) { + signCount = 0; _prevTime = 0; index = 0; - signCount = 0; noiseCount = 0; sON = 0; @@ -38,6 +46,10 @@ void RadiationWatch::setup() pinMode(_noisePin,INPUT); digitalWrite(_noisePin,HIGH); + //Attach interrupt handler to catch incoming radiation pulses, + //and execute triggerRadiationPulse() when this happens. + attachInterrupt(_signIRQ, triggerRadiationPulse, FALLING); + //Initialize cpmHistory[] for(int i = 0; i < kHistoryCount;i++ ) { @@ -61,19 +73,9 @@ void RadiationWatch::loop() { // Raw data of Radiation Pulse: Not-detected -> High, Detected -> Low int sign = signPin(); - // Raw data of Noise Pulse: Not-detected -> Low, Detected -> High int noise = noisePin(); - //Radiation Pulse normally keeps low for about 100[usec] - if(sign==0 && sON==0) - {//Deactivate Radiation Pulse counting for a while - sON = 1; - signCount++; - }else if(sign==1 && sON==1){ - sON = 0; - } - //Noise Pulse normally keeps high for about 100[usec] if(noise==1 && nON==0) {//Deactivate Noise Pulse counting for a while @@ -203,7 +205,7 @@ double RadiationWatch::uSvhError() } } -RadiationWatchPrinter::RadiationWatchPrinter(int signPin, int noisePin) : RadiationWatch(signPin, noisePin) +RadiationWatchPrinter::RadiationWatchPrinter(int signPin, int noisePin, int signIRQ) : RadiationWatch(signPin, noisePin, signIRQ) { } diff --git a/RadiationWatch.h b/RadiationWatch.h index 049d551..c9a27eb 100644 --- a/RadiationWatch.h +++ b/RadiationWatch.h @@ -11,7 +11,7 @@ class RadiationWatch { public: - RadiationWatch(int signPin, int noisePin); + RadiationWatch(int signPin, int noisePin, int signIRQ); void setup(); void loop(); @@ -31,7 +31,7 @@ class RadiationWatch double cpmTime(); - static const unsigned int kHistoryCount = 200; + static const unsigned int kHistoryCount = 50; // was 200 double _cpmHistory[kHistoryCount]; //History of count rates @@ -39,10 +39,10 @@ class RadiationWatch int _signPin; //Radiation Pulse (Yellow) int _noisePin; //Vibration Noise Pulse (White) + int _signIRQ; //The IRQ number for the radiation pulse pin (depends on Arduino model) int index; //Number of loops - int signCount; //Counter for Radiation Pulse int noiseCount; //Counter for Noise Pulse int sON;//Lock flag for Radiation Pulse @@ -63,7 +63,7 @@ class RadiationWatch class RadiationWatchPrinter : public RadiationWatch { public: - RadiationWatchPrinter(int signPin, int noisePin); + RadiationWatchPrinter(int signPin, int noisePin, int signIRQ); virtual void printKey(); virtual void printStatus(); diff --git a/readme.md b/readme.md index f0aa60e..165aebe 100644 --- a/readme.md +++ b/readme.md @@ -2,4 +2,10 @@ An Arduino library to interface with the [Radiation Watch](http://www.radiation-watch.org/) Pocket Geiger -Based on the [Radiation Watch sample code](http://radiation-watch.sakuraweb.com/share/ARDUINO.zip). +Based on [thomasaw's library](https://github.com/thomasaw/RadiationWatch) and the [Radiation Watch sample code](http://radiation-watch.sakuraweb.com/share/ARDUINO.zip) + +Changes in this version: + +x) Changed from using loops for the radiation detection to interrupts + +x) Added support for classic radiation "click" via speaker