Control fan speed by temperature sensor reading. Set point can be adjusted by buttons.
Dependencies: mbed DebounceIn TextLCD DS1820
main.cpp@0:af20639f9907, 2021-12-02 (annotated)
- Committer:
- mustafayassin
- Date:
- Thu Dec 02 11:43:48 2021 +0000
- Revision:
- 0:af20639f9907
- Child:
- 1:d6ceed323d25
Initial implementation with key denouncing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mustafayassin | 0:af20639f9907 | 1 | #include "mbed.h" |
mustafayassin | 0:af20639f9907 | 2 | #include "TextLCD.h" |
mustafayassin | 0:af20639f9907 | 3 | #include "DebounceIn.h" |
mustafayassin | 0:af20639f9907 | 4 | |
mustafayassin | 0:af20639f9907 | 5 | /* |
mustafayassin | 0:af20639f9907 | 6 | This basic example just shows how to read the ADC internal channels raw values. |
mustafayassin | 0:af20639f9907 | 7 | Please look in the corresponding device reference manual for a complete |
mustafayassin | 0:af20639f9907 | 8 | description of how to make a temperature sensor, VBat or Vref measurement. |
mustafayassin | 0:af20639f9907 | 9 | */ |
mustafayassin | 0:af20639f9907 | 10 | |
mustafayassin | 0:af20639f9907 | 11 | // Temperature input |
mustafayassin | 0:af20639f9907 | 12 | AnalogIn adc_temp1(A0); |
mustafayassin | 0:af20639f9907 | 13 | AnalogIn adc_temp2(A1); |
mustafayassin | 0:af20639f9907 | 14 | AnalogIn adc_temp3(A2); |
mustafayassin | 0:af20639f9907 | 15 | |
mustafayassin | 0:af20639f9907 | 16 | // Switch input |
mustafayassin | 0:af20639f9907 | 17 | DebounceIn tempUp(A3); |
mustafayassin | 0:af20639f9907 | 18 | DebounceIn tempDown(A4); |
mustafayassin | 0:af20639f9907 | 19 | DebounceIn onOff(A5); |
mustafayassin | 0:af20639f9907 | 20 | |
mustafayassin | 0:af20639f9907 | 21 | // LCD |
mustafayassin | 0:af20639f9907 | 22 | TextLCD lcd(D0, D1, D2, D3, D4, D5); // rs, e, d4-d7 |
mustafayassin | 0:af20639f9907 | 23 | |
mustafayassin | 0:af20639f9907 | 24 | // LED |
mustafayassin | 0:af20639f9907 | 25 | DigitalOut led(LED1); |
mustafayassin | 0:af20639f9907 | 26 | |
mustafayassin | 0:af20639f9907 | 27 | // PWM |
mustafayassin | 0:af20639f9907 | 28 | PwmOut fan(D12); |
mustafayassin | 0:af20639f9907 | 29 | |
mustafayassin | 0:af20639f9907 | 30 | uint8_t tempSetValue = 25; // degree |
mustafayassin | 0:af20639f9907 | 31 | bool fanOn = false; |
mustafayassin | 0:af20639f9907 | 32 | float fanDutyCycle = 0.0f; |
mustafayassin | 0:af20639f9907 | 33 | |
mustafayassin | 0:af20639f9907 | 34 | //const uint8_t PRESS_DURATION = 5; |
mustafayassin | 0:af20639f9907 | 35 | |
mustafayassin | 0:af20639f9907 | 36 | int main() |
mustafayassin | 0:af20639f9907 | 37 | { |
mustafayassin | 0:af20639f9907 | 38 | // Configure |
mustafayassin | 0:af20639f9907 | 39 | tempUp.mode(PullUp); |
mustafayassin | 0:af20639f9907 | 40 | tempDown.mode(PullUp); |
mustafayassin | 0:af20639f9907 | 41 | onOff.mode(PullUp); |
mustafayassin | 0:af20639f9907 | 42 | fan.period(4.0f); // period |
mustafayassin | 0:af20639f9907 | 43 | fan.write(fanDutyCycle); // duty cycle |
mustafayassin | 0:af20639f9907 | 44 | |
mustafayassin | 0:af20639f9907 | 45 | // Loop |
mustafayassin | 0:af20639f9907 | 46 | while(1) { |
mustafayassin | 0:af20639f9907 | 47 | float temp1 = (adc_temp1.read()*3.3f*100.0f); |
mustafayassin | 0:af20639f9907 | 48 | lcd.printf("Temp1 = %.1f\n", temp1); |
mustafayassin | 0:af20639f9907 | 49 | lcd.printf(""); |
mustafayassin | 0:af20639f9907 | 50 | printf("\033[2A"); |
mustafayassin | 0:af20639f9907 | 51 | |
mustafayassin | 0:af20639f9907 | 52 | if(tempUp == 0) tempSetValue++; |
mustafayassin | 0:af20639f9907 | 53 | if(tempDown == 0) tempSetValue--; |
mustafayassin | 0:af20639f9907 | 54 | if(onOff == 0) fanOn = !fanOn; |
mustafayassin | 0:af20639f9907 | 55 | //if(temp1 > tempSetValue && fanOn) { |
mustafayassin | 0:af20639f9907 | 56 | // fan.write(fanDutyCycle); |
mustafayassin | 0:af20639f9907 | 57 | //} else { |
mustafayassin | 0:af20639f9907 | 58 | // fan.write(0.0); |
mustafayassin | 0:af20639f9907 | 59 | //} |
mustafayassin | 0:af20639f9907 | 60 | led = !led; |
mustafayassin | 0:af20639f9907 | 61 | wait(0.020); |
mustafayassin | 0:af20639f9907 | 62 | } |
mustafayassin | 0:af20639f9907 | 63 | } |