Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
temp_fans.cpp@0:c8b4d53dbed4, 2021-03-15 (annotated)
- Committer:
- tsmith10
- Date:
- Mon Mar 15 21:29:34 2021 +0000
- Revision:
- 0:c8b4d53dbed4
- Child:
- 1:1faa8c2f5507
First draft
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tsmith10 | 0:c8b4d53dbed4 | 1 | #include "mbed.h" |
| tsmith10 | 0:c8b4d53dbed4 | 2 | #include "temp_fans.h" |
| tsmith10 | 0:c8b4d53dbed4 | 3 | |
| tsmith10 | 0:c8b4d53dbed4 | 4 | |
| tsmith10 | 0:c8b4d53dbed4 | 5 | temp_fans::temp_fans():LM35(A2),fan(A5){} |
| tsmith10 | 0:c8b4d53dbed4 | 6 | |
| tsmith10 | 0:c8b4d53dbed4 | 7 | void temp_fans::temp_fans_UPDATE(void) { |
| tsmith10 | 0:c8b4d53dbed4 | 8 | |
| tsmith10 | 0:c8b4d53dbed4 | 9 | |
| tsmith10 | 0:c8b4d53dbed4 | 10 | float temp, sum, duty_cycle; |
| tsmith10 | 0:c8b4d53dbed4 | 11 | int i; //For loop counter |
| tsmith10 | 0:c8b4d53dbed4 | 12 | fan.period_us(40.0f); // 40 micro second period (25KHz) for fan PWM |
| tsmith10 | 0:c8b4d53dbed4 | 13 | |
| tsmith10 | 0:c8b4d53dbed4 | 14 | |
| tsmith10 | 0:c8b4d53dbed4 | 15 | sum=0; //Reset sum for average value of input readings |
| tsmith10 | 0:c8b4d53dbed4 | 16 | for(i=0; i<10; i++) { |
| tsmith10 | 0:c8b4d53dbed4 | 17 | sum=sum+LM35.read(); |
| tsmith10 | 0:c8b4d53dbed4 | 18 | wait_us(10000); |
| tsmith10 | 0:c8b4d53dbed4 | 19 | } |
| tsmith10 | 0:c8b4d53dbed4 | 20 | |
| tsmith10 | 0:c8b4d53dbed4 | 21 | temp=(sum/10*3.3)*100; //Average & factor for analog to temperature conversion |
| tsmith10 | 0:c8b4d53dbed4 | 22 | |
| tsmith10 | 0:c8b4d53dbed4 | 23 | printf("Temperature: %d oC\r\n", (int)temp); |
| tsmith10 | 0:c8b4d53dbed4 | 24 | |
| tsmith10 | 0:c8b4d53dbed4 | 25 | if (temp<15) { |
| tsmith10 | 0:c8b4d53dbed4 | 26 | duty_cycle=0; //Fans not in motion |
| tsmith10 | 0:c8b4d53dbed4 | 27 | } |
| tsmith10 | 0:c8b4d53dbed4 | 28 | else if(temp>75) { |
| tsmith10 | 0:c8b4d53dbed4 | 29 | printf("ERROR: SYSTEM OVERHEATED!\r\n"); |
| tsmith10 | 0:c8b4d53dbed4 | 30 | duty_cycle=100; //Fans at MAX speed |
| tsmith10 | 0:c8b4d53dbed4 | 31 | } |
| tsmith10 | 0:c8b4d53dbed4 | 32 | else { |
| tsmith10 | 0:c8b4d53dbed4 | 33 | duty_cycle=(temp-15)*2/100; //Incrementing PWM value by 2% per degree from [15-75 oC] |
| tsmith10 | 0:c8b4d53dbed4 | 34 | } |
| tsmith10 | 0:c8b4d53dbed4 | 35 | |
| tsmith10 | 0:c8b4d53dbed4 | 36 | fan.write(duty_cycle); // Duty cycle (relative to period) |
| tsmith10 | 0:c8b4d53dbed4 | 37 | printf("PWM Speed: %d%\r\n\r\n", (int)(duty_cycle*100)); |
| tsmith10 | 0:c8b4d53dbed4 | 38 | |
| tsmith10 | 0:c8b4d53dbed4 | 39 | |
| tsmith10 | 0:c8b4d53dbed4 | 40 | } |
| tsmith10 | 0:c8b4d53dbed4 | 41 | |
| tsmith10 | 0:c8b4d53dbed4 | 42 | |
| tsmith10 | 0:c8b4d53dbed4 | 43 | |
| tsmith10 | 0:c8b4d53dbed4 | 44 |
