Problem reading random noise generator

If that's happening, why do they seem to be grouping at the top and bottom ends?

An observation: most (maybe all) of the circuits I have seen like that use 12V or higher (including the one in your link which calls for 13V). I vaguely recall one author claiming that 12V is the minimum necessary for the circuit to work correctly. But, that could easily be because of the transistors he was using.

If you want continued help with the software I suggest you post the current version of your sketch.

This may give you some troubleshooting ideas...
http://forum.arduino.cc/index.php?topic=161682.0

Thank you, Coding Badly, that thread will be very helpful. I'll post my sketch tomorrow when I get back to it. This is giving me a ton of ammunition with which to improve my design!

acegard:
If that's happening, why do they seem to be grouping at the top and bottom ends?

Because the signal is moving faster in the middle of the transition, just think of a sin wave the fastest movement is at the zero crossing poi nt. those are just the levels you will miss.

Grumpy_Mike:
Don't forget that the noise will have a higher frequency in the noise that you can sample at. Therefor there will be a degree of avraging going on which will limit the readings at the top and bottom end. You need to add a low pass filter to your noise source to take it below the niquis rate.

I tried something similar a while ago and got very similar results...a disproportionate binfull of 0s and 255s in my case....

im sure its a sampling frequency/nyquist issue. in my case I was cleaning up the signal with an lm393...I had biased my zener +NPN amp to be abt 2.5 midpoint and fed that plus a pot.tweaked Vcc/2 to the 393.

even tho my scope is ancient and slow, I could see that if I shifted the vcc midpoint up or down slightly in the wide blur of random fuzz, even tho it was still sort of middle.ish my distribution would vary widely...i did actually get a fairly flat distribution at one point, but I could never repeat it. I concluded I was being thwarted by the slew rate of the 393, or some kind of "beat frequency" between a less than nyquist rate on the max 393 slew rate...

my knowledge and scope were well below required level at the time, so I took the easy solution..I gave up!

ps they probably still are, but at least I can send you some empathy, if not fix your entropy....

Just to reinforce what Coding Badly pointed out, I made a circuit based on that type of design using 2n2222 and I found that a supply of 5V and 9V didn't work well. It needed at least 12V.

Avalanche noise circuits have several issues as random number generators. The first, is that they exhibit a fair degree of bias, regardless with how you capture the noise (ADC or comparator).

My experiments have indicated that two stages of 'whitening' are required to remove the bias from the readings. I use a Von Neumann filter followed by either some form of linear feed back shift register (LFSR). In general I have found that it requires at least a 4:1 whitening ratio to get a reasonably uniform RNG. For LFSR, I have used either a simple XOR'ing of consecutive bytes (which worked for one example circuit with A2D) to a Jenkings one-at-a-time hash on my second implementation which required more whitening due to a different noise source (5V zener source, no 10-15V supply needed).

The second issue with avalanche noise random number generator circuits is that the balance of 1's to 0's will change over time. If your code doesn't accomodate such changes your RNG will stop producing uniform RN when the bias exceeds your whitening technique's ability to cope. As you can see from the pages below, I noticed LARGE changes in the circuits balance point as it aged.

If your interested in my experiments, which include the code I derived from Rob Sewards median calculation approach are available from

https://code.google.com/p/avr-hardware-random-number-generation/wiki/AvalancheNoise

The author of the HRNG link you posted claims to have gone to some effort to reach his goal, namely

trying to design a circuit that could produce a symmetrical, zero-average noise signal from a reverse biased p-n junction in avalanche breakdown

but it really isn't clear how he achieved and verified the output of the hardware other than with software. Your circuit has no provision for adjustments and can't produce a zero mean signal. Also, he used a digital input to generate "1"s and "0"s -- your use of the ADC throws in an additional complication. So you may be fighting an uphill battle.

I don't mean to be discouraging! These things are fun and challenging.

As an aside, it is interesting to listen to various different transistors in avalanche breakdown. You can do this by coupling the output transistor via a capacitor to the high gain phono input of an audio amplifier. There are a few different processes that lead to breakdown, and these manifest themselves as clicks, pops and hisses of different apparent pitches.

el_supremo:
Just to reinforce what Coding Badly pointed out, I made a circuit based on that type of design using 2n2222 and I found that a supply of 5V and 9V didn't work well. It needed at least 12V.

I disagree. my original circuit was stolen from a 12v design. I dropped it to 5v, changed the NPN bias to get 2.5 ish midpoint and enough gain to exceed the upper and lower hystersis points of the 393. it didnt work...until I realised that the key point is the current through the noise source is quite critical. i guess most mfrs try to reruce noise and though zeners are inherently noisy there is a point on their curve where they are most noisy. we are talking nanocurrents here, any help you can get to boost the starting point is a bonus...I rebiased the zener to get the exact datasheet current point for peak noise and it burst into life....

albeit with the nyquist sample rate problem, so im not cel3brating yet, just my 2c that 5v circuits can be made noisy with a bit of care,a datasheet and a calculator

BareMetal:

el_supremo:
Just to reinforce what Coding Badly pointed out, I made a circuit based on that type of design using 2n2222 and I found that a supply of 5V and 9V didn't work well. It needed at least 12V.

I disagree. my original circuit was stolen from a 12v design. I dropped it to 5v, changed the NPN bias to get 2.5 ish midpoint and enough gain to exceed the upper and lower hystersis points of the 393. it didnt work...until I realised that the key point is the current through the noise source is quite critical. i guess most mfrs try to reruce noise and though zeners are inherently noisy there is a point on their curve where they are most noisy. we are talking nanocurrents here, any help you can get to boost the starting point is a bonus...I rebiased the zener to get the exact datasheet current point for peak noise and it burst into life....

albeit with the nyquist sample rate problem, so im not cel3brating yet, just my 2c that 5v circuits can be made noisy with a bit of care,a datasheet and a calculator

Avalanche noise is generally an order of magnitude lower for 5V and below sources (typically zeners') when compared to 12V+ sources, even when current is adjusted for. Further the reverse bias transistor used in the OP example doesn't enter the breakdown zone until about 8-9V, though I have found a few examples of the 2N3904 (the transistor typically specified) that entered breakdown at lower voltages, the mean tends to be around 9V, with noise peaking near 12V.

The 'best' current for maximum noise varies with the device; however, the ones I have tested have generally been 5-50 uA.

Further, there have been several people who have tested a variety of such transistors, and the 2N3904 tend to produce the most noise.

jremington:
The author of the HRNG link you posted claims to have gone to some effort to reach his goal, namely

trying to design a circuit that could produce a symmetrical, zero-average noise signal from a reverse biased p-n junction in avalanche breakdown

but it really isn't clear how he achieved and verified the output of the hardware other than with software. Your circuit has no provision for adjustments and can't produce a zero mean signal. Also, he used a digital input to generate "1"s and "0"s -- your use of the ADC throws in an additional complication. So you may be fighting an uphill battle.

The original author of the circuit the OP is using believed that a relatively even balance between 0's and 1's produce a non-biased output (it doesn't), but they didn't appear to actually test that asuumption since they used 'whitening' techniques from the start:

This time there is no need to minimize bias inherent in the operation of the generator, if all went well we should get a probability of 1s and 0s very close to 50%. Of course I'm going to assume that the bit-stream still contain bias and correlation, so a de-skewing technique for reducing bias and correlation will be used anyway.
On the software side I have basically used the same code I had written for the Chua random generator: at the beginning GPIO4 is configured as input, then one bit is read (and its value is optionally printed), next we wait for a delay and repeat. This delay has been set to 500?s, producing a 2000bit/s stream.

Yes, I see. He ran a simple program to read two independently generated output streams from the HRNG and produce a third stream, as outlined by the following (from Random Sequence Generator based on Chua Circuit )

John von Neumann invented a simple algorithm to fix simple bias, and reduce correlation. It considers successive pairs of consecutive, non-overlapping bits from the input stream, taking one of three actions: when two successive bits are equal, they are discarded; a sequence of 1,0 becomes a 1; and a sequence of 0,1 becomes a 0. It thus represents a falling edge with a 1, and a rising edge with a 0. This eliminates simple bias, and is easy to implement. This technique works no matter how the bits have been generated. It cannot assure randomness in its output, however. What it can do is transform a biased random bit stream into an unbiased one. However, significant numbers of bits are discarded. Thanks to the hardware bias reduction, we can affirm that the probability of 1 is p ~ 1/2, and in this case the discard of input pairs is minimum, giving us an output stream that is 1/4 the length of the input on average.

Wow, it's obvious that I don't have nearly as good of a grasp of this as I thought I did! I'm going to spend the morning reading all the responses and sources and come back when I can post an informed, coherent reply!
Thank you guys for your great, informative responses.

wanderson:
Avalanche noise is generally an order of magnitude lower for 5V and below sources (typically zeners') when compared to 12V+ sources, even when current is adjusted for. Further the reverse bias transistor used in the OP example doesn't enter the breakdown zone until about 8-9V, though I have found a few examples of the 2N3904 (the transistor typically specified) that entered breakdown at lower voltages, the mean tends to be around 9V, with noise peaking near 12V.

The 'best' current for maximum noise varies with the device; however, the ones I have tested have generally been 5-50 uA.

Further, there have been several people who have tested a variety of such transistors, and the 2N3904 tend to produce the most noise.

I am limited to a voltage source of 9V for the final project, unfortunately, so I chose the 2N2222 as opposed to the 2N3904 because, even though it might not produced as much noise, it looks like it's easier to push into breakdown at lower voltages according to the example I cited in the OP. If I'm off base here, let me know.

BareMetal:
I disagree. my original circuit was stolen from a 12v design. I dropped it to 5v, changed the NPN bias to get 2.5 ish midpoint and enough gain to exceed the upper and lower hystersis points of the 393. it didnt work...until I realised that the key point is the current through the noise source is quite critical. i guess most mfrs try to reruce noise and though zeners are inherently noisy there is a point on their curve where they are most noisy. we are talking nanocurrents here, any help you can get to boost the starting point is a bonus...I rebiased the zener to get the exact datasheet current point for peak noise and it burst into life....

albeit with the nyquist sample rate problem, so im not cel3brating yet, just my 2c that 5v circuits can be made noisy with a bit of care,a datasheet and a calculator

So you're using a Zener diode to generate avalanche noise as opposed to transistor breakdown? Does your circuit look something like this, getting the noise from the zener and then using the two transistors to amplify that?

acegard:
So you're using a Zener diode to generate avalanche noise as opposed to transistor breakdown? Does your circuit look something like this, getting the noise from the zener and then using the two transistors to amplify that?

The reverse biased transistor used in the original circuit you cited, as well as my original circuit, not to mention the typical circuit floating around the web for 30+ years is behaving in the exact same manner as a zener diode. This app note might help you:

I have also made some suggested changes to your circuit. First, since you are using a capacitor to isolate the noise voltage/current from the op-amp (ac-coupling) you can use a single sided rail to rail op amp supplied by 5V to perform your later amplification and buffering. I use the MCP629x family but any similar opamp should work. BTW, you may want to try a larger capacitor, say 0.1 - 0.22 uF

I also didn't show it, but adding a buffer (similar to what you have on the first op amp following the transistor) to your voltage divider would be beneficial, particularly if you keep the resistors your using to connect the divider to the + input of the op amps.

I

acegard:
I am limited to a voltage source of 9V for the final project...

9V battery?

acegard:
Wow, it's obvious that I don't have nearly as good of a grasp of this as I thought I did! I'm going to spend the morning reading all the responses and sources and come back when I can post an informed, coherent reply!
Thank you guys for your great, informative responses.

wanderson:
Avalanche noise is generally an order of magnitude lower for 5V and below sources (typically zeners') when compared to 12V+ sources, even when current is adjusted for. Further the reverse bias transistor used in the OP example doesn't enter the breakdown zone until about 8-9V, though I have found a few examples of the 2N3904 (the transistor typically specified) that entered breakdown at lower voltages, the mean tends to be around 9V, with noise peaking near 12V.

The 'best' current for maximum noise varies with the device; however, the ones I have tested have generally been 5-50 uA.

Further, there have been several people who have tested a variety of such transistors, and the 2N3904 tend to produce the most noise.

I am limited to a voltage source of 9V for the final project, unfortunately, so I chose the 2N2222 as opposed to the 2N3904 because, even though it might not produced as much noise, it looks like it's easier to push into breakdown at lower voltages according to the example I cited in the OP. If I'm off base here, let me know.

BareMetal:
I disagree. my original circuit was stolen from a 12v design. I dropped it to 5v, changed the NPN bias to get 2.5 ish midpoint and enough gain to exceed the upper and lower hystersis points of the 393. it didnt work...until I realised that the key point is the current through the noise source is quite critical. i guess most mfrs try to reruce noise and though zeners are inherently noisy there is a point on their curve where they are most noisy. we are talking nanocurrents here, any help you can get to boost the starting point is a bonus...I rebiased the zener to get the exact datasheet current point for peak noise and it burst into life....

albeit with the nyquist sample rate problem, so im not cel3brating yet, just my 2c that 5v circuits can be made noisy with a bit of care,a datasheet and a calculator

So you're using a Zener diode to generate avalanche noise as opposed to transistor breakdown? Does your circuit look something like this, getting the noise from the zener and then using the two transistors to amplify that?

yep, but with lm393 replacing 2nd xistor stage, op pulled up to give what I hoped was a random stream of digits, which I could count 8 off at a time to give me a series of random 8bit numbers, or so I naively thought....but hey im a,relative newbie to amps n volts, so I forgave myself. it seemd like a good idea. it taught me how to bias a transistor, at least...so it was worthwhile for my hobby needs. my main aim in all this is to learn learn learn not make millions designing a better microwidget.

the "zener" was a ZXRE4041 OR 1004 cant remember which and the peak noise current was something like 7 or 8 uA I think, which is bang in line with what the other far more knowledgeable posters have said. I shall drop out now in the presence of such greatness, my 2c has been spent! these blokes know whst they are on about, im just a nosey amateur.

BareMetal:
yep, but with lm393 replacing 2nd xistor stage, op pulled up to give what I hoped was a random stream of digits, which I could count 8 off at a time to give me a series of random 8bit numbers, or so I naively thought....but hey im a,relative newbie to amps n volts, so I forgave myself. it seemd like a good idea. it taught me how to bias a transistor, at least...so it was worthwhile for my hobby needs. my main aim in all this is to learn learn learn not make millions designing a better microwidget.

the "zener" was a ZXRE4041 OR 1004 cant remember which and the peak noise current was something like 7 or 8 uA I think, which is bang in line with what the other far more knowledgeable posters have said. I shall drop out now in the presence of such greatness, my 2c has been spent! these blokes know whst they are on about, im just a nosey amateur.

Sounds like you use this circuit as a basis; https://mywebspace.wisc.edu/lnmaurer/web/minirng/minirng.html Which uses a ZXRE1004 voltage reference (kindof a zener) as the noise source. The key to that circuit is the use of a high slew rate single sided rail to rail op amp. The author used a OPA2340 (or a MCP6282), I don't think a LM393 would be up to the job. I implemented the circuit with the original OPA2340 as well as my go to MCP629x series, and both worked great. Even though the circuit produced a good balance of 1's and 0's (near 50/50) it still required a fair bit of whitening to produce a uniform distribution. Attached are two graphs. The first shows the distribution with no whitening, the second shows a two tiered whitening approach; Von Neumann followed by a Jenkins one at a time hash.

Here are the ent results for the first test; followed by the results for the second

Entropy = 7.655314 bits per byte.

Optimum compression would reduce the size
of this 22124796 byte file by 4 percent.

Chi square distribution for 22124796 samples is 12926784.31, and randomly
would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 126.1181 (127.5 = random).
Monte Carlo value for Pi is 3.375643870 (error 7.45 percent).
Serial correlation coefficient is -0.000318 (totally uncorrelated = 0.0).
Entropy = 7.999928 bits per byte.

Optimum compression would reduce the size
of this 2679092 byte file by 0 percent.

Chi square distribution for 2679092 samples is 266.62, and randomly
would exceed this value 29.59 percent of the times.

Arithmetic mean value of data bytes is 127.5069 (127.5 = random).
Monte Carlo value for Pi is 3.143365844 (error 0.06 percent).
Serial correlation coefficient is -0.000298 (totally uncorrelated = 0.0).

Which just goes to show how nescessary whitening is for any TRNG.

Here is Arduino functions to implement the two whitening algorithms I used:

uint8_t pool[POOL_SIZE];
uint8_t pool_position;

uint32_t jenkins_one_at_a_time_hash(uint8_t *key, size_t len)
{
    uint32_t hash, i;
    for(hash = i = 0; i < len; ++i)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

// This routine takes the bit stream from the first phase of bias removal and then
// adds a second stage of bias removal by xor'ng two consequetive bytes.  
// Depending upon the current operating mode, will dictate how the resulting byte
// is processed
void buildNum(byte input)
{
  static uint8_t tmp = 0;
  static int num_counter = 0;
  uint32_t out;

  if (input == 1)
  {
    tmp = (tmp << 1) | 0x01;
  } else {
    tmp = (tmp << 1); 
  }
  num_counter++;
  num_counter %= 8;
  if(num_counter == 0)
  {
    pool_position++;
    pool[pool_position - 1] = tmp;
    if (pool_position == POOL_SIZE)
    {
      // Ok, we have filled the pool up.  Time to hash the values for an output
      out = jenkins_one_at_a_time_hash(pool, POOL_SIZE);
      Serial.println(out);
      pool_position = 0;
      tmp = 0;  
    }
  }
}

// This routine implements the von Neumann algorithm for removing bias from a bit stream
void vonNeumann(byte input)
{
  static int count = 1;
  static boolean previous = 0;
  static boolean flip_flop = 0;
  
  flip_flop = !flip_flop;

  if(flip_flop)
  {
    if(input == 1 && previous == 0)
    {
      buildNum(0);
    } else if (input == 0 && previous == 1) {
      buildNum(1); 
    }
  }
  previous = input;
}

I feed the bits to the VonNeumann function which starts the whitening process. The BuildNum function takes the preliminary whitened output from the VonNeumann function and builds a queue of 16-bytes, which are then fed to the Jenkins hash function, which produces a 32-bit (uint32_t) value which is then output as the random number. It was these processed numbers which produced the second set of results above. The last is an image of the circuit I breadboarded for testing this ZXRE1004 noise source.

yes, I think, you are right, I only had 393 somimtried to use my limited knowledge to make at least something to work, even if far from perfect. i also couldnt find his valuemfor v+, hence my cheapmhack and recalculation to 5v. with your expert advice I may get the right op amp and try again. as i said before, I did get some sort of output, and at one sweet spot which I coild never replicate, my rudimentary processing sketch produced a pretty good looking distribution.

the one big thing I learned from this "failure" is that there is apparently random (at least to a novice) and then there is Random.

I also only have A level maths from 40years ago, so figuring out just how random my random was is beyond me. I am in awe of folk that can do maths or play the piano...

your excellent contribution has reignited my curiosity...I might well rebuild and report back, but dont hold your breath!

Okay, after a long day of testing and tweaking and incorporating the suggestions given, I have some results of my own working mainly from this design as a starting point, but changing the transistors to 2N2222s and the voltage divider resistors to 10K and 5.6K, to keep it at the same relative level from 9V as opposed to 12V. Using this circuit, I compared a sketch that I wrote, emulating the code on Rob Seward's page but mainly refactoring, to Rob Seward's code. I noticed that as the sample size increased, it became apparent that the results tended to bunch toward the low end of the spectrum. I also have some specific questions, and some answers to yours:

Yes.

wanderson:
I have also made some suggested changes to your circuit. First, since you are using a capacitor to isolate the noise voltage/current from the op-amp (ac-coupling) you can use a single sided rail to rail op amp supplied by 5V to perform your later amplification and buffering. I use the MCP629x family but any similar opamp should work. BTW, you may want to try a larger capacitor, say 0.1 - 0.22 uF

I have abandoned the amplification stage for now, as this appears to be working. Tomorrow morning, the lab opens again and I'm going to go in and put everything on the scope to see what it looks like and see if I can get better performance when adding an amplification stage. What characteristics should I look for in an op-amp that would be "good" for this application?

wanderson:
The 'best' current for maximum noise varies with the device; however, the ones I have tested have generally been 5-50 uA.

Further, there have been several people who have tested a variety of such transistors, and the 2N3904 tend to produce the most noise.

Is this current you speak of the base-emitter current of the "noisy" transistor?

Grumpy_Mike:
Don't forget that the noise will have a higher frequency in the noise that you can sample at. Therefor there will be a degree of avraging going on which will limit the readings at the top and bottom end. You need to add a low pass filter to your noise source to take it below the niquis rate.

I tried this. I measured the frequency at which the Arduino was sampling the input (roughly 7.3 kHz), then designed a first-order lowpass filter (diagram attached) with unity gain and a cutoff frequency of 3.65 kHz which, unless I am wrong, is the Nyquist sampling frequency. When I connected it, however, it either drove all my samples to 0 or 1023 (reading from the analog input). Tomorrow, I am going to put it on the scope and see what is happening, but until then, are there any ideas? I am aware that the tendency of the distribution to accumulate at the lower ranges is likely due to this, but I'm not sure how to remedy it.

Attached is my Excel spreadsheet with my results, as well as diagrams of my updated circuits. The op-amp is a TL082, powered by +9V and gnd.

Edit: forgot to post my code. Here it is! I'm having trouble getting the Von Neumann algorithm to work, so all it does right now is XOR whitening.

///A sketch to sample and display the reading at analog 0 to test the generation of random noise

#define inPin A0
#define led 13
#define CALIBRATE_SIZE 30000

unsigned long time = 0;
long times = 0;
int mini = 1023;
int maxi = 0;
int threshold = 0; 
unsigned int Bins[256];
unsigned int mapped[20];
unsigned int mapped2[20];
unsigned long samples = 0;
unsigned long maxSamples = 500000;

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(led,OUTPUT);
  Serial.begin(19200);
  for(int i = 0; i < 256; i++)
  {
    Bins[i] = 0;
  }
  
  for(int i = 0; i < 20; i++)
  {
    mapped[i] = 0;
    mapped2[i] = 0;
  }
    

}

void loop()
{
  Serial.println("Calibrating...");
  threshold = calibrate();
  Serial.print("Finished calibration. Threshold = ");Serial.println(threshold);
  delay(1000);
  //reset the bins
  for(int i = 0; i < 256; i++)
  {
    Bins[i] = 0;
  }
  while(!Serial.available() && samples < maxSamples)
  {
    byte result = collectData();
    Bins[result]++;
    int m = result%21;;
    mapped[m]++;
    int m2 = map(result, 0, 255, 0,19);
    mapped2[m2]++;
    //Serial.println(result);
    samples++;
    Serial.println(samples);
  }
 
  Serial.println("Full distribution:");
  for(int i = 0; i < 256; i++)
  {
    Serial.print(i+1);Serial.print(": ");Serial.println(Bins[i]);
  }
  Serial.println("Divided:");
  for(int i = 0; i < 20; i++)
  {
    Serial.print(i+1);Serial.print(": ");Serial.println(mapped[i]);
  }
  
  Serial.println("Mapped:");
  for(int i = 0; i < 20; i++)
  {
    Serial.print(i+1);Serial.print(": ");Serial.println(mapped2[i]);
  }
  Serial.print("Samples: ");
  Serial.println(samples);
  while(true)
  {}
 
}

byte collectData()//returns an 8-bit number from the random bitstream
{
  byte out = 0;
  
  for(int i = 0; i < 8; i++)
  {
    static boolean flipFlop = 0;
    //unbias with XOR whitening
    int input = analogRead(0);
    //int first = analogRead(inPin);
    //boolean one = 0;
    //boolean two = 0;
    //if(first >= threshold)
      //one = 1;
    //else if(first < threshold)
      //one = 0;
    //int second = analogRead(0);
    //if(second >= threshold)
      //two = 1;
    //else if(second < threshold)
      //two = 0;
      
      
    //if(first && !second)
      //out = (out <<1) | 0x01;
    //else if (!first && second)
      //out = (out <<1);
    flipFlop!= flipFlop;
    boolean in = 0;
    if(input >= threshold)
      in = (1 ^ flipFlop);
      
    else if(input < threshold)
      in = (0 ^ flipFlop);
    
    if(in)
      out = (out <<1) | 0x01;
    else
      out = (out <<1);
  }
  
  return out;
}

unsigned int calibrate()
{
  digitalWrite(led,HIGH);
  for(int i = 0; i < CALIBRATE_SIZE; i++)
  {
    int adc_value = analogRead(inPin);
    byte analog = (byte)adc_value;//truncates to 0-255
    Bins[analog]++;
  }
  
  //find the median
  unsigned long half;
  unsigned long total = 0;
  int i;

  for(i=0; i < 256; i++){
    total += Bins[i];
  }	

  half = total >> 1;
  total = 0;
  for(i=0; i < 256; i++){
    total += Bins[i];
    if(total > half){
      break;
    }	
  }
  digitalWrite(led,LOW);
  return i;
}

Statistics.xlsx (56 KB)

1 Like