GigerCounter Arduino Program
GigerCounter Arduino Program
* 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
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;
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:
// 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);
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);
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);
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);
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");
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; }