Control fan speed by temperature sensor reading. Set point can be adjusted by buttons.
Dependencies: mbed DebounceIn TextLCD DS1820
main.cpp@2:d301da531767, 2021-12-09 (annotated)
- Committer:
- mustafayassin
- Date:
- Thu Dec 09 02:18:09 2021 +0000
- Revision:
- 2:d301da531767
- Parent:
- 1:d6ceed323d25
Used DS1820 temperature sensor.
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 | 1:d6ceed323d25 | 4 | #include "DS1820.h" |
mustafayassin | 0:af20639f9907 | 5 | |
mustafayassin | 0:af20639f9907 | 6 | /* |
mustafayassin | 0:af20639f9907 | 7 | This basic example just shows how to read the ADC internal channels raw values. |
mustafayassin | 0:af20639f9907 | 8 | Please look in the corresponding device reference manual for a complete |
mustafayassin | 0:af20639f9907 | 9 | description of how to make a temperature sensor, VBat or Vref measurement. |
mustafayassin | 0:af20639f9907 | 10 | */ |
mustafayassin | 0:af20639f9907 | 11 | |
mustafayassin | 0:af20639f9907 | 12 | // Temperature input |
mustafayassin | 1:d6ceed323d25 | 13 | //AnalogIn adc_temp1(A0); |
mustafayassin | 0:af20639f9907 | 14 | AnalogIn adc_temp2(A1); |
mustafayassin | 0:af20639f9907 | 15 | AnalogIn adc_temp3(A2); |
mustafayassin | 0:af20639f9907 | 16 | |
mustafayassin | 0:af20639f9907 | 17 | // Switch input |
mustafayassin | 0:af20639f9907 | 18 | DebounceIn tempUp(A3); |
mustafayassin | 0:af20639f9907 | 19 | DebounceIn tempDown(A4); |
mustafayassin | 0:af20639f9907 | 20 | DebounceIn onOff(A5); |
mustafayassin | 0:af20639f9907 | 21 | |
mustafayassin | 0:af20639f9907 | 22 | // LCD |
mustafayassin | 0:af20639f9907 | 23 | TextLCD lcd(D0, D1, D2, D3, D4, D5); // rs, e, d4-d7 |
mustafayassin | 0:af20639f9907 | 24 | |
mustafayassin | 0:af20639f9907 | 25 | // LED |
mustafayassin | 0:af20639f9907 | 26 | DigitalOut led(LED1); |
mustafayassin | 0:af20639f9907 | 27 | |
mustafayassin | 0:af20639f9907 | 28 | // PWM |
mustafayassin | 0:af20639f9907 | 29 | PwmOut fan(D12); |
mustafayassin | 0:af20639f9907 | 30 | |
mustafayassin | 1:d6ceed323d25 | 31 | // Temp |
mustafayassin | 1:d6ceed323d25 | 32 | DS1820 ds1820(A0); |
mustafayassin | 1:d6ceed323d25 | 33 | |
mustafayassin | 0:af20639f9907 | 34 | uint8_t tempSetValue = 25; // degree |
mustafayassin | 0:af20639f9907 | 35 | bool fanOn = false; |
mustafayassin | 0:af20639f9907 | 36 | float fanDutyCycle = 0.0f; |
mustafayassin | 0:af20639f9907 | 37 | |
mustafayassin | 1:d6ceed323d25 | 38 | uint8_t ds1820Counter = 0; |
mustafayassin | 1:d6ceed323d25 | 39 | uint8_t temp1 = 0; |
mustafayassin | 1:d6ceed323d25 | 40 | |
mustafayassin | 0:af20639f9907 | 41 | //const uint8_t PRESS_DURATION = 5; |
mustafayassin | 0:af20639f9907 | 42 | |
mustafayassin | 0:af20639f9907 | 43 | int main() |
mustafayassin | 0:af20639f9907 | 44 | { |
mustafayassin | 1:d6ceed323d25 | 45 | // Configure pull-up |
mustafayassin | 0:af20639f9907 | 46 | tempUp.mode(PullUp); |
mustafayassin | 0:af20639f9907 | 47 | tempDown.mode(PullUp); |
mustafayassin | 0:af20639f9907 | 48 | onOff.mode(PullUp); |
mustafayassin | 1:d6ceed323d25 | 49 | // 100 ms debounce |
mustafayassin | 1:d6ceed323d25 | 50 | tempUp.set_samples(100); |
mustafayassin | 1:d6ceed323d25 | 51 | tempDown.set_samples(100); |
mustafayassin | 1:d6ceed323d25 | 52 | onOff.set_samples(100); |
mustafayassin | 1:d6ceed323d25 | 53 | |
mustafayassin | 0:af20639f9907 | 54 | fan.period(4.0f); // period |
mustafayassin | 0:af20639f9907 | 55 | fan.write(fanDutyCycle); // duty cycle |
mustafayassin | 0:af20639f9907 | 56 | |
mustafayassin | 1:d6ceed323d25 | 57 | if (ds1820.begin()) lcd.printf("DS1820 found!"); |
mustafayassin | 1:d6ceed323d25 | 58 | else lcd.printf("DS1820 not found"); |
mustafayassin | 1:d6ceed323d25 | 59 | wait(1.0); |
mustafayassin | 1:d6ceed323d25 | 60 | lcd.cls(); |
mustafayassin | 0:af20639f9907 | 61 | // Loop |
mustafayassin | 0:af20639f9907 | 62 | while(1) { |
mustafayassin | 1:d6ceed323d25 | 63 | //uint8_t temp1 = static_cast<uint8_t>(adc_temp1.read()*3.3f*100.0f); |
mustafayassin | 1:d6ceed323d25 | 64 | |
mustafayassin | 1:d6ceed323d25 | 65 | if (ds1820Counter == 0) ds1820.startConversion(); |
mustafayassin | 1:d6ceed323d25 | 66 | if(ds1820Counter++ == 50) { |
mustafayassin | 1:d6ceed323d25 | 67 | float ds1820Temp = 0.0f; |
mustafayassin | 1:d6ceed323d25 | 68 | if(ds1820.read(ds1820Temp) == 0) { |
mustafayassin | 1:d6ceed323d25 | 69 | temp1 = static_cast<uint8_t>(ds1820Temp); |
mustafayassin | 1:d6ceed323d25 | 70 | } |
mustafayassin | 1:d6ceed323d25 | 71 | ds1820Counter = 0; |
mustafayassin | 1:d6ceed323d25 | 72 | } |
mustafayassin | 1:d6ceed323d25 | 73 | |
mustafayassin | 1:d6ceed323d25 | 74 | uint8_t temp2 = static_cast<uint8_t>(adc_temp2.read()*3.3f*100.0f); |
mustafayassin | 1:d6ceed323d25 | 75 | uint8_t temp3 = static_cast<uint8_t>(adc_temp3.read()*3.3f*100.0f); |
mustafayassin | 1:d6ceed323d25 | 76 | |
mustafayassin | 1:d6ceed323d25 | 77 | lcd.printf("T1=%02d, T2=%02d\n", temp1, temp2); |
mustafayassin | 1:d6ceed323d25 | 78 | lcd.printf("T3=%02d, SP=%02d\n", temp3, tempSetValue); |
mustafayassin | 1:d6ceed323d25 | 79 | |
mustafayassin | 0:af20639f9907 | 80 | |
mustafayassin | 0:af20639f9907 | 81 | if(tempUp == 0) tempSetValue++; |
mustafayassin | 0:af20639f9907 | 82 | if(tempDown == 0) tempSetValue--; |
mustafayassin | 0:af20639f9907 | 83 | if(onOff == 0) fanOn = !fanOn; |
mustafayassin | 0:af20639f9907 | 84 | //if(temp1 > tempSetValue && fanOn) { |
mustafayassin | 0:af20639f9907 | 85 | // fan.write(fanDutyCycle); |
mustafayassin | 0:af20639f9907 | 86 | //} else { |
mustafayassin | 0:af20639f9907 | 87 | // fan.write(0.0); |
mustafayassin | 0:af20639f9907 | 88 | //} |
mustafayassin | 0:af20639f9907 | 89 | led = !led; |
mustafayassin | 0:af20639f9907 | 90 | wait(0.020); |
mustafayassin | 0:af20639f9907 | 91 | } |
mustafayassin | 0:af20639f9907 | 92 | } |