SOLVED: Gm-counter, start after press button, measure for "time"

Hi Everyone,

I’m new at this forum. Have some experience with c++ and turbo pascal (yes long time ago). Before this project i programmed an Arduino for controlling the light (KiKU). But with this program for some reason i get figure it out.

Problem: I bought the Geiger counter from cooking hacks. And im now trying to implement it. What i want is not that difficult. I want the counter to start when button is pressed and then measure for some time (f.e 10 sec). During that time i want the counts to be visible in the screen and after that time i want to be able to start the counter again by pressing start.

I tried to things a this moment 1) just add more time when pressing “start” 2) running the program again when pressing start, but for some reason it doenst work. I think i overlook something.

One thing that goes wrong is that the time add mutiple times when holding hte button to long. I understand why but dont know a way around.

// 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);
//pins
const int start=14;//start op 14

// Variables
int ledArray [] = {10,11,12,13,9};
int geiger_input = 2;
long count = 0;
long countPerMinute = 0;
long timeEnd = 0;
long timePreviousMeassure = 0;
long measurementtime = 11000; //meetijd tijd in ms +1000 ms
long timePreviouscor = 0;
float radiationValue = 0.0;
long N=0;

boolean buttonState = false;
boolean lastButtonState = LOW;
boolean setState=LOW;

boolean state1 = true;
boolean state2 = false;

void setup(){
pinMode(start,INPUT);
pinMode(geiger_input, INPUT);
digitalWrite(geiger_input,HIGH);
digitalWrite(start,LOW);
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(100);

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

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

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

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

attachInterrupt(0,countPulse,FALLING);

}

void loop()
{
if( digitalRead(start)== HIGH)
{
count=0;
// timePreviousMeassure=0;
measurementtime=millis()+measurementtime;
timePreviouscor=timeEnd;
delay(1000);
}
                         if (millis() <= measurementtime)                      
                          {
                          lcd.clear();    
                          lcd.setCursor(0, 0);
                          lcd.print("Cnt=");
                          lcd.setCursor(4,0);
                          lcd.print(count);
                          lcd.setCursor(8,2);

                          lcd.setCursor(8,0);
                          lcd.print(measurementtime);
                          lcd.setCursor(0,15);
                          lcd.print(N);
                          
                          lcd.print("tijd:");
                          lcd.setCursor(13,2);
                          lcd.print(timePreviousMeassure/1000);
                          lcd.setCursor(15,2);
                          lcd.print("s");
                          delay(100);
                          timePreviousMeassure = millis()-timePreviouscor;  
                          /*lcd.clear();    
                          lcd.setCursor(0, 0);
                          lcd.print(timestart/1000);
                          lcd.setCursor(4,0);
                          lcd.print(measurementtime/1000);
                          delay(1000);*/     
                          timeEnd=millis();
                          buttonState=true;
                          N=N+1;
                          }
             
           

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

the time add mutiple times when holding hte button to long. I understand why but dont know a way around.

You need to detect when the button becomes pressed rather than when it is pressed.
Look at the StateChangeDetection example in the IDE.

Incidentally, please use code tags when posting code to make it easy to select and copy and to prevent any of it being interpreted as HTML tags.

Thanks, that helped, only now does the counter stay zero :frowning: (it doenst count anymore)

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

// this constant won't change:
const int  startPin = 14;    // the pin that the pushbutton is attached to
int geiger_input = 2;  //pin gmcounter
int ledArray [] = {10,11,12,13,9};

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
long count = 0;
long measurementtime = 10000; //meetijdtijd
long N=0;
long timePreviousMeassure = 0;
long starttime = 0;




void setup() {
  // initialize the button pin as a input:
  pinMode(startPin, INPUT); 
  // initialize serial communication:
  Serial.begin(9600);
  
}


void loop() 
{
  // read the pushbutton input pin:
  buttonState = digitalRead(startPin); //read pin state

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
        lcd.begin(16, 2);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("ON");
        lcd.setCursor(0,5);
        lcd.print("Pushes: ");  
        lcd.setCursor(10,5);
        lcd.print(buttonPushCounter);  
        starttime=millis();
                        //while (millis() <= measurementtime+(buttonPushCounter*measurementtime))                      
                          while (millis() <= measurementtime+starttime)
                             {
                             lcd.clear();    
                             lcd.setCursor(0, 0);
                             lcd.print("Cnt=");
                             lcd.setCursor(4,0);
                             lcd.print(count); //display nr of counts from gm tube
                             lcd.setCursor(8,2);                          
                             lcd.print("tijd:");
                             lcd.setCursor(13,2);
                             lcd.print(timePreviousMeassure/1000);
                             timePreviousMeassure = millis()-starttime;  
                             lcd.setCursor(15,2);
                             lcd.print("s");
                             delay(100);
                                                          }
       
    } else 
      {
        // if the current state is LOW then the button went from on to off:
         lcd.clear();
         lcd.setCursor(0, 0);
         lcd.print("Druk op start");
         lcd.setCursor(0, 3);
         lcd.print(count);
         
      }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

 //hier worden de pulsen getelt
void countPulse(){
  detachInterrupt(0);
  count++;
  while(digitalRead(2)==0){
  }
  attachInterrupt(0,countPulse,FALLING);
}

problem solved, forgot to attach interrupt

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

// this constant won't change:
const int  startPin = 14;    // the pin that the pushbutton is attached to
int geiger_input = 2;  //pin gmcounter
int ledArray [] = {10,11,12,13,9};

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
long count = 0;
long measurementtime = 10000; //meetijdtijd
long N=0;
long timePreviousMeassure = 0;
long starttime = 0;




void setup() {
  // initialize the button pin as a input:
  pinMode(startPin, INPUT); 
   // initialize serial communication:
   pinMode(geiger_input, INPUT);
   digitalWrite(geiger_input,HIGH);
     attachInterrupt(0,countPulse,FALLING);
  Serial.begin(19200);
  
}


void loop() 
{
  // read the pushbutton input pin:
  buttonState = digitalRead(startPin); //read pin state

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      count=0;
        lcd.begin(16, 2);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("ON");
        lcd.setCursor(0,5);
        lcd.print("Pushes: ");  
        lcd.setCursor(10,5);
        lcd.print(buttonPushCounter);  
        starttime=millis();
                          while (millis() <= measurementtime+starttime)
                             {
                             lcd.clear();    
                             lcd.setCursor(0, 0);
                             lcd.print("Cnt=");
                             lcd.setCursor(7,0);
                             //count=analogRead(geiger_input);
                             lcd.print(count); //read nr of counts from gm tube
                             lcd.setCursor(8,2);                          
                             lcd.print("tijd:");
                             lcd.setCursor(13,2);
                             lcd.print(timePreviousMeassure/1000);
                             timePreviousMeassure = millis()-starttime;  
                             lcd.setCursor(15,2);
                             lcd.print("s");
                             delay(100);
                             
                                                          }
       
    } else 
      {
        // if the current state is LOW then the button went from on to off:
         lcd.clear();
         lcd.setCursor(0, 0);
         lcd.print("Druk op start");
         lcd.setCursor(0, 3);
         lcd.print(count);
         
      }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

 //hier worden de pulsen getelt
void countPulse(){
  detachInterrupt(0);
  count++;
  while(digitalRead(2)==0){
  }
  attachInterrupt(0,countPulse,FALLING);
}