this program is compiled several times with various inputs ..... but at the last we got what we wanted... for any queries mail me at siva4cva@gmail.com use the ''tera term'' terminal for viewing output.
main.cpp@0:c63035081869, 2015-12-23 (annotated)
- Committer:
- shia
- Date:
- Wed Dec 23 10:16:49 2015 +0000
- Revision:
- 0:c63035081869
home automation using 7500; it includes ; current sensor ; PIR sensor; ADC channel reading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shia | 0:c63035081869 | 1 | #include "mbed.h" |
shia | 0:c63035081869 | 2 | DigitalIn PIR(D8); |
shia | 0:c63035081869 | 3 | DigitalIn light_sen(D7); |
shia | 0:c63035081869 | 4 | AnalogIn ADC_CHSEL(A0); |
shia | 0:c63035081869 | 5 | DigitalOut led(LED1); |
shia | 0:c63035081869 | 6 | unsigned int ADC_VALUE,Factor; |
shia | 0:c63035081869 | 7 | float current = 0; |
shia | 0:c63035081869 | 8 | int wattage; |
shia | 0:c63035081869 | 9 | float y,meas,a[10],avg; |
shia | 0:c63035081869 | 10 | int i,x; |
shia | 0:c63035081869 | 11 | int PIR_sensor; |
shia | 0:c63035081869 | 12 | int light; |
shia | 0:c63035081869 | 13 | |
shia | 0:c63035081869 | 14 | int main() { |
shia | 0:c63035081869 | 15 | |
shia | 0:c63035081869 | 16 | |
shia | 0:c63035081869 | 17 | printf("\n GSAS Micro Systems \n"); |
shia | 0:c63035081869 | 18 | |
shia | 0:c63035081869 | 19 | wait(0.1f); |
shia | 0:c63035081869 | 20 | |
shia | 0:c63035081869 | 21 | while(1) { |
shia | 0:c63035081869 | 22 | |
shia | 0:c63035081869 | 23 | meas = ADC_CHSEL.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
shia | 0:c63035081869 | 24 | |
shia | 0:c63035081869 | 25 | avg=0; |
shia | 0:c63035081869 | 26 | for(i=0;i<10;i++) |
shia | 0:c63035081869 | 27 | { |
shia | 0:c63035081869 | 28 | a[i]=meas; |
shia | 0:c63035081869 | 29 | wait(0.2); |
shia | 0:c63035081869 | 30 | } |
shia | 0:c63035081869 | 31 | meas=a[0]; |
shia | 0:c63035081869 | 32 | for(i=1;i<10;i++){ |
shia | 0:c63035081869 | 33 | if(meas<a[i]) |
shia | 0:c63035081869 | 34 | meas=a[i]; |
shia | 0:c63035081869 | 35 | } |
shia | 0:c63035081869 | 36 | |
shia | 0:c63035081869 | 37 | |
shia | 0:c63035081869 | 38 | printf(" actual value = %f\r\n", meas); |
shia | 0:c63035081869 | 39 | |
shia | 0:c63035081869 | 40 | x = meas*300; |
shia | 0:c63035081869 | 41 | avg=0; |
shia | 0:c63035081869 | 42 | for(i=0;i<10;i++) |
shia | 0:c63035081869 | 43 | { |
shia | 0:c63035081869 | 44 | a[i]=x; |
shia | 0:c63035081869 | 45 | wait(0.2); |
shia | 0:c63035081869 | 46 | } |
shia | 0:c63035081869 | 47 | for(i=0;i<10;i++) |
shia | 0:c63035081869 | 48 | { |
shia | 0:c63035081869 | 49 | avg=avg+(a[i]/10); |
shia | 0:c63035081869 | 50 | |
shia | 0:c63035081869 | 51 | } |
shia | 0:c63035081869 | 52 | |
shia | 0:c63035081869 | 53 | printf("initial read = %d\r\n", x); |
shia | 0:c63035081869 | 54 | |
shia | 0:c63035081869 | 55 | meas = x * 1.51; // Change the value to be in the 0 to 3300 range |
shia | 0:c63035081869 | 56 | avg=0; |
shia | 0:c63035081869 | 57 | for(i=0;i<10;i++) |
shia | 0:c63035081869 | 58 | { |
shia | 0:c63035081869 | 59 | a[i]=meas; |
shia | 0:c63035081869 | 60 | wait(0.2); |
shia | 0:c63035081869 | 61 | } |
shia | 0:c63035081869 | 62 | for(i=0;i<10;i++) |
shia | 0:c63035081869 | 63 | { |
shia | 0:c63035081869 | 64 | avg=avg+(a[i]/10); |
shia | 0:c63035081869 | 65 | |
shia | 0:c63035081869 | 66 | } |
shia | 0:c63035081869 | 67 | current = (meas - 329.18)/13.59; |
shia | 0:c63035081869 | 68 | // if (current < 0) |
shia | 0:c63035081869 | 69 | // { |
shia | 0:c63035081869 | 70 | // current = 0; |
shia | 0:c63035081869 | 71 | // } |
shia | 0:c63035081869 | 72 | wattage = current * 230; |
shia | 0:c63035081869 | 73 | printf("measure = %.2f mV\r\n", meas); |
shia | 0:c63035081869 | 74 | printf("current = %.2f A\r\n", current); |
shia | 0:c63035081869 | 75 | printf("wattage = %d W\r\n", wattage); |
shia | 0:c63035081869 | 76 | |
shia | 0:c63035081869 | 77 | printf("======================\r\n"); |
shia | 0:c63035081869 | 78 | int PIR_sensor; |
shia | 0:c63035081869 | 79 | |
shia | 0:c63035081869 | 80 | PIR_sensor = PIR; |
shia | 0:c63035081869 | 81 | //printf("PIR Value : %d\r\n",PIR_sensor); |
shia | 0:c63035081869 | 82 | |
shia | 0:c63035081869 | 83 | if (PIR_sensor==1) |
shia | 0:c63035081869 | 84 | { |
shia | 0:c63035081869 | 85 | printf("==================================== WELCOME ============\r\n"); |
shia | 0:c63035081869 | 86 | } |
shia | 0:c63035081869 | 87 | else |
shia | 0:c63035081869 | 88 | printf("=== room is empty ===\r\n"); |
shia | 0:c63035081869 | 89 | |
shia | 0:c63035081869 | 90 | int light; |
shia | 0:c63035081869 | 91 | light = light_sen; |
shia | 0:c63035081869 | 92 | if (light == 1) |
shia | 0:c63035081869 | 93 | { |
shia | 0:c63035081869 | 94 | printf("*********************************** light is ON ****************"); |
shia | 0:c63035081869 | 95 | } |
shia | 0:c63035081869 | 96 | else |
shia | 0:c63035081869 | 97 | printf("################################### light is OFF ################"); |
shia | 0:c63035081869 | 98 | |
shia | 0:c63035081869 | 99 | |
shia | 0:c63035081869 | 100 | |
shia | 0:c63035081869 | 101 | |
shia | 0:c63035081869 | 102 | wait(0.1); // 200 ms |
shia | 0:c63035081869 | 103 | } |
shia | 0:c63035081869 | 104 | } |