Advertisement
CuriousScientist

Geiger-Muller counter code

Jun 16th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //This code belongs to the following tutorial video: https://youtu.be/l9-wzEY1Ehg
  3. /*
  4. Please consider buying the parts using these affiliation links to support me:
  5. Arduino: https://www.banggood.com/custlink/33KKF85c3i
  6. LCD: https://www.banggood.com/custlink/KKmm5oMimv
  7. GM Board: https://www.banggood.com/custlink/33vDVL2V12
  8. Am-241 Source: https://www.banggood.com/custlink/DG3mFO2OIj
  9. */
  10.  
  11.  
  12. /*
  13.  *Pins for the NOKIA LCD:
  14.  *PIN_SCE   7 //Pin 3 on LCD
  15.  *PIN_RESET 6 //Pin 4 on LCD
  16.  *PIN_DC    5 //Pin 5 on LCD
  17.  *PIN_SDIN  4 //Pin 6 on LCD
  18.  *PIN_SCLK  3 //Pin 7 on LCD
  19.  *
  20.  *PIN for the GM board: Pin 0 on Arduino, VIN on the GM board
  21.  *
  22.  * 5V and GND should be obvious :)
  23.  */
  24.  
  25.  
  26. #include <PCD8544.h>
  27. #include <SPI.h>
  28. #define integratingTime 15000  //Logging period in milliseconds
  29. #define oneMinute 60000  //One minute
  30.  
  31. unsigned long counts = 0;     //variable for GM Tube events
  32. unsigned long events = 0;
  33. unsigned long cpm = 0;        //variable for CPM
  34. unsigned int multiplier;  //variable for calculation CPM in this sketch
  35. unsigned long previousMillis;  //variable for time measurement
  36. double avgCounts = 0;
  37. double avgCPM = 0;
  38. double avgUSV = 0;
  39. double sumCPM = 0;
  40. double sumUSV = 0;
  41. double uSv = 0;
  42. double dose = 0;
  43.  
  44. const int ledPin =  9;
  45.  
  46. static PCD8544 lcd;
  47.  
  48.  
  49. void tube_impulse() //subprocedure for capturing events from GM board
  50. {      
  51.   counts++;
  52.   events++;
  53. }
  54.  
  55.  
  56. void setup() {
  57.   // PCD8544-compatible displays may have a different resolution...
  58.   lcd.begin(84, 48);
  59.  
  60.   counts = 0;
  61.   cpm = 0;
  62.   multiplier = oneMinute / integratingTime;      //calculating multiplier, depend on your log period
  63.   Serial.begin(9600);
  64.   attachInterrupt(0, tube_impulse, FALLING); //define external interrupts
  65.   pinMode(ledPin, OUTPUT);
  66.   Serial.println("GM Tube - Send 's' to start...");
  67. }
  68.  
  69.  
  70. void loop()
  71. {
  72.  
  73. withserialPort();
  74.  
  75. }
  76.  
  77.  
  78. void withserialPort()
  79.   {
  80.              
  81.   if (Serial.available() > 0) {
  82.       lcd.setCursor(0, 4); //row 5
  83.       lcd.print("Counts: ");
  84.       lcd.print(events);  
  85.    
  86.     char cin = Serial.read();
  87.     if(cin = 's'){
  88.      while(Serial.read() != 'N') {
  89.  
  90.                  lcd.setCursor(0, 4); //row 5
  91.                  lcd.print("Counts: ");
  92.                  lcd.print(events);  
  93.                
  94.                 unsigned long currentMillis = millis();
  95.                
  96.                 if(currentMillis - previousMillis > integratingTime){
  97.                  
  98.                   avgCounts++;
  99.                   previousMillis = currentMillis;
  100.                   cpm = counts * multiplier; //cpm * 60/15 = cpm * 4
  101.  
  102.                   uSv =( cpm  / 151.0);
  103.                
  104.                
  105.                   sumCPM = (double)cpm + sumCPM;
  106.                   sumUSV = uSv + sumUSV;
  107.              
  108.                   avgCPM = sumCPM / avgCounts;
  109.                   avgUSV = sumUSV / avgCounts;                
  110.                
  111.                
  112.                
  113.  
  114.                 //print cpm on serial
  115.                 Serial.print((currentMillis)/1000);
  116.                 Serial.print("\t");
  117.                 Serial.print(cpm);
  118.                 Serial.print("\t");
  119.                 Serial.print(avgCPM);
  120.                 Serial.print("\n");  
  121.                
  122.              
  123.                
  124.  
  125.  
  126.                  //print on LCD
  127.                  lcd.setCursor(0, 0); //row 1
  128.                  lcd.print("S CPM   uSv/hr"); //first row: information
  129.                  lcd.setCursor(0, 1); //row 2
  130.                  lcd.print("N ");
  131.                  lcd.print(cpm);
  132.                  lcd.print("    ");
  133.                  lcd.print(uSv,4);
  134.                  
  135.                  lcd.setCursor(0, 2); //row 3
  136.                  lcd.print("A ");
  137.                  lcd.print(avgCPM,1);
  138.                  lcd.print("  ");
  139.                  lcd.print(avgUSV,4);
  140.              
  141.                                  
  142.                  counts = 0;
  143.               //-------------------------------------
  144.                
  145.              
  146.                
  147.                 }
  148.             }
  149.             }
  150.         }
  151.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement