radsim.ino
/*
radsim.ino
Eric Ayars
Simulate pulses from a half-life experiment using an Arduino.
The way I'm doing this is to have a "decayLevel" that decays according to
the formula for radioactive decay. Then a random number 'event' is chosen,
and if event is less than decayLevel, a decay has occurred.
This SHOULD give me pulses that decrease in frequency in a manner similar
to that of a radiation source. (Yep. It does.)
*/
#include <math.h> // allow exp() function
#define PULSE 13 // Pulse output pin
int decayLevel;
long time = 0;
long tau = 100000; // lifetime
int event; // random number storage
void setup() {
randomSeed(analogRead(0)); // read initial seed from an unconnected
// analog input
pinMode(PULSE, OUTPUT);
}
void loop() {
time = millis(); // how long has it been...
decayLevel = int(1000*exp(-float(time)/float(tau)));
// the above line sets the threshold for decay
event = random(1100); // set this 10% higher than max decaylevel
// to eliminate "saturation" at start
if (event < decayLevel) {
// Decay occurs. Write a pulse.
digitalWrite(PULSE, HIGH);
digitalWrite(PULSE, LOW);
} else {
// no decay occurs. Write a non-pulse to keep timing consistent.
digitalWrite(PULSE, LOW);
digitalWrite(PULSE, LOW);
}
}
Generated by GNU enscript 1.6.4.