smart-home
/
smart-home-final-version
main.cpp
- Committer:
- osamugint
- Date:
- 2018-10-08
- Revision:
- 3:abe0c74fd74d
- Parent:
- 2:6acc2f4efd39
File content as of revision 3:abe0c74fd74d:
#include "mbed.h" #include "TextLCD.h" void settemp(); void tempmenu(); float getTemp(); void count(); void light(); AnalogIn temp_sen(A0); TextLCD lcd(PTC7,PTC0,PTC3,PTC4,PTC5,PTC6); InterruptIn button(D0); InterruptIn rising_edge(A4); DigitalIn push2(D1); DigitalIn push1(D2); DigitalIn push3(D3); PwmOut mypwm(A5); Timer tm1; //DigitalOut myled(A); int counter = 0; int temp = 25; //float tempC, tempF, tempSamp[10]; int main() { //tempmenu(); int duration = 0; while(1){ if (duration == 0){ tm1.start(); } duration = tm1.read_ms(); light(); if (duration == 5000){ lcd.printf("counter: %i",counter); tm1.reset(); duration = 0; } } } void settemp(){ lcd.cls(); lcd.printf("enter temp: %d",temp); wait(0.2); while(1){ // temp up if(push2 == 0){ temp++; lcd.printf("enter temp: %d",temp); } // temp down if(push1 == 0){ temp--; lcd.printf("enter temp: %d",temp); } // confirm if(push3 == 0){ break; } lcd.cls(); wait(0.2); } } void tempmenu() { // interrupt button.fall(&settemp); while(1) { lcd.cls(); mypwm.period_ms(10); // display menu lcd.printf("T:%.2f \n", getTemp()); lcd.printf("1 to mod temp"); wait(0.2); if(getTemp() < temp){ // if current temp is below temp set wait(0.5); mypwm.pulsewidth_ms(8); } else { mypwm.pulsewidth_ms(2); } } } float getTemp() { float avg = 0.0; float tempC, tempF, tempSamp[10]; int i; // read temp from sensor for(i = 0; i < 10; i++) { tempSamp[i] = temp_sen.read(); wait(.05); } // calc average temp for(i = 0; i < 10; i++){ avg = avg + (tempSamp[i] / 10); } // calc result tempC = (avg * 3.685503686 * 100); tempF = (9.0 * tempC) / 5.0 + 32.0; return tempC; } void light(){ rising_edge.rise(&count); } void count(){ counter++; }