Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. int tempPin = A1;
  2. int fanPin = 11;
  3.  
  4. double tempMeasurement;
  5. double tempError;
  6. int fanPWM;
  7. double pconst = 20;
  8. double desTemp = 27;
  9.  
  10. void setup() {
  11. pinMode(fanPin, OUTPUT);
  12. pinMode(tempPin, INPUT);
  13. Serial.begin(9600);
  14. }
  15.  
  16. void loop() {
  17. tempMeasurement = readTemp();
  18.  
  19. if (tempMeasurement < desTemp) {
  20. fanPWM = 0;
  21. digitalWrite(fanPin, LOW);
  22. } else {
  23. tempError = tempMeasurement - desTemp;
  24. fanPWM = (int) (pconst * tempError);
  25. if (fanPWM > 255) {
  26. fanPWM = 255;
  27. }
  28. analogWrite(fanPin, fanPWM);
  29. Serial.print("Fan percentage is:");
  30. Serial.println(fanPWM * 100 / 255);
  31. }
  32. Serial.print("Temp: ");
  33. Serial.println(tempMeasurement);
  34. delay(100);
  35. }
  36.  
  37. double readTemp() {
  38. int tempM = analogRead(tempPin);
  39. double tempD = (double) tempM;
  40. tempD = tempD * (5.0 / 1023.0) * 1000.0 * (1 / 10.0);
  41. return tempD;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement