Advertisement
Guest User

Untitled

a guest
May 6th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. // which analog pin to connect
  2. #define THERMISTORPIN A1
  3. // resistance at 25 degrees C
  4. #define THERMISTORNOMINAL 10000
  5. // temp. for nominal resistance (almost always 25 C)
  6. #define TEMPERATURENOMINAL 25
  7. // how many samples to take and average, more takes longer
  8. // but is more 'smooth'
  9. #define NUMSAMPLES 11
  10. // The beta coefficient of the thermistor (usually 3000-4000)
  11. #define BCOEFFICIENT 3380
  12. // the value of the 'other' resistor
  13. #define SERIESRESISTOR 10000
  14.  
  15. int samples[NUMSAMPLES];
  16. int counter = 0;
  17. int switchPin = 1;
  18. int LED = 0; // handlebar led high
  19.  
  20. void setup()
  21. {
  22. pinMode(switchPin, INPUT);
  23. pinMode(LED, OUTPUT);
  24. }
  25.  
  26. void loop()
  27. {
  28. uint8_t i;
  29. float average;
  30.  
  31. //Handle input
  32. int switchVal = digitalRead(switchPin);
  33.  
  34. if(switchVal == HIGH)
  35. {
  36. delayMicroseconds(350000);
  37. counter ++;
  38.  
  39. //Reset count if over max mode number
  40. if(counter >= 4)
  41. {
  42. counter = 0;
  43. }
  44. }
  45. else
  46. {
  47. //Change mode
  48. switch (counter)
  49. {
  50. case 0:
  51. // take N samples in a row, with a slight delay
  52. for (i=0; i< NUMSAMPLES; i++)
  53. {
  54. samples[i] = analogRead(THERMISTORPIN);
  55. delayMicroseconds(1000);
  56. }
  57.  
  58. // average all the samples out
  59. average = 0;
  60.  
  61. for (i=0; i< NUMSAMPLES; i++)
  62. {
  63. average += samples[i];
  64. }
  65.  
  66. average /= NUMSAMPLES;
  67.  
  68. // convert the value to resistance
  69. average = 1023 / average - 1;
  70. average = SERIESRESISTOR / average;
  71.  
  72. float steinhart;
  73. steinhart = average / THERMISTORNOMINAL; // (R/Ro)
  74. steinhart = log(steinhart); // ln(R/Ro)
  75. steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
  76. steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  77. steinhart = 1.0 / steinhart; // Invert
  78. steinhart -= 273.15; // convert to C
  79.  
  80.  
  81. if (steinhart > 30)
  82. {
  83. analogWrite(LED,10);
  84. }
  85. else
  86. {
  87. if (steinhart < 28) // ramp light on after this temp
  88. analogWrite(LED,255);
  89. }
  90.  
  91. break;
  92.  
  93. case 1: //medium mode
  94. // take NUMSAMPLES samples in a row, with a slight delay
  95.  
  96. for (i=0; i< NUMSAMPLES; i++)
  97. {
  98. samples[i] = analogRead(THERMISTORPIN);
  99. delayMicroseconds(1000);
  100. }
  101. // average all the samples out
  102. average = 0;
  103.  
  104. for (i=0; i< NUMSAMPLES; i++)
  105. {
  106. average += samples[i];
  107. }
  108.  
  109. average /= NUMSAMPLES;
  110. // convert the value to resistance
  111. average = 1023 / average - 1;
  112. average = SERIESRESISTOR / average;
  113. float steinhart1;
  114. steinhart1 = average / THERMISTORNOMINAL; // (R/Ro)
  115. steinhart1 = log(steinhart1); // ln(R/Ro)
  116. steinhart1 /= BCOEFFICIENT; // 1/B * ln(R/Ro)
  117. steinhart1 += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  118. steinhart1 = 1.0 / steinhart1; // Invert
  119. steinhart1 -= 273.15; // convert to C
  120.  
  121. if (steinhart1 > 30)
  122. {
  123. analogWrite(LED,10);
  124. }
  125. else
  126. {
  127. if (steinhart1 < 28)
  128. analogWrite(LED,125);
  129. }
  130.  
  131. break;
  132.  
  133.  
  134. case 2: // lo mode
  135. // take N samples in a row, with a slight delay
  136. for (i=0; i< NUMSAMPLES; i++)
  137. {
  138. samples[i] = analogRead(THERMISTORPIN);
  139. delayMicroseconds(1000);
  140. }
  141.  
  142. // average all the samples out
  143. average = 0;
  144.  
  145. for (i=0; i< NUMSAMPLES; i++)
  146. {
  147. average += samples[i];
  148. }
  149.  
  150. average /= NUMSAMPLES;
  151. // convert the value to resistance
  152. average = 1023 / average - 1;
  153. average = SERIESRESISTOR / average;
  154. float steinhart3;
  155. steinhart3 = average / THERMISTORNOMINAL; // (R/Ro)
  156. steinhart3 = log(steinhart3); // ln(R/Ro)
  157. steinhart3 /= BCOEFFICIENT; // 1/B * ln(R/Ro)
  158. steinhart3 += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  159. steinhart3 = 1.0 / steinhart3; // Invert
  160. steinhart3 -= 273.15; // convert to C
  161.  
  162. if (steinhart3 > 30)
  163. {
  164. analogWrite(LED,10);
  165. }
  166. else
  167. {
  168. if (steinhart3 < 28)
  169. analogWrite(LED,50);
  170. }
  171.  
  172. break;
  173.  
  174. case 3: //off
  175. analogWrite(LED,01);
  176.  
  177. break;
  178.  
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement