Thursday, 26 April 2012

Geiger Counter

Updated Geiger Counter Code. This program uses a loop to determine the length of the pulse. Then it will , add one to the value of a counter if the pulse is of a certain length, then waits for another pulse. This program will then write the value to the corresponding place in the memory of the EEPROM chip. The program is designed to change the slots in memory that are being used if a significant change in pressure is detected. In event of pressure change, it will shift the store bits so that the former data remains and new data can be taken at a different altitude. Finally, in the 0th slot of eeprom memory the number of pressure bins is stored, to ensure that the correct amount of data is printed in the print data routine. This design is  to be used to measure the background radiation vs altitude.


/******************************************
Geiger counter Program
Waits for a switch d8 or d9
d8 it is high, then goes into write mode,
counting pulses of specific length
d9 is high, goes into read mode, reading off of EEPROM on atmega
*******************************************/
#include  "Wire.h"    // for External EEPROM CHIP
#define chip 0x50

int a0=0; int b0=0; int c0=0; int d0=0; int e0=0;
int a1=0; int b1=0; int c1=0; int d1=0; int e1=0;
int a2=0; int b2=0; int c2=0; int d2=0; int e2=0;
int a3=0;

int x=0;
void writeEEPROM(int input,int x){
         byte first = lowByte(input);
         byte last = highByte(input);
         writeData(31+x, first);
         writeData(32+x, last);

}
void initializeSlots(int x){
  writeData(32+x,0); writeData(1+x,0); writeData(2+x,0); writeData(3+x,0); writeData(4+x,0); writeData(5+x,0);
  writeData(6+x,0); writeData(7+x,0); writeData(8+x,0); writeData(9+x,0); writeData(10+x,0); writeData(11+x,0);
  writeData(12+x,0); writeData(13+x,0); writeData(14+x,0); writeData(15+x,0); writeData(16+x,0); writeData(17+x,0);
  writeData(18+x,0); writeData(19+x,0); writeData(20+x,0); writeData(21+x,0); writeData(22+x,0); writeData(23+x,0);
  writeData(24+x,0); writeData(25+x,0); writeData(26+x,0); writeData(27+x,0); writeData(28+x,0); writeData(29+x,0);
  writeData(30+x,0); writeData(31+x,0);}
void initializeCounts(){
//reinitializes the counters
    a0=0; b0=0; c0=0; d0=0; e0=0;
    a1=0; b1=0; c1=0; d1=0; e1=0;
    a2=0; b2=0; c2=0; d2=0; e2=0;
    a3=0;
}

void setup(){
  Serial.begin(9600); // for screen output
  Wire.begin();   // wake up, EEPROM
  pinMode(8, INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
  pinMode(13, OUTPUT);
  pinMode(11,INPUT);
}
void writeData(unsigned int add, byte data)
// writes a byte of data 'data' to the chip at I2C address 'device', in memory location 'add'
{
Wire.beginTransmission(chip);
Wire.write((int)(add >> 8));   // left-part of pointer address
Wire.write((int)(add & 0xFF)); // and the right
Wire.write(data);
Wire.endTransmission();
//Serial.println(data, DEC);
delay(10);
}
byte readData(unsigned int add) // reads a byte of data from memory location 'add' in chip at I2C address 'device'
{
byte result;  // returned value
Wire.beginTransmission(chip); //  these three lines set the pointer position in the EEPROM
Wire.write((int)(add >> 8));   // left-part of pointer address
Wire.write((int)(add & 0xFF)); // and the right
Wire.endTransmission();
Wire.requestFrom(chip,1); // now get the byte of data...
result = Wire.read();  return result; // and return it as a result of the function readData
}
void loop(){
  Serial.println("Waiting For Switch");
  while(digitalRead(8)==LOW && digitalRead(9)==LOW){
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
  }
 
  if(digitalRead(8)==HIGH){
    Serial.println("Initializing Counts");
    initializeCounts();  
    initializeSlots(0);
   
   
   
   
    Serial.println("Counting Background Radiation");
    int formerMax=900;
    int loopStopper = 1;
    while(digitalRead(8)==HIGH){
    int height = analogRead(0);
    //Serial.println(formerMax);
    //Serial.println(height);
   
    if(height<1000&&height>=900&&formerMax!=1000){
    formerMax=1000;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<900&&height>=800&&formerMax!=900){
    formerMax=900;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<800&&height>=700&&formerMax!=800){
    formerMax=800;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<700&&height>=600&&formerMax!=700){
    formerMax=700;
    loopStopper++;
    x=x+32;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<600&&height>=500&&formerMax!=600){
    formerMax=600;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<500&&height>=400&&formerMax!=500){
    formerMax=500;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<400&&height>=300&&formerMax!=400){
    formerMax=400;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<300&&height>=200&&formerMax!=300){
    formerMax=300;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<200&&height>=100&&formerMax!=200){
    formerMax=200;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    else if(height<100&&height>=0&&formerMax!=100){
    formerMax=100;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    writeEEPROM(formerMax,x);
    }
    /***
    else if(height<450&&height>=400&&formerMax!=450){
    formerMax=450;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<400&&height>=350&&formerMax!=400){
    formerMax=400;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<350&&height>=300&&formerMax!=350){
    formerMax=350;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<300&&height>=250&&formerMax!=300){
    formerMax=300;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<250&&height>=200&&formerMax!=250){
    formerMax=300;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<200&&height>=150&&formerMax!=200){
    formerMax=200;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<150&&height>=100&&formerMax!=150){
    formerMax=150;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<100&&height>=50&&formerMax!=100){
    formerMax=100;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    else if(height<50&&height>=0&&formerMax!=50){
    formerMax=50;
    x=x+32;
    loopStopper++;
    initializeSlots(x);
    initializeCounts();
    }
    ***/
   
     
      while(digitalRead(10)==LOW && digitalRead(8)==HIGH);    
      int counter=0;
     
      while(digitalRead(10)==HIGH){
      counter++;
      delayMicroseconds(192);
     
      }
     if(counter==1){//0ms<t<.2ms
       a0++;
       byte first = lowByte(a0);
       byte last = highByte(a0);
       writeData(1+x, first);
       writeData(2+x, last);
     }
     else if(counter==2){//.2ms<t<.4ms
       b0++;
       byte first = lowByte(b0);
       byte last = highByte(b0);
       writeData(3+x, first);
       writeData(4+x, last);
     }
     else if(counter==3){//.4ms<t<.6ms
       c0++;
       byte first = lowByte(c0);
       byte last = highByte(c0);
       writeData(5+x, first);
       writeData(6+x, last);
     }
     else if(counter==4){//.6ms<t<.8ms
       d0++;
       byte first = lowByte(d0);
       byte last = highByte(d0);
       writeData(7+x, first);
       writeData(8+x, last);
     }
     else if(counter==5){//.8ms<t<1.0ms
       e0++;
       byte first = lowByte(e0);
       byte last = highByte(e0);
       writeData(9+x, first);
       writeData(10+x, last);
     }
     else if(counter==6){//1.0ms<t<1.2ms
       a1++;
       byte first = lowByte(a1);
       byte last = highByte(a1);
       writeData(11+x, first);
       writeData(12+x, last);
     }
     else if(counter==7){//1.2ms<t<1.4ms
       b1++;
       byte first = lowByte(b1);
       byte last = highByte(b1);
       writeData(13+x, first);
       writeData(14+x, last);
     }
     else if(counter==8){//1.4ms<t<1.6ms
       c1++;
       byte first = lowByte(c1);
       byte last = highByte(c1);
       writeData(15+x, first);
       writeData(16+x, last);
     }
     else if(counter==9){//1.6ms<t<1.8ms
       d1++;
       byte first = lowByte(d1);
       byte last = highByte(d1);
       writeData(17+x, first);
       writeData(18+x, last);
     }
     else if(counter==10){//1.8ms<t<2.0ms
       e1++;
       byte first = lowByte(e1);
       byte last = highByte(e1);
       writeData(19+x, first);
       writeData(20+x, last);
     }
     else if(counter==11){//2.0ms<t<2.2ms
       a2++;
       byte first = lowByte(a2);
       byte last = highByte(a2);
       writeData(21+x, first);
       writeData(22+x, last);
     }
     else if(counter==12){//2.2ms<t<2.4ms
       b2++;
       byte first = lowByte(b2);
       byte last = highByte(b2);
       writeData(23+x, first);
       writeData(24+x, last);
     }
     else if(counter==13){//2.4ms<t<2.6ms
       c2++;
       byte first = lowByte(c2);
       byte last = highByte(c2);
       writeData(25+x, first);
       writeData(26+x, last);
     }
     else if(counter==14){//2.6ms<t<2.8ms
       d2++;
       byte first = lowByte(d2);
       byte last = highByte(d2);
       writeData(27+x, first);
       writeData(28+x, last);
     }
     else if(counter==15){//2.8ms<t<3.0ms
       e2++;
       byte first = lowByte(e2);
       byte last = highByte(e2);
       writeData(29+x, first);
       writeData(30+x, last);
     }
     /**else if(counter>=16){//t>3.0ms
       a3++;
       byte first = lowByte(a3);
       byte last = highByte(a3);
       writeData(31+x, first);
       writeData(32+x, last);
     }**/
   
    }
    writeData(0,loopStopper);
  }
  if (digitalRead(9)==HIGH&&digitalRead(8)==LOW){  
    Serial.println("Printing Data");
    int z=0;
    int lS = readData(0);
    int zStop = (lS-1)*32;
   
    while(z<=zStop){
    Serial.print("0.0ms<t<0.2ms     ");
    byte temp1=readData(2+z)<<8;
    Serial.println(readData(1+z)+temp1);
 
    Serial.print("0.2ms<t<0.4ms     ");
    byte temp3=readData(4+z)<<8;
    Serial.println(readData(3+z)+temp3);
 
    Serial.print("0.4ms<t<0.6ms     ");
    byte temp5=readData(6+z)<<8;
    Serial.println(readData(5+z)+temp5);
 
    Serial.print("0.6ms<t<0.8ms     ");
    byte temp7=readData(8+z)<<8;
    Serial.println(readData(7+z)+temp7);
   
    Serial.print("0.8ms<t<1.0ms     ");
    byte temp9=readData(10+z)<<8;
    Serial.println(readData(9+z)+temp9);

    Serial.print("1.0ms<t<1.2ms     ");
    byte temp11=readData(12+z)<<8;
    Serial.println(readData(11+z)+temp11);

    Serial.print("1.2ms<t<1.4ms     ");
    byte temp13=readData(14+z)<<8;
    Serial.println(readData(13+z)+temp13);
   
    Serial.print("1.4ms<t<1.6ms     ");
    byte temp15=readData(16+z)<<8;
    Serial.println(readData(15+z)+temp15);
   
    Serial.print("1.6ms<t<1.8ms     ");
    byte temp17=readData(18+z)<<8;
    Serial.println(readData(17+z)+temp17);
   
    Serial.print("1.8ms<t<2.0ms     ");
    byte temp19=readData(20+z)<<8;
    Serial.println(readData(19+z)+temp17);
   
    Serial.print("2.0ms<t<2.2ms     ");
    byte temp21=readData(22+z)<<8;
    Serial.println(readData(21+z)+temp21);
   
    Serial.print("2.2ms<t<2.4ms     ");
    byte temp23=readData(24+z)<<8;
    Serial.println(readData(23+z)+temp23);
   
    Serial.print("2.4ms<t<2.6ms     ");
    byte temp25=readData(26+z)<<8;
    Serial.println(readData(25+z)+temp25);
   
    Serial.print("2.6ms<t<2.8ms     ");
    byte temp27=readData(28+z)<<8;
    Serial.println(readData(27+z)+temp27);
   
    Serial.print("2.8ms<t<3.0ms     ");
    byte temp29=readData(30+z)<<8;
    Serial.println(readData(29+z)+temp29);
   
    Serial.print("pressure bin      ");
    byte temp31=readData(32+z)<<8;
    Serial.println(readData(31+z)+temp31);
   
    z=z+32;
    Serial.println();
    }
    Serial.println("\nFinished Printing Data");
    while(digitalRead(9)==HIGH){
        delay(100);
    }
   
 }
 
}

No comments:

Post a Comment