I need to increase the resolution of a thermistor for a project I'm working on and oversampling seemed like a pretty good solution to that problem. I found this code and it seems to be exactly what I need, but when I run it, I don't get anything on the serial monitor. I feel like I'm missing something really obvious but I just can't find what it is. I know the thermistor is wired correctly because my other code works, just with lower resolution than I need. Here is the code:
#define BUFFER_SIZE 16 // For 12 Bit ADC data
volatile uint32_t result[BUFFER_SIZE];
volatile int i = 0;
volatile uint32_t sum=0;
/*
ADC and Timer1 setup function
Argument 1: uint8_t channel must be between 0 and 7
Argument 2: int frequency must be integer from 1 to 600
WARNING! Any value above 600 is likely to result in a loss
of data and could result in a reduced accuracy of ADC
conversions
*/
void setupADC(uint8_t channel, int frequency)
{
cli();
ADMUX = channel | _BV(REFS0);
ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADATE) | _BV(ADIE);
ADCSRB |= _BV(ADTS2) | _BV(ADTS0); //Compare Match B on counter 1
TCCR1A = _BV(COM1B1);
TCCR1B = _BV(CS11)| _BV(CS10) | _BV(WGM12);
uint32_t clock = 250000;
uint16_t counts = clock/(BUFFER_SIZE*frequency);
OCR1B = counts;
TIMSK1 = _BV(OCIE1B);
ADCSRA |= _BV(ADEN) | _BV(ADSC);
sei();
}
ISR(ADC_vect)
{
result[i] = ADC;
i=++i&(BUFFER_SIZE-1);
for(int j=0;j<BUFFER_SIZE;j++)
{
sum+=result[j];
}
if(i==0)
{
double Temp;
// See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
Temp = log(((10240000/sum) - 10000));
Temp = 1 / (0.001285 + (0.0002362 * Temp) + (0.00000009285 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
double fTemp;
double temp = Temp; // Read sensor
fTemp = (temp * 1.8) + 32.0; // Convert to USA
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" C / ");
Serial.print(fTemp);
Serial.println(" F");
sum = sum>>2;
Serial.println(sum,DEC);
}
sum=0;
TCNT1=0;
}
ISR(TIMER1_COMPB_vect)
{
}
void setup()
{
pinMode(0, INPUT);
Serial.begin(9600);
}
void loop()
{
setupADC(0,40);
}
Any help would be greatly appreciated. Thanks in advance!