Scott Vincent / temp_fan

temp_fans.cpp

Committer:
tsmith10
Date:
2021-03-15
Revision:
0:c8b4d53dbed4
Child:
1:1faa8c2f5507

File content as of revision 0:c8b4d53dbed4:

#include "mbed.h"
#include "temp_fans.h"


temp_fans::temp_fans():LM35(A2),fan(A5){}

void temp_fans::temp_fans_UPDATE(void) {
    
    
    float temp, sum, duty_cycle; 
    int i; //For loop counter
    fan.period_us(40.0f); // 40 micro second period (25KHz) for fan PWM
   
    
    sum=0; //Reset sum for average value of input readings
    for(i=0; i<10; i++) {
        sum=sum+LM35.read();
        wait_us(10000);
    }
    
   temp=(sum/10*3.3)*100; //Average & factor for analog to temperature conversion

    printf("Temperature: %d oC\r\n", (int)temp);
   
    if (temp<15) {
        duty_cycle=0; //Fans not in motion 
        }
    else if(temp>75) {
         printf("ERROR: SYSTEM OVERHEATED!\r\n"); 
         duty_cycle=100; //Fans at MAX speed  
    }
    else {
    duty_cycle=(temp-15)*2/100; //Incrementing PWM value by 2% per degree from [15-75 oC]
    }
    
    fan.write(duty_cycle);      // Duty cycle (relative to period)  
    printf("PWM Speed: %d%\r\n\r\n", (int)(duty_cycle*100));
  
 
}