ArduinoGeigerCounter Code Help for Class

Hello,

My group and I need help figuring out code for our Geiger Counter RadiationD-v1.1 which we are using in our physics project. Whenever we attempt to collect data it only gives us zeros as the radiation value. The only time we’ll get a different value is if we have the material we’re measuring touch the tube but even then the data isn’t valid as the Geiger Counter will measure my finger and a piece of uranium at the same radiation value. We’ve been trying to work the code out but haven’t had any breakthroughs. We would really appreciate any help.

Here’s the code ( the code isn’t ours but rather an imperfect code we found online and have been trying to fix)

// 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;
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(500);

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

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

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

 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 = 1;

 }

}

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);
   }
 }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Seriously imperfect code: it contains italic text!

(now please go and read the sticky so you know how to properly code. And what else we need for you to have any chance of help).

By the way, this belongs in the project guidance section.

You have a couple of serious errors in the code, the major one being the interrupt service routine. There is no need to detach and reattach the ISR inside the ISR, interrupts are already disabled inside the ISR. Additionally, you want the ISR to take as little time as possible to execute, waiting for an input to change state inside an ISR is going to cause major problems.

Here is a link to a good explanation of interrupts:

https://gammon.com.au/interrupts

The other obvious problem is that you have defined an array for the output pins that connect to the LEDs, but in the code you are not properly referencing the individual elements of the array.

Another suggestion, instead of attachInterrupt (0, use attachInterrupt(digitalPinToInterrupt(2), depending on which arduino board you are using, interrupt 0 may not be digital pin 2.

long count = 0;

Variables shared with interrupt routines MUST be declared volatile.

The only statement you should have in your interrupt routine is this one:

 count++;