Pages: [1]   Go Down
Author Topic: How to write geiger counter code?  (Read 332 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im a beginner .   smiley-sad
I dont know how to write program wich ll count High states  on uC input pin , and later convert it to Counts Per Minute and uSv/h.
I use Atmega 8 with bootloader, 8x2 lcd and STS-5 Geiger tube with High voltage converter.
Tube specs: ( 27 counts per second = 10 uSv/h)
Please Help me with this part of program.

Thats what i have done: (probably all is wrong , but it diplays on lcd  number of counts)
Code:
#include <LiquidCrystal.h>
int led = 1;
int inputGM =7;
int inputGMstate = 0;
int counts=0;
unsigned time;
int result;
LiquidCrystal lcd(6, 8, 5, 4, 3, 2);

void setup() {  
  
  pinMode(led, OUTPUT);  
  pinMode(inputGM,INPUT);  
    lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("COUNTS:");
}

void loop() {
  time = millis()/1000;
  inputGMstate = digitalRead(inputGM);
  if(inputGMstate == HIGH){
    delay(50);
  counts = counts + 1;
  delay(50);
}
 result = counts / time * 60;
  lcd.setCursor(0, 1);
  lcd.print(counts);
//led displays one count  
  if (counts >= 10){
  digitalWrite(led,HIGH);
}
else
{ digitalWrite(led,LOW);
}
}
https://sites.google.com/site/diygeigercounter/gm-tubes-supported
« Last Edit: February 07, 2014, 12:12:56 pm by Tiger11 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Given that you are attaching the LCD to pin 2, why are you writing it LOW at the end of loop()?

Use Tools + Auto Format before posting code again! That mess is too difficult to read.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34127
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Remove all the delays, you don't need them and they will give an inaccurate count.

Do not use " time = millis( ) /1000 " it will add nothing and is wrong.
Time the interval between two pulses and then work out how many you would get in a minute and display that.
Logged

Oregon, USA
Offline Offline
Edison Member
*
Karma: 67
Posts: 2230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google "arduino geiger counter" for several examples. Some have code, like this one: http://www.cooking-hacks.com/documentation/tutorials/geiger-counter-arduino-radiation-sensor-board
Logged

"It seems to run on some form of electricity"

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cut from this code this what i need , i don't understand how it works......

Code:
/*
 *  ------Geiger Tube board (Arduino Code) Example--------
 *
 *  Explanation: This example shows how to get the signal from the Geiger Tube
 *  in Arduino, we use one of the Arduino interrupt pins (PIN2).
 *  We count the time (ms) between two pulses of the Geiger tube.
 *
 *  Copyright (C) 2011 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version: 0.3
 *  Design: Marcos Yarza, David Gascon
 *  Implementation: Marcos Yarza
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(3,4,5,6,7,8);


// Threshold values for the led bar
#define TH1 45
#define TH2 95
#define TH3 200
#define TH4 400
#define TH5 600

// Conversion factor - CPM to uSV/h
#define CONV_FACTOR 0.00812

// Variables
int ledArray [] = {10,11,12,13,9};
int geiger_input = 2;
long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;

void setup(){
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);
  for (int i=0;i<5;i++){
    pinMode(ledArray[i],OUTPUT);
  }

  Serial.begin(19200);

  //set up the LCD\'s number of columns and rows:
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiation Sensor");
  lcd.setCursor(0,1);
  lcd.print("Board - Arduino"); 
  delay(1000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Cooking Hacks");
  delay(1000);

  lcd.clear();
  lcd.setCursor(0,1); 
  lcd.print("www.cooking-hacks.com");
  delay(500);
  for (int i=0;i<5;i++){
    delay(200); 
    lcd.scrollDisplayLeft();
  }
  delay(500);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  - Libelium -");
  lcd.setCursor(0,1);
  lcd.print("www.libelium.com");   
  delay(1000);

  lcd.clear(); 
  lcd.setCursor(0, 0);
  lcd.print("CPM=");
  lcd.setCursor(4,0);
  lcd.print(6*count);
  lcd.setCursor(0,1);
  lcd.print(radiationValue);

  attachInterrupt(0,countPulse,FALLING);

}

void loop(){
  if (millis()-timePreviousMeassure > 10000){
    countPerMinute = 6*count;
    radiationValue = countPerMinute * CONV_FACTOR;
    timePreviousMeassure = millis();
    Serial.print("cpm = ");
    Serial.print(countPerMinute,DEC);
    Serial.print(" - ");
    Serial.print("uSv/h = ");
    Serial.println(radiationValue,4);     
    lcd.clear();   
    lcd.setCursor(0, 0);
    lcd.print("CPM=");
    lcd.setCursor(4,0);
    lcd.print(countPerMinute);
    lcd.setCursor(0,1);
    lcd.print(radiationValue,4);
    lcd.setCursor(6,1);
    lcd.print(" uSv/h");

    //led var setting 
    if(countPerMinute <= TH1) ledVar(0);
    if((countPerMinute <= TH2)&&(countPerMinute>TH1)) ledVar(1);
    if((countPerMinute <= TH3)&&(countPerMinute>TH2)) ledVar(2);
    if((countPerMinute <= TH4)&&(countPerMinute>TH3)) ledVar(3);
    if((countPerMinute <= TH5)&&(countPerMinute>TH4)) ledVar(4);
    if(countPerMinute>TH5) ledVar(5);

    count = 0;

  }

}

void countPulse(){
  detachInterrupt(0);
  count++;
  while(digitalRead(2)==0){
  }
  attachInterrupt(0,countPulse,FALLING);
}

void ledVar(int value){
  if (value > 0){
    for(int i=0;i<=value;i++){
      digitalWrite(ledArray[i],HIGH);
    }
    for(int i=5;i>value;i--){
      digitalWrite(ledArray[i],LOW);
    }
  }
  else {
    for(int i=5;i>=0;i--){
      digitalWrite(ledArray[i],LOW);
    }
  }
}
Logged

Pages: [1]   Go Up
Jump to: