Keep and Share logo     Log In  |  Mobile View  |  Help  
 
Visiting
 
Select a Color
   
 
Sparkfun geiger counter lcd display v2 ino.txt -- 5k
#include <SoftwareSerial.h>
#include <TimerOne.h>

   volatile long count = 0;
   volatile long cpm = 0;
   volatile long alarm_flag = 0;
   float avg_cpm = 0;
   float avg_cpm_act = 0;
   float avg_usvh = 0;
   long count1 = 0;
   long count2 = 0;
   long cpm_act = 0;
   long count_dt = 0;
   long max_cpm = 0;
   float max_cpm_act = 0;
   float max_usvh = 0;
   long min_cpm = 60000000;
   float min_cpm_act = 0;
   float min_usvh = 0;
   long ms_dt = 0;
   long minutes = -1;
 
   // Create a software serial port!
   SoftwareSerial lcd = SoftwareSerial(0,2);
   
   void setup()
   {
   lcd.begin(9600); // set up serial port for 9600 baud.
   // lcd.write(18);
   delay(5000); // wait for display to boot up.
                 
   // Set the size of the display if it isn't 20x4
   // (you only have to do this once).
   // lcd.write(0x7C);
   // lcd.write(3); // 20 character lines.
   // lcd.write(0xFE);
   // lcd.write(5); // 4 lines.
   // delay(100);
   
   // Set the brightness - turn it off.
   // lcd.write(0x7C);
   // lcd.write(128);
   // delay(100);

   // Set up interrupts from the geiger counter
   pinMode(6, OUTPUT); // Radiation alarm output pin.
   pinMode(3, INPUT);
   attachInterrupt(0, geiger_count, RISING);
   Timer1.initialize(1000000); // Set a 10^6 usec countdown timer.
   Timer1.attachInterrupt(timerIsr); // Attach the service routine here.
   }
 
void loop()
 {
 if (minutes == -1)
   {
   minutes = 0;
   count = 0;
   }
 else
   {
   // Clear the display.
   lcd.write(0xFE);
   lcd.write(0x01);

   // Set the cursor to the beginning of the first line.
   lcd.write(0xFE);
   lcd.write(128); // 128 + 0.
   
   count2 = count;
   cpm = count2 - count1;
   if (minutes > 0)
     avg_cpm = (float)count2 / (float)minutes;
   else
     avg_cpm = count2;

   avg_cpm_act = avg_cpm/(1.0 - (avg_cpm*0.0000006666667));

   if (minutes > 0)
     {
     if (cpm <= min_cpm)
       {
       min_cpm = cpm;
       min_cpm_act = min_cpm/(1.0 - (min_cpm*0.0000006666667));
       }

     if (cpm >= max_cpm)
       {
       max_cpm = cpm;
       max_cpm_act = max_cpm/(1.0 - (max_cpm*0.0000006666667));
       }
     }

   min_usvh = 0.002747126 * min_cpm_act;  // 0.002747126 = 9.56/(60*58) (9.56 is the
                                          // best estimate I could find for the soft
                                          // tissue absorption factor.)
   avg_usvh = 0.002747126 * avg_cpm_act;  
   max_usvh = 0.002747126 * max_cpm_act;  
 
   lcd.print("CPM = ");
   lcd.print(cpm);
   lcd.write(0xFE); // Position the cursor.
   lcd.write(139); // 128 + 11.
   lcd.print("Mins:");
   lcd.print(minutes);

   lcd.write(0xFE); // Position the cursor on the second line.
   lcd.write(198); // 128 + 64 + 6.

   if (minutes > 0)
     {
     lcd.print("Min  Avg  Max");
     lcd.write(0xFE); // Position the cursor on the third line.
     lcd.write(148); // 128 + 20.
     lcd.print("  CPM ");
     lcd.print(long(min_cpm_act + 0.5));
     lcd.write(0xFE); // Advance the cursor.
     lcd.write(159); // 128 + 20 + 11.
     lcd.print(long(avg_cpm_act + 0.5));
     lcd.write(0xFE); // Advance the cursor.
     lcd.write(164); // 128 + 20 + 16.
     lcd.print(long(max_cpm_act + 0.5));
     lcd.write(0xFE); // Position the cursor at the fourth line.
     lcd.write(212); // 128 + 84.
     lcd.print("uSv/h ");
     lcd.print(min_usvh, 2);
     lcd.write(0xFE); // Advance the cursor.
     lcd.write(223); // 128 + 84 + 11.
     lcd.print(avg_usvh, 2);
     lcd.write(0xFE); // Advance the cursor.
     lcd.write(228); // 128 + 84 + 16.
     lcd.print(max_usvh, 2);
     }
   
   count1 = count2;
   ++minutes;
   ms_dt = count_dt/40;  // 40 "dead times" per millisecond.
   count_dt = count_dt%40;
   delay (60000 - (ms_dt));
   }
 }

void geiger_count()
 {
 ++count;    // Increment the number of geiger counts.
 ++count_dt; // Increment the "dead time" count.
 delayMicroseconds(25); // 25 microseconds "dead time".
 }

/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
if ((cpm >= 100) || (alarm_flag == 1))
 {
 alarm_flag = 1;
 // Toggle radiation alarm LED.
 digitalWrite(6, digitalRead(6) ^ 1);
 }
}

Creation date: Jul 27, 2021 5:03am     Last modified date: Jun 25, 2023 4:59am   Last visit date: Nov 24, 2024 6:22am
    Report Objectionable Content