Tuesday, May 16, 2023

Geiger counter back up in Regenstein Library

My long suffering Geiger Counter is back up and running. I used ChatGPT to create a sample Arduino sketch to interact with the pulses coming from the counter and send a per-minute count to the serial port. Then I have a Powershell script write the date-time and the number of counts to a CSV file on a web server. Then I have dygraph.js make a picture of the results. One flaw that is evident is if the Geiger counter connection gets loose, and it does, once it reconnects it looks like there's a sudden surge of rate, which is fake. Also, this whole thing is reliant on powershell running on a Windows box that gets patched frequently. Oh and ChatGPT will make very big mistakes that you absolutely have to catch.
Enjoy this subpar code!
const int geigerPin = 2; // the pin number connected to the Geiger counter output
volatile int count = 0; // variable to count the number of pulses


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

  pinMode(geigerPin, INPUT); // set Geiger counter pin as input
  Serial.begin(9600); // initialize serial communication
  attachInterrupt(digitalPinToInterrupt(geigerPin), pulseCount, FALLING); // interrupt on falling edge of Geiger counter pulse
  pinMode(13, OUTPUT); 
  pinMode(5, OUTPUT);


}

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

  delay(60000); // wait for 1 minute
  detachInterrupt(digitalPinToInterrupt(geigerPin)); // disable interrupt
  float cpm = count; // * 60.0 / 60000.0; // calculate counts per minute -- this seems wrong
  // Serial.print(cpm); // print counts per minute //
  Serial.println(cpm); // print counts per minute
  count = 0; // reset count
  attachInterrupt(digitalPinToInterrupt(geigerPin), pulseCount, FALLING); // re-enable interrupt
}

void pulseCount() {
  count++; // increment pulse count
    digitalWrite(13, HIGH);
    tone(5, 3800, 5);
    delay(50);
    digitalWrite(13, LOW);
  }
  

Powershell:
# Configure serial port settings
$port = new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one


# Loop indefinitely
while ($true) {
# Read integer from serial port
$port = new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.Open()
$number = $port.ReadLine()

# Write number to file
$path = "\\samba\dean\web\counts.csv"
$timestamp=get-date -format s
# // $NewLine = "{0},{1}" -f $date,$number
# Format the data as a CSV string
    $data = "{0},{1}" -f $timestamp , $number.Trim()
#// $NewLine | add-content -path $file
# $string=$date,$number
# //Add-Content $path $string
# Append the data to the file
    Add-Content $path $data

 echo $data
 $port.Close()
  }