Skip to content

Commit

Permalink
Included suggestions by @freaklabs Thanks!
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigeru Kobayashi authored and Shigeru Kobayashi committed May 13, 2011
1 parent bc4b0b9 commit 983f4a3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 40 deletions.
118 changes: 81 additions & 37 deletions arduino/GeigerCounterToPachube/GeigerCounterToPachube.pde
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
// Connection:
// * An Arduino Ethernet Shield
// * D3: The output pin of the Geiger counter (active low)
//
// * D3: The output pin of the Geiger counter (active low or high)
//
// Requirements:
// EthernetDHCP
// http://gkaindl.com/software/arduino-ethernet
//
//
// Reference:
// * http://www.sparkfun.com/products/9848
//
// Created by @kotobuki
// Modified by @freaklabs

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <limits.h>

#include "PrivateSettings.h"

// The IP address of api.pachube.com
byte serverIpAddress[] = {
byte serverIpAddress[] = {
173, 203, 98, 29 };

// The TCP client
Expand All @@ -33,46 +37,58 @@ unsigned long nextExecuteMillis = 0;
// Value to store counts per minute
int count = 0;

// Event flag signals when a geiger event has occurred
volatile unsigned char eventFlag = 0;

// The last connection time to disconnect from the server
// after uploaded feeds
long lastConnectionTime = 0;

// The conversion coefficient from cpm to µSv/h
float conversionCoefficient = 0;

// NetRAD - this is specific to the NetRAD board
const int speakerPin = 6; // pin number of piezo speaker
const int ledPin = 7; // pin number of event LED

void setup() {
Serial.begin(9600);
Serial.begin(57600);

// Set the conversion coefficient from cpm to µSv/h
switch (tubeModel) {
case LND_712:
// Reference:
// http://www.lndinc.com/products/348/
//
//
// 1,000CPS ≒ 0.14mGy/h
// 60,000CPM ≒ 140µGy/h
// 1CPM ≒ 0.002333µGy/h
conversionCoefficient = 0.002333;
Serial.println("Tube model: LND 712");
break;
case SMB_20:
case SBM_20:
// Reference:
// http://www.libelium.com/wireless_sensor_networks_to_control_radiation_levels_geiger_counters
conversionCoefficient = 0.00277;
Serial.println("Tube model: SMB-20");
conversionCoefficient = 0.0057;
Serial.println("Tube model: SBM-20");
break;
case J408GAMMA:
// Reference:
// http://garden.seeedstudio.com/index.php?title=Geiger_Counter
//
//
// 300CPS = 0.0084µGy/s
// 18,000CPM = 30.24µGy/h
// 1CPM = 0.00168µGy/h
conversionCoefficient = 0.00168;
Serial.println("Tube model: J408gamma");
break;
case J306BETA:
// NOTE: THIS IS DUMMY
// Reference:
// http://garden.seeedstudio.com/index.php?title=Geiger_Counter
//
// 300CPS = 0.0084µGy/s
// 18,000CPM = 30.24µGy/h
// 1CPM = 0.00168µGy/h
conversionCoefficient = 0.00168;
Serial.println("Tube model: J306beta");
break;
Expand All @@ -97,9 +113,9 @@ void setup() {
Serial.println();

// Attach an interrupt to the digital pin and start counting
//
//
// Note:
// Most Arduino boards have two external interrupts:
// Most Arduino boards have two external interrupts:
// numbers 0 (on digital pin 2) and 1 (on digital pin 3)
attachInterrupt(1, onPulse, interruptMode);
updateIntervalInMillis = updateIntervalInMinutes * 60000;
Expand All @@ -118,17 +134,31 @@ void loop() {
Serial.print(c);
}

unsigned long now = millis();
if (client.connected() && ((now - lastConnectionTime) > 30000)) {
if (client.connected() && (elapsedTime(lastConnectionTime) > 30000)) {
Serial.println();
Serial.println("Disconnecting.");
client.stop();
}

if (now < nextExecuteMillis) {
// Add any geiger event handling code here
if (eventFlag) {
eventFlag = 0; // clear the event flag for later use

Serial.println(count, DEC); // dump the current count
tone(speakerPin, 1000); // beep the piezo speaker

digitalWrite(ledPin, HIGH); // flash the LED
delay(20);
digitalWrite(ledPin, LOW);

noTone(speakerPin); // turn off the speaker pulse
}

// check if its time to update server. elapsedTime function will take into account
// counter rollover.
if (elapsedTime(lastConnectionTime) < updateIntervalInMillis) {
return;
}
nextExecuteMillis = now + updateIntervalInMillis;

Serial.println();
Serial.println("Updating...");
Expand All @@ -139,10 +169,33 @@ void loop() {
updateDataStream(countsPerMinute);
}

// On each falling edge of the Geiger counter's output,
// increment the counter
// On each falling edge of the Geiger counter's output,
// increment the counter and signal an event. The event
// can be used to do things like pulse a buzzer or flash an LED
void onPulse() {
count++;
eventFlag = 1;
}

// Since "+" operator doesn't support float values,
// convert a float value to a fixed point value
void appendFloatValueAsString(String& outString,float value) {
int integerPortion = (int)value;
int fractionalPortion = (value - integerPortion + 0.0005) * 1000;

outString += integerPortion;
outString += ".";

if (fractionalPortion < 10) {
// e.g. 9 > "00" + "9" = "009"
outString += "00";
}
else if (fractionalPortion < 100) {
// e.g. 99 > "0" + "99" = "099"
outString += "0";
}

outString += fractionalPortion;
}

void updateDataStream(float countsPerMinute) {
Expand All @@ -156,7 +209,7 @@ void updateDataStream(float countsPerMinute) {
Serial.println();
Serial.print("Connecting to Pachube...");
if (client.connect()) {
Serial.println("connected");
Serial.println("Connected");
lastConnectionTime = millis();
}
else {
Expand Down Expand Up @@ -190,24 +243,15 @@ void updateDataStream(float countsPerMinute) {
client.println(csvData);
}

// Since "+" operator doesn't support float values,
// convert a float value to a fixed point value
void appendFloatValueAsString(String& outString,float value) {
int integerPortion = (int)value;
int fractionalPortion = (value - integerPortion + 0.0005) * 1000;

outString += integerPortion;
outString += ".";
// Calculate elapsed time. this takes into account rollover.
unsigned long elapsedTime(unsigned long startTime) {
unsigned long stopTime = millis();

if (fractionalPortion < 10) {
// e.g. 9 > "00" + "9" = "009"
outString += "00";
}
else if (fractionalPortion < 100) {
// e.g. 99 > "0" + "99" = "099"
outString += "0";
if (startTime >= stopTime) {
return startTime - stopTime;
}
else {
return (ULONG_MAX - (startTime - stopTime));
}

outString += fractionalPortion;
}

6 changes: 3 additions & 3 deletions arduino/GeigerCounterToPachube/PrivateSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ byte macAddress[] = {
const int updateIntervalInMinutes = 5;

enum TubeModel {
LND_712, // LND
SMB_20, // GSTube
LND_712, // LND
SBM_20, // GSTube
J408GAMMA, // North Optic
J306BETA // North Optic
J306BETA // North Optic
};

// Tube model
Expand Down

0 comments on commit 983f4a3

Please sign in to comment.