TEXT 122
Sketch Temp By theneedyguy on 7th March 2017 02:21:07 PM
  1. float temp;
  2. int tempPin = A0;
  3. int tempMin = 25;
  4. int tempMax = 35;
  5. int tempRead = 0;
  6. int fan = 6;
  7. int fanSpeed = 0;
  8.  
  9. void setup() {
  10.   pinMode(fan, OUTPUT);
  11.   pinMode(tempPin, INPUT);
  12.   Serial.begin(9600);
  13. }
  14. void loop() {
  15.   tempRead = analogRead(tempPin);
  16.   temp = map(tempRead, 0, 410, -50, 150);
  17.   Serial.println(temp);
  18.   delay(2000); // delay in between reads for stability
  19.   if((temp >= tempMin) && (temp <= tempMax)) //if temperature is higher than the minimum range
  20.   {
  21.     fanSpeed = map(temp, tempMin, tempMax, -32, -255); // the actual speed of fan
  22.     analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
  23.   }  
  24.   if(temp < tempMin) { // if temp is lower than minimum temp
  25.     fanSpeed = 0; // fan is not spinning
  26.     digitalWrite(fan, fanSpeed);
  27.   }
  28.   delay(1000);
  29. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.