![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
tokuni
Diff: main.cpp
- Revision:
- 0:c168443703fb
- Child:
- 1:b647fdc5a465
diff -r 000000000000 -r c168443703fb main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 23 06:16:39 2014 +0000 @@ -0,0 +1,111 @@ +#include "mbed.h" +Serial pc(USBTX, USBRX); +AnalogIn tempPin1(p15); +AnalogIn tempPin2(p16); +AnalogIn tempPin3(p17); +AnalogIn tempPin4(p18); +PwmOut pwm1(p21); +PwmOut pwm2(p22); +#define start 500 +#define end 2000 +float tempTotal1; +float tempTotal2; +float tempTotal3; +float tempTotal4; +uint32_t tempDiv1; +uint32_t tempDiv2; +uint32_t tempDiv3; +uint32_t tempDiv4; +#define MAX_TEMP_AVG 10 +void sampleTemp(); +void sampleTemps(); +void resetTempSamples(); +float getAvgTemp(); + +void sampleTemp() +{ + tempTotal1 += tempPin1; + tempDiv1++; + tempTotal2 += tempPin2; + tempDiv2++; + tempTotal3 += tempPin3; + tempDiv3++; + tempTotal4 += tempPin4; + tempDiv4++; +} +void sampleTemps() +{ + for(int i=0;i<MAX_TEMP_AVG;i++) { + sampleTemp(); + wait(0.1); + } +} +void resetTempSamples() +{ + tempTotal1 = 0.0; + tempDiv1 = 0; + tempTotal2 = 0.0; + tempDiv2 = 0; + tempTotal3 = 0.0; + tempDiv3 = 0; + tempTotal4 = 0.0; + tempDiv4 = 0; +} + +float getAvgTemp1() +{ + resetTempSamples(); + sampleTemps(); + float tempAvg1 = (((tempTotal1)*3300/(float)tempDiv1)-400.0)/19.5; + return tempAvg1; +} +float getAvgTemp2() +{ + resetTempSamples(); + sampleTemps(); + float tempAvg2 = (((tempTotal2)*3300/(float)tempDiv2)-400.0)/19.5; + return tempAvg2; +} +float getAvgTemp3() +{ + resetTempSamples(); + sampleTemps(); + float tempAvg3 = (((tempTotal3)*3300/(float)tempDiv3)-400.0)/19.5; + return tempAvg3; +} +float getAvgTemp4() +{ + resetTempSamples(); + sampleTemps(); + float tempAvg4 = (((tempTotal4)*3300/(float)tempDiv4)-400.0)/19.5; + return tempAvg4; +} +int main() { + + tempDiv1 = 0; + tempDiv2 = 0; + tempDiv3 = 0; + tempDiv4 = 0; + int a,b; + pc.printf("MBED ready..\r\n"); + sampleTemps(); + while(1){ + sampleTemp(); + float k=getAvgTemp1()-getAvgTemp2(); + float i=getAvgTemp3()-getAvgTemp4(); + pc.printf("Temp1:%f\nTemp2:%f\n",getAvgTemp1(),getAvgTemp2()); + pc.printf("Temp3:%f\nTemp4:%f\n",getAvgTemp3(),getAvgTemp4()); + a = k * 2; + b = i * 2; + { + for(int p = start; p < end; p += a) + pwm1.pulsewidth_us(p); + wait_ms(10); + } + { + for(int q = start; q < end; q += b) + pwm2.pulsewidth_us(q); + wait_ms(10); + } +} +} \ No newline at end of file