Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created November 2, 2013 11:05
Show Gist options
  • Save dwblair/7277807 to your computer and use it in GitHub Desktop.
Save dwblair/7277807 to your computer and use it in GitHub Desktop.
Updated ThermalFishingBob code
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
#define RED_LED 9
#define GREEN_LED 10
#define BLUE_LED 11
const float lowReading = 77;
const float highReading = 85;
const unsigned char separatorCharacter = 255;
int samples[NUMSAMPLES];
void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
pinMode(RED_LED,OUTPUT);
pinMode(GREEN_LED,OUTPUT);
pinMode(BLUE_LED,OUTPUT);
}
void loop(void) {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
//Serial.print("Average analog reading ");
//Serial.println(average);
int reading=int(average);
//Serial.println("reading=");
//Serial.println(reading);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
//Serial.print("Thermistor resistance ");
//Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
delay(100);
float celcius=steinhart;
float fahrenheit = (celcius*1.8) + 32;
float state = normf(fahrenheit, lowReading, highReading);
int hue = map(state,0,255,359,(359*0.5)); // not the whole color wheel
setLedColorHSV(hue,1,1);
Serial.print(fahrenheit);
Serial.print(" degrees F, state: ");
Serial.println(state);
}
float normf(float x, float low, float high) {
float y = (x - low) * 255.f / (high - low);
if(y > 255) {
y = 255;
}
if(y < 0) {
y = 0;
}
return y;
}
void setLedColorHSV(int h, double s, double v) {
//this is the algorithm to convert from RGB to HSV
double r=0;
double g=0;
double b=0;
double hf=h/60.0;
int i=(int)floor(h/60.0);
double f = h/60.0 - i;
double pv = v * (1 - s);
double qv = v * (1 - s*f);
double tv = v * (1 - s * (1 - f));
switch (i)
{
case 0: //rojo dominante
r = v;
g = tv;
b = pv;
break;
case 1: //verde
r = qv;
g = v;
b = pv;
break;
case 2:
r = pv;
g = v;
b = tv;
break;
case 3: //azul
r = pv;
g = qv;
b = v;
break;
case 4:
r = tv;
g = pv;
b = v;
break;
case 5: //rojo
r = v;
g = pv;
b = qv;
break;
}
//set each component to a integer value between 0 and 255
int red=constrain((int)255*r,0,255);
int green=constrain((int)255*g,0,255);
int blue=constrain((int)255*b,0,255);
setLedColor(red,green,blue);
}
//Sets the current color for the RGB LED
void setLedColor(int red, int green, int blue) {
//Note that we are reducing 1/4 the intensity for the green and blue components because
// the red one is too dim on my LED. You may want to adjust that.
analogWrite(RED_LED,red); //Red pin attached to 9
analogWrite(GREEN_LED,green); //Red pin attached to 9
analogWrite(BLUE_LED,blue); //Red pin attached to 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment