radiation detector won't count

Hello,
I built a geiger counter inside of a small plastic project box. I recently added a 3 digit led display on a second board controlled by an arduino microcontroller. I am using just the atmel chip in a socket with a crystal. I have the multiplexed display working perfectly, and I can send the time value to the display as a check by commenting out all the lines in the top part of the code and sending "time" to "displayValue". I commented these lines out in the code but you can read through and see them.

The problem I am having is the display. When I run this code, the display just reads "111". I have played with my code by changing values here and there, and I can't seem to affect those 1's with anything other than the counter routine I outlined above. The data is reaching the arduino properly, and I can see this by throwing a line of code in to send "state" to "displayValue", and the LSB will flicker when I excite the geiger tube with radioactive source. I have the pulses coming out of the geiger counter running through a 555 timer to make them a constant pulsewidth, which is about 100 ns. To be clear, the pulses are randomly timed, and have a constant pulsewidth of 100ns.

Does anyone have any idea why I might be seeing "111" instead of zero's and/or why I can't get the counetr to increment? Any help would be greatly appreciated.

THIS CODE HAS BEEN MODIFIED AND IS IN THE POST BELOW

// Geiger counter seven segment display with analog input
// By Scott Damiani
// Sept 2, 2013
// rev 9/7/2013

const int digitalPin = A0; // set analog input pin as A0 with digitalRead
const int maxvalue = 999;


void setup() // run these once
 
{
  DDRD = B11111111; // set port D pins as digital outputs 7:0
  DDRB = B111111;  // set port B pins as digital outputs 13:8

  // not needed with DDRB  pinMode(8, OUTPUT);
  //                        pinMode(9, OUTPUT);
  //                        pinMode(10, OUTPUT); // set 8, 9, and 10 as outputs

  int sensorReading = 0;
  int num3 = 0;
  int num2 = 0;
  int num1 = 0;
  int digitSelect = 1;
  int cps = 0;
   int displayValue = 0;
   int count = 0;
 
}


void loop () // the main program
{
  int time; int lastTime; int state; int sampleTime = 1; int displayValue; int count; int lastState;
  
  lastState = state;  // if state changes, last state will record the change 
  
  state = digitalRead(digitalPin);  // sets state to 0 or 1 depending on current input status
  
  lastTime = time; // when time changes, lastTime will record the change
  
   time = (millis()/(sampleTime*1000));  // (should be *1000) converts milliseconds to seconds
   
    if (time != lastTime) {displayValue = count; count = 0;} /// when time increments, 
    /// the current value of count will be displayed, and then the count is reset
    
     if (state != lastState) { // if input is HIGH then execute next line
     
      if (state == HIGH) {count++; lastState = HIGH; // increment counter if input is HIGH
       ///this line makes sure to read only the rising edge of the input pulse
      
      } else {lastState = LOW;}
     }
     
   // displayValue = time; //activate this for counter and deactivate all above but "time = (millis.."line
 
  int num3 = (displayValue/100);  // seperates the 100's digit and set it to num3 active on pin 10

    int num2 = ((displayValue/10)-(num3*10));  // seperates the 10's digit and set it to num2 active on pin 9

  int num1 = ((displayValue)-(num2*10)-(num3*100));   // seperates the 1's digit and set it to num1 active on pin 8



  for (int digitSelect = 1; digitSelect<4; digitSelect++) // selects a number to set active on display

  {
    // delay(1);  // this line helps eliminates bleed/blur from the other numbers but makes the MSB go crazy

    int num = 1;  // this won't work above with the other int statements

    if (digitSelect == 1) {
      num = num1; 
      PORTB = B000110;
    } // sends LOW to pin 8 and biases LED#1 power switching transistor

    if (digitSelect == 2) {
      num = num2; 
      PORTB = B000011;
    } // sends LOW to pin 9 and biases LED#2 power switching transistor

    if (digitSelect == 3) {
      num = num3; 
      PORTB = B000101;
    } // same as above    this last one could be an else statement too 

    delay (9);
   // this was here to attempt to remove blur but doesn't completely remove it


    switch (num) { //  MSB is set to 0 for all cases and num is the current displayed digit with active high

   // had to swap pins "b" and "f" due to an assembly error

    case 0:
      PORTD = B11111010; // number zero or = 0x7E   this outputs to segments on pins 1-7 and pin 0 is always LOW
      break;

    case 1:
      PORTD = B10001000; // number one or = 0x30   hex example  
      break;

    case 2:
      PORTD = B10110110; // number two
      break;

    case 3:
      PORTD = B10011110; // number three
      break;

    case 4:
      PORTD = B11001100; // number four
      break;

    case 5:
      PORTD = B01011110; // number five
      break;

    case 6:
      PORTD = B01111110; // number six
      break;

    case 7:
      PORTD = B10001010; // number seven
      break;

    case 8:
      PORTD = B11111110; // number eight
      break;

    case 9:
      PORTD = B11011110; // number nine
      break;

    default:
      PORTD = B00000000; // blank


    } // switch end

  } // for end

} // loop end

How do you remember the state ?
Each time the loop() funtion runs, the variables are created.

You can make a variable global to remember the state.

Why don't you use Arduino functions for the ports ?

for(int i=0; i<=13; i++)
  pinMode(i,OUTPUT);

The function digitalWrite is used to write an output.

The variables that are created in setup() are removed after setup() has finished. They don't do anything at the moment.

  int time; int lastTime; int state; int sampleTime = 1; int displayValue; int count; int lastState;

It's supposed to be one statement per line. I quit reading at this point.

I could try and change the ports to arduino functions, but will that really make a difference since I can make the counter increment by sending "time" to "displayValue"?

PaulS, does it matter if I run all the int statements in one line? I will seperate the lines if it will make the code run diffrently.

Ok, I changed the code with the arduino function for mapping the outputs. (thank you Caltoa)

I still cannot get the counter to increment.

When you say they are created, do you mean created and reset to zero? Is there another way to create the variables?

// Geiger counter seven segment display with analog input
// By Scott Damiani
// Sept 2, 2013
// rev 9/8/2013

const int digitalPin = A0; // set analog input pin as A0 with digitalRead
const int maxvalue = 999;

void setup() // run these once
 {
 for(int i=0; i<=13; i++) // to setup pins with arduino functins 
  pinMode(i,OUTPUT);
 }

void loop () // the main program
{
  int num;
  int time;
  int lastTime;
  int state; 
  int sampleTime = 1;
  int displayValue;
  int count; 
  int lastState;
   
  lastState = state;  // if state changes, last state will record the change 
  
  state = digitalRead(digitalPin);  // sets state to 0 or 1 depending on current input status
  
  lastTime = time; // when time changes, lastTime will record the change
  
   time = (millis()/(sampleTime*100));  // (should be *1000) converts milliseconds to seconds
   
    if (time != lastTime) {displayValue = count; count = 0;} // when time increments, 
    // the current value of count will be displayed, and then the count is reset
    
     if (state != lastState) { // if input is HIGH then execute next line
   
       if (state == HIGH) {count++; lastState = HIGH; // increment counter if input is HIGH
       //this line makes sure to read only the rising edge of the input pulse
      
       } else {lastState = LOW;}
     }
     
     //displayValue = time; //activate this for counter and deactivate all above but "time = (millis.."line


//----------  display output code (everything from here down works)  ---------------
 if (displayValue > 999){displayValue = 999;}
   else if(displayValue < 0){displayValue = 0;}
  int num3 = (displayValue/100);  // seperates the 100's digit and set it to num3 active on pin 10

    int num2 = ((displayValue/10)-(num3*10));  // seperates the 10's digit and set it to num2 active on pin 9

  int num1 = ((displayValue)-(num2*10)-(num3*100));   // seperates the 1's digit and set it to num1 active on pin 8



  for (int digitSelect = 1; digitSelect<4; digitSelect++) // selects a number to set active on display

  {
   
    if (digitSelect == 1) {
      num = num1; 
      PORTB = B000110;
    } // sends LOW to pin 8 and biases LED#1 power switching transistor

    if (digitSelect == 2) {
      num = num2; 
      PORTB = B000011;
    } // sends LOW to pin 9 and biases LED#2 power switching transistor

    if (digitSelect == 3) {
      num = num3; 
      PORTB = B000101;
    } // same as above    this last one could be an else statement too 

    delay (5); // this line removes the blur from the display

    switch (num) { //  MSB is set to 0 for all cases and num is the current displayed digit with active high

   // had to swap pins "b" and "f" due to an assembly error

    case 0:
      PORTD = B11111010; // number zero or = 0x7E   this outputs to segments on pins 1-7 and pin 0 is always LOW
      break;

    case 1:
      PORTD = B10001000; // number one or = 0x30   hex example  
      break;

    case 2:
      PORTD = B10110110; // number two
      break;

    case 3:
      PORTD = B10011110; // number three
      break;

    case 4:
      PORTD = B11001100; // number four
      break;

    case 5:
      PORTD = B01011110; // number five
      break;

    case 6:
      PORTD = B01111110; // number six
      break;

    case 7:
      PORTD = B10001010; // number seven
      break;

    case 8:
      PORTD = B11111110; // number eight
      break;

    case 9:
      PORTD = B11011110; // number nine
      break;

    default:
      PORTD = B00000000; // blank


    } // switch end

  } // for end

} // loop end

When you say they are created, do you mean created and reset to zero? Is there another way to create the variables?

Created on each pass through loop(). If there is an initialization statement, then the variable is initialized to the specified value. If not, it is not initialized, and you get whatever crap was already there. You can use global variables or declare the local variables static.