You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

120 lines
3.8 KiB

  1. /*
  2. * Geiger counter Kit could get on: https://www.aliexpress.com search: geiger counter kit
  3. * --------------------------------------------------------------------------------------
  4. * WHAT IS CPM?
  5. * CPM (or counts per minute) is events quantity from Geiger Tube you get during one minute. Usually it used to
  6. * calculate a radiation level. Different GM Tubes has different quantity of CPM for background. Some tubes can produce
  7. * about 10-50 CPM for normal background, other GM Tube models produce 50-100 CPM or 0-5 CPM for same radiation level.
  8. * Please refer your GM Tube datasheet for more information. Just for reference here, J305 and SBM-20 can generate
  9. * about 10-50 CPM for normal background.
  10. * --------------------------------------------------------------------------------------
  11. * HOW TO CONNECT GEIGER KIT?
  12. * The kit 3 wires that should be connected to Arduino UNO board: 5V, GND and INT. PullUp resistor is included on
  13. * kit PCB. Connect INT wire to Digital Pin#2 (INT0), 5V to 5V, GND to GND. Then connect the Arduino with
  14. * USB cable to the computer and upload this sketch.
  15. *
  16. * Author:JiangJie Zhang * If you have any questions, please connect cajoetech@qq.com
  17. * Author:Dejvino
  18. *
  19. * License: MIT License
  20. *
  21. * Please use freely with attribution. Thank you!
  22. */
  23. // include the Servo library
  24. #include <Servo.h>
  25. Servo myServo; // create a servo object
  26. int const PIN_SERVO = 9;
  27. int angle; // variable to hold the angle for the servo motor
  28. int const PIN_TUBE 2;
  29. int const PIN_FRESH = LED_BUILTIN;
  30. int freshState = 0;
  31. #include <SPI.h>
  32. //#define LOG_PERIOD 15000 //Logging period in milliseconds, recommended value 15000-60000.
  33. #define LOG_PERIOD 30000
  34. #define MAX_PERIOD 60000 //Maximum logging period without modifying this sketch
  35. unsigned long counts; //variable for GM Tube events
  36. unsigned long cpm; //variable for CPM
  37. unsigned int multiplier; //variable for calculation CPM in this sketch
  38. unsigned long previousMillis; //variable for time measurement
  39. int const subDiv = 60;
  40. int const subTimespan = LOG_PERIOD / subDiv;
  41. int subCounts = 0;
  42. int subTime = 0;
  43. void tube_impulse(){ //subprocedure for capturing events from Geiger Kit
  44. counts++;
  45. }
  46. void setup(){ //setup subprocedure
  47. myServo.attach(PIN_SERVO); // attaches the servo on pin 9 to the servo object
  48. myServo.write(angle);
  49. counts = 0;
  50. cpm = 0;
  51. multiplier = MAX_PERIOD / LOG_PERIOD; //calculating multiplier, depend on your log period
  52. Serial.begin(9600);
  53. attachInterrupt(0, tube_impulse, FALLING); //define external interrupts
  54. pinMode(PIN_FRESH, OUTPUT);
  55. digitalWrite(PIN_FRESH, LOW);
  56. delay(500);
  57. myServo.write(0);
  58. delay(1000);
  59. myServo.write(200);
  60. delay(1000);
  61. myServo.write(100);
  62. delay(1000);
  63. myServo.write(0);
  64. }
  65. void loop(){ //main cycle
  66. unsigned long currentMillis = millis();
  67. unsigned long timeDiff = currentMillis - previousMillis;
  68. if (freshState == 1 && timeDiff > LOG_PERIOD / 4) {
  69. freshState = 0;
  70. digitalWrite(PIN_FRESH, LOW);
  71. }
  72. if(timeDiff > LOG_PERIOD){
  73. previousMillis = currentMillis;
  74. cpm = counts * multiplier;
  75. angle = cpm;
  76. //myServo.write(angle);
  77. Serial.print("CPM: ");
  78. Serial.println(cpm);
  79. counts = 0;
  80. digitalWrite(PIN_FRESH, HIGH);
  81. freshState = 1;
  82. subCounts = 0;
  83. subTime = currentMillis;
  84. }
  85. int subDiff = currentMillis - subTime;
  86. if (subDiff >= subTimespan) {
  87. subTime = currentMillis;
  88. int subTimeSteps = timeDiff / subTimespan;
  89. subCounts = counts;
  90. int change = (counts * multiplier) - (cpm * subTimeSteps / subDiv);
  91. angle = cpm + change;
  92. if (angle < 0) { angle = 0; }
  93. if (angle > 200) { angle = 200; }
  94. myServo.write(angle);
  95. Serial.print("R: ");
  96. Serial.print(angle);
  97. Serial.println();
  98. }
  99. }