Sunday, June 17, 2012

Fake Neuron in Arduino

I am getting ready to go ahead with electrophysiology experiments recording from the olfactory bulb.  To test my acquisition equipment and software, I created a little Arduino program that simulates a simple neuron with firing rate driven by "sniffing".  The neuron generates Poisson action potentials with the rate parameter governed by a sinusoid (sniffing).

The shield has an LED that indicates the sniffing and a speaker that indicates the spiking.  Here is the shield and program in action (turn volume to max to hear the neuron):


And here is the sketch:


/*
  Fake Neuron
  Yevgeniy Sirotin, 6/16/12
 */


#include <math.h>


unsigned long t0;
unsigned long t;
unsigned long ti;


int ts;
float r;


void setup() {
    pinMode(9, OUTPUT); 
    pinMode(7, OUTPUT);
    t0 = millis();
    
    TCCR1B = 0x01;
    
    ti = 0;
    ts = 0;
}


void loop() {
    t = millis();
    
    r = 0.5 * (1.0 + sin( (float) ((t-t0) % 2000) * 2.0 * 3.14 * 1.0 / 1000));
    analogWrite(9, (int) (255.0 * r ));
       
    if (t>=ti)
    { 
      digitalWrite(7,HIGH);
      delay(1);
      digitalWrite(7,LOW);
      
      if (r<0.1) r = 0.1;
      ti = t + (unsigned long) (-1000.0 / (100 * r) * log(random(10000) / 10000.0 ));
      
   }      
}