Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int tempPin = A1;
- int fanPin = 11;
- double tempMeasurement;
- double tempError;
- int fanPWM;
- double pconst = 20;
- double desTemp = 27;
- void setup() {
- pinMode(fanPin, OUTPUT);
- pinMode(tempPin, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- tempMeasurement = readTemp();
- if (tempMeasurement < desTemp) {
- fanPWM = 0;
- digitalWrite(fanPin, LOW);
- } else {
- tempError = tempMeasurement - desTemp;
- fanPWM = (int) (pconst * tempError);
- if (fanPWM > 255) {
- fanPWM = 255;
- }
- analogWrite(fanPin, fanPWM);
- Serial.print("Fan percentage is:");
- Serial.println(fanPWM * 100 / 255);
- }
- Serial.print("Temp: ");
- Serial.println(tempMeasurement);
- delay(100);
- }
- double readTemp() {
- int tempM = analogRead(tempPin);
- double tempD = (double) tempM;
- tempD = tempD * (5.0 / 1023.0) * 1000.0 * (1 / 10.0);
- return tempD;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement