Created
February 11, 2016 21:27
-
-
Save moorepants/23358d74bf51594267ab to your computer and use it in GitHub Desktop.
Code for the temperature controller in EME185.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment