smart-home
/
smart-home-final-version
Diff: main.cpp
- Revision:
- 2:6acc2f4efd39
- Parent:
- 0:3e6bb88dd7ee
- Child:
- 3:abe0c74fd74d
--- a/main.cpp Sat Sep 29 22:47:56 2018 +0000 +++ b/main.cpp Sun Sep 30 17:49:50 2018 +0000 @@ -3,7 +3,7 @@ void settemp(); void tempmenu(); -float temp_cal(); +float getTemp(); AnalogIn temp_sen(A0); TextLCD lcd(PTC7,PTC0,PTC3,PTC4,PTC5,PTC6); InterruptIn button(D0); @@ -14,7 +14,7 @@ //DigitalOut myled(A); int temp = 25; -float tempC,tempF,a[10],avg; +//float tempC, tempF, tempSamp[10]; int main() { tempmenu(); @@ -27,71 +27,68 @@ while(1){ // temp up - if(push2 == 0 && push1 == 1){ + if(push2 == 0){ temp++; - lcd.cls(); lcd.printf("enter temp: %d",temp); - wait(0.2); } // temp down - if(push1 == 0 && push2 == 1){ + if(push1 == 0){ temp--; - lcd.cls(); lcd.printf("enter temp: %d",temp); - wait(0.2); } // confirm if(push3 == 0){ -// tempmenu(); break; } - + lcd.cls(); + wait(0.2); } } void tempmenu() { - while(1){ + + // interrupt + button.fall(&settemp); + while(1) { lcd.cls(); + mypwm.period_ms(10); // display menu - mypwm.period_ms(10); - lcd.printf("T:%.2f \n",temp_cal()); + lcd.printf("T:%.2f \n", getTemp()); lcd.printf("1 to mod temp"); wait(0.2); - if(temp_cal()<temp){ - wait(0.5); - mypwm.pulsewidth_ms(8); - } - if(temp_cal()>=temp){ - mypwm.pulsewidth_ms(2); - } - - //else{ - //myled =0;} - // interrupt - button.fall(&settemp); - lcd.printf("end"); + if(getTemp() < temp){ + // if current temp is below temp set + wait(0.5); + mypwm.pulsewidth_ms(8); + } else { + mypwm.pulsewidth_ms(2); + } } } -float temp_cal(){ - avg=0; - int i; -for(i=0;i<10;i++) -{ -a[i]=temp_sen.read(); -wait(.05); -} -for(i=0;i<10;i++) -{ -avg=avg+(a[i]/10); +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; } -tempC=(avg*3.685503686*100); -tempF=(9.0*tempC)/5.0 + 32.0; -return tempC; - } - -