Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // which analog pin to connect
- #define THERMISTORPIN A1
- // 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 11
- // The beta coefficient of the thermistor (usually 3000-4000)
- #define BCOEFFICIENT 3380
- // the value of the 'other' resistor
- #define SERIESRESISTOR 10000
- int samples[NUMSAMPLES];
- int counter = 0;
- int switchPin = 1;
- int LED = 0; // handlebar led high
- void setup()
- {
- pinMode(switchPin, INPUT);
- pinMode(LED, OUTPUT);
- }
- void loop()
- {
- uint8_t i;
- float average;
- //Handle input
- int switchVal = digitalRead(switchPin);
- if(switchVal == HIGH)
- {
- delayMicroseconds(350000);
- counter ++;
- //Reset count if over max mode number
- if(counter >= 4)
- {
- counter = 0;
- }
- }
- else
- {
- //Change mode
- switch (counter)
- {
- case 0:
- // take N samples in a row, with a slight delay
- for (i=0; i< NUMSAMPLES; i++)
- {
- samples[i] = analogRead(THERMISTORPIN);
- delayMicroseconds(1000);
- }
- // average all the samples out
- average = 0;
- for (i=0; i< NUMSAMPLES; i++)
- {
- average += samples[i];
- }
- average /= NUMSAMPLES;
- // convert the value to resistance
- average = 1023 / average - 1;
- average = SERIESRESISTOR / 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
- if (steinhart > 30)
- {
- analogWrite(LED,10);
- }
- else
- {
- if (steinhart < 28) // ramp light on after this temp
- analogWrite(LED,255);
- }
- break;
- case 1: //medium mode
- // take NUMSAMPLES samples in a row, with a slight delay
- for (i=0; i< NUMSAMPLES; i++)
- {
- samples[i] = analogRead(THERMISTORPIN);
- delayMicroseconds(1000);
- }
- // average all the samples out
- average = 0;
- for (i=0; i< NUMSAMPLES; i++)
- {
- average += samples[i];
- }
- average /= NUMSAMPLES;
- // convert the value to resistance
- average = 1023 / average - 1;
- average = SERIESRESISTOR / average;
- float steinhart1;
- steinhart1 = average / THERMISTORNOMINAL; // (R/Ro)
- steinhart1 = log(steinhart1); // ln(R/Ro)
- steinhart1 /= BCOEFFICIENT; // 1/B * ln(R/Ro)
- steinhart1 += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
- steinhart1 = 1.0 / steinhart1; // Invert
- steinhart1 -= 273.15; // convert to C
- if (steinhart1 > 30)
- {
- analogWrite(LED,10);
- }
- else
- {
- if (steinhart1 < 28)
- analogWrite(LED,125);
- }
- break;
- case 2: // lo mode
- // take N samples in a row, with a slight delay
- for (i=0; i< NUMSAMPLES; i++)
- {
- samples[i] = analogRead(THERMISTORPIN);
- delayMicroseconds(1000);
- }
- // average all the samples out
- average = 0;
- for (i=0; i< NUMSAMPLES; i++)
- {
- average += samples[i];
- }
- average /= NUMSAMPLES;
- // convert the value to resistance
- average = 1023 / average - 1;
- average = SERIESRESISTOR / average;
- float steinhart3;
- steinhart3 = average / THERMISTORNOMINAL; // (R/Ro)
- steinhart3 = log(steinhart3); // ln(R/Ro)
- steinhart3 /= BCOEFFICIENT; // 1/B * ln(R/Ro)
- steinhart3 += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
- steinhart3 = 1.0 / steinhart3; // Invert
- steinhart3 -= 273.15; // convert to C
- if (steinhart3 > 30)
- {
- analogWrite(LED,10);
- }
- else
- {
- if (steinhart3 < 28)
- analogWrite(LED,50);
- }
- break;
- case 3: //off
- analogWrite(LED,01);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement