0% found this document useful (0 votes)
2 views9 pages

GigerCounter Arduino Program

This document describes a program for the RHElectronics Radiation Detector compatible with Arduino, utilizing components like the Arduino Mega 256 and Adafruit GPS Shield. It outlines the setup and main loop functions to read GPS data, display it on a TFT screen, and log data to an SD card, while also integrating a Geiger counter for radiation detection. The program emphasizes that while it is functional, no claims are made regarding its accuracy, and users assume responsibility for its use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

GigerCounter Arduino Program

This document describes a program for the RHElectronics Radiation Detector compatible with Arduino, utilizing components like the Arduino Mega 256 and Adafruit GPS Shield. It outlines the setup and main loop functions to read GPS data, display it on a TFT screen, and log data to an SD card, while also integrating a Geiger counter for radiation detection. The program emphasizes that while it is functional, no claims are made regarding its accuracy, and users assume responsibility for its use.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

/*

* This program suports the RHElectronics Radiation Detector Arduino Compatable version 3.1
* Computer components;
* Arduino Mega 256 (needed two Serial ports for debugger and GPS)
* Adafruit GPS Shield
* Adafruit 2.8 TFT display
* All of the operations in the main loop are fit into less than 900ms allowing the processing to
finish before the next
* GPS message is received.
* GPS is set up to sent locations at a 1 second rate. This works well since it has a good clock.
*
*This program appears to work but no clame is made for its accuracy.
*Anyone may use it but no responsibility is assumed for its use.
*Its a good place to start if you want to build your own code
*/

#include <SPI.h>
#include <SD.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"
#include <Adafruit_GPS.h>

#define _cs 10
#define _dc 9
#define _rst 2

// SPI bus uses MOSI - pin 11, ISO - 12, CLK - 13


// SD chipselect - 4
// Display chipselect - 10
// Display data - 9
// Display rst -8

// Use hardware SPI for display


Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);

int i = 0;
int j = 0;
int k = 0;
int x = 0;
int y = 0;
int z = 0;
int p = 0;
int q = 0;
int r = 0;
long int time;

// set up variables using the SD utility library functions:


Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = 4;

// Giger Counter Support Values


const byte interruptPin = 21;
int timeAdjustment;
volatile int pointer = 0;
volatile int counts[61];
int sum;
float sieverts;
File myFile;
// set up read/write function
char Alpha[] = ("");
String WriteDataRecord = "Null Record";
String ReadDataRecord = "Read Data Record";
String WriteFileName = "LogData.txt";
String ReadFileName = "LogData.txt";
String RemoveFileName = "LogData.txt";
int records = 0;
int writeSDflag = 0;
int displayDataflag = 0;
int hours = 0;
int minutes = 0;
int seconds = 0, ms = 0;
int days = 0;
int months = 0;
int years = 0;
int signalLock = 0;
int signalQual = 0;
float Latitude = 0;
float Longitude = 0;
char LatEW = 0;
char LonNS = 0;
int Speed = 0;
int Angle = 0;
int Altitude = 0;
int Satellites = 0;

//GPS TX to Arduino Due Serial1 RX pin 19


//GPS RX to Arduino Due Serial1 TX pin 18
#define mySerial Serial1

Adafruit_GPS GPS(&mySerial);

// Set to 'true if you want to debug and listen to the raw GPS sentences.
#define GPSECHO false

// this keeps track of whether we're using the interrupt - off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean);

// this keeps track of whether we're using the interrupt - off by default!
//boolean usingInterrupt = false;
//void useInterrupt(boolean);

void setup() {
// put your setup code here, to run once:

// Open serial communications and wait for port to open:


Serial.begin(9600);
// while (!Serial) {};

// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
Serial1.begin(9600);
while (!Serial1) {};
delay(1000);

// Initialize the Giger Counter Interrupts


pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), tick, LOW);

tft.begin();
tft.fillScreen(ILI9340_BLACK);

//uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including
altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);

// Set the update rate


GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// GPS.sendCommand(PMTK_SET_NMEA_UPDATE_200_MILLIHERTZ); // 0.5Hz update
rate
// GPS.sendCommand(PMTK_SET_NMEA_UPDATE_100_MILLIHERTZ); // 0.1Hz update rate

//Request updates on antenna status, comment out to keep quiet


//GPS.sendCommand(PGCMD_ANTENNA);

Serial.println("");
Serial.println("Initialization Completed");

int S = SSDDiscoverFunction();
S = SSDStartSSDFunction();
S = SSDListDirFunction();
S = SSDRemoveFunction();
if(S > 0) Serial.println("Remove Error");
}

void loop() {
// put your main code here, to run repeatedly:

time=micros();

char c = GPS.read();
//if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// WriteDataRecord = GPS.lastNMEA();
if (!GPS.parse(GPS.lastNMEA())) return;
hours = GPS.hour - 5;
if(hours < 0) hours += 24;
minutes = GPS.minute;
seconds = GPS.seconds;
ms = GPS.milliseconds;
days = GPS.day;
months = GPS.month;
years = GPS.year;
signalLock = GPS.fix;
signalQual = GPS.fixquality;
if (signalLock) {
Latitude = GPS.latitude;
LatEW = GPS.lat;
Longitude = GPS.longitude;
LonNS = GPS.lon;
Speed = GPS.speed;
Angle = GPS.angle;
Altitude = GPS.altitude * 3.280839895;
Satellites = GPS.satellites;
}
displayDataflag = displayDataflag + 1;
}
while(displayDataflag > 1){
tft.setRotation(3);
tft.setTextColor(ILI9340_WHITE);
tft.setTextSize(2);

for (i=10;i<26;i++){tft.drawFastHLine(70,i,150,ILI9340_BLACK);}
tft.setCursor(0, 10);
tft.print("Time: ");
tft.print(hours, DEC); tft.print(':');
tft.print(minutes, DEC); tft.print(':');
tft.print(seconds, DEC); tft.print(':');
tft.println(ms, DEC);

for (i=26;i<42;i++){tft.drawFastHLine(70,i,150,ILI9340_BLACK);}
tft.print("Date: ");
tft.print(months, DEC); tft.print('/');
tft.print(days, DEC); tft.print("/20");
tft.println(years, DEC);

for (i=42;i<58;i++){tft.drawFastHLine(150,i,60,ILI9340_BLACK);}
tft.print("Signal Lock: "); tft.println(signalLock);
for (i=58;i<74;i++){tft.drawFastHLine(150,i,60,ILI9340_BLACK);}
tft.print("Signal Qual: "); tft.println(signalQual);

if (signalLock) {
for (i=74;i<90;i++){tft.drawFastHLine(50,i,220,ILI9340_BLACK);}
int z = int(Latitude);
int x = int(z/100);
int y = z - int((x*100));
z = int(((Latitude - float(z)))*60);
tft.print("LAT: "); tft.print(x); tft.print("deg "); tft.print(y); tft.print("min "); tft.print(z); tft.print("sec
"); tft.println(LatEW);

WriteDataRecord = "LOC = ";

itoa(x, Alpha, 10);


WriteDataRecord = WriteDataRecord + Alpha + "Deg ";
itoa(y, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + "min " ;
itoa(z, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + "sec " + LatEW +" ";

for (i=90;i<106;i++){tft.drawFastHLine(50,i,220,ILI9340_BLACK);}
z = int(Longitude);
x = int(z/100);
y = z - int((x*100));
z = int(((Longitude - float(z)))*60);
tft.print("LON: "); tft.print(x); tft.print("deg "); tft.print(y); tft.print("min "); tft.print(z);
tft.print("sec "); tft.println(LonNS);

itoa(x, Alpha, 10);


WriteDataRecord = WriteDataRecord + Alpha + "Deg ";
itoa(y, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + "min " ;
itoa(z, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + "sec " + LonNS +" ";

for (i=106;i<122;i++){tft.drawFastHLine(75,i,60,ILI9340_BLACK);}
tft.print("Speed: "); tft.println(Speed);
for (i=122;i<138;i++){tft.drawFastHLine(75,i,60,ILI9340_BLACK);}
tft.print("Angle: "); tft.println(Angle);
for (i=138;i<154;i++){tft.drawFastHLine(110,i,80,ILI9340_BLACK);}
tft.print("Altitude: "); tft.println(Altitude);
for (i=154;i<170;i++){tft.drawFastHLine(135,i,50,ILI9340_BLACK);}
tft.print("Satellites: "); tft.println(Satellites);
}
Serial.print("\nTime: ");
Serial.print(hours, DEC); Serial.print(':');
Serial.print(minutes, DEC); Serial.print(':');
Serial.print(seconds, DEC); Serial.print('.');
Serial.println(ms);
Serial.print("Date: ");
Serial.print(months, DEC); Serial.print('/');
Serial.print(days, DEC); Serial.print("/20");
Serial.println(years, DEC);
Serial.print("Fix: "); Serial.print(signalLock);
Serial.print(" quality: "); Serial.println(signalQual);
if (signalLock) {
Serial.print("Location: ");
Serial.print(Latitude, 4); Serial.print(LatEW);
Serial.print(", ");
Serial.print(Longitude, 4); Serial.println(LonNS);
Serial.print("Speed (knots): "); Serial.println(Speed);
Serial.print("Angle: "); Serial.println(Angle);
Serial.print("Altitude: "); Serial.println(Altitude);
Serial.print("Satellites: "); Serial.println(Satellites);
}
// Giger Counter Code
noInterrupts();
if (pointer < 60) { pointer = pointer + 1; }
else { pointer = 0; }
counts[pointer] = 0;
interrupts();
sum = 0;
for (i=0; i <= 60; i++){ if(i != pointer){ sum = sum + counts[i]; }}
Serial.print("Count = ");
Serial.print(sum);
sieverts = sum * 0.0057; // Convert Counts to Micro Sieverts per Hour
Serial.print(" Dose = ");
Serial.print(sieverts);
Serial.println(" uSieverts/Hr");
tft.println("");
for (i=186;i<202;i++){tft.drawFastHLine(190,i,60,ILI9340_BLACK);}
tft.print("Counts/Minute = "); tft.println(sum);
for (i=202;i<218;i++){tft.drawFastHLine(75,i,60,ILI9340_BLACK);}
tft.print("Dose = "); tft.print(sieverts); tft.println(" uSieverts/Hr");

WriteDataRecord = WriteDataRecord + "Counts ";


itoa(sum, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + " Dosage = ";
x = int(sieverts*1000);
ltoa(x, Alpha, 10);
WriteDataRecord = WriteDataRecord + Alpha + "*10E-9 Sieverts EOR";
Serial.println(WriteDataRecord);

int S = SSDWriteFunction();
if(S > 0) Serial.println("Write Error");
S = SSDReadFunction();
if(S > 0) Serial.println("Read Error");
Serial.println(ReadDataRecord);

records += 1;
Serial.print("Record Number Written to Disk = "); Serial.println(records);

time=micros()-time;
Serial.print("Process Time = "); Serial.print(time); Serial.print("us");

displayDataflag = 0;
}
}
// Interrupt Routine for the Giger Counter
void tick() { counts[pointer] = counts[pointer] + 1; }

// Check to see if the SD card is there and Initialize the File System
int SSDDiscoverFunction() {
int R = 1;
Serial.print("\nSD card - ");
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("Initialization failed");
Serial.println(" Things to check:");
Serial.println(" Is a card inserted?");
Serial.println(" Is your wiring correct?");
Serial.println(" Does the chipSelect PIN match your Shield?");
return R; }
Serial.println("Recognized");
// print the type of card
Serial.print(" Card type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");}
// Open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println(" Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return R;}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print(" Volume type is FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print(" Volume size (bytes): ");
Serial.println(volumesize);
Serial.print(" Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print(" Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
R = 0;
return R; }

// Start up the SD card file system


int SSDStartSSDFunction() {
int R = 1;
Serial.println();
Serial.print(" SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return R; }
Serial.println("initialization done.");
R = 0;
return R; }

// List the SC card directory


int SSDListDirFunction() {
int R = 0;
Serial.println("Files found on the card (name, date and size in bytes): ");
root.openRoot(volume);
//list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println("");
return R; }

//Write to the SD card File System


int SSDWriteFunction() {
int R = 1;
if (!SD.exists(WriteFileName)) {
//if file does not exist create it
Serial.println("\nData File Created - " + WriteFileName);
} else {
Serial.println("\nData Append to - " + WriteFileName);
}
myFile = SD.open(WriteFileName, FILE_WRITE);
if (!SD.exists(WriteFileName)) {
Serial.println("Not able to Create File");
return R; }
// if the file is available, write to it:
if (myFile.available()) {
myFile.println(WriteDataRecord);
// Close the file after writing
myFile.close(); }
Serial.print("Data Written to File - ");
Serial.println(WriteDataRecord);
R = 0;
return R; }

// Read from the SD card


int SSDReadFunction() {
int R = 1;
ReadDataRecord = "";
if (!SD.exists(ReadFileName)) {
Serial.println("File Not Available");
return R; }
myFile = SD.open(ReadFileName);
Serial.print("Data Read from File - ");
Serial.print(ReadFileName);
while (myFile.available()) {
ReadDataRecord += char(myFile.read()); }
Serial.print(ReadDataRecord);
myFile.close();
R = 0;
return R; }

//Remove a File from the SD card File System


int SSDRemoveFunction(){
int R = 1;
if (!SD.exists(RemoveFileName)) {
Serial.println("No File to Remove");
return R; }
SD.remove(RemoveFileName);
Serial.println("Removed - " + RemoveFileName);
R = 0;
return R; }

You might also like