Resident Community management
Dependencies: HCSR04 Servo mbed
main.cpp@1:054435f5db1c, 2018-09-20 (annotated)
- Committer:
- Albinarackal
- Date:
- Thu Sep 20 08:22:34 2018 +0000
- Revision:
- 1:054435f5db1c
- Parent:
- 0:2d3a72735265
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Albinarackal | 0:2d3a72735265 | 1 | #include "mbed.h" |
Albinarackal | 0:2d3a72735265 | 2 | #include "HCSR04.h" |
Albinarackal | 0:2d3a72735265 | 3 | #include "Servo.h" |
Albinarackal | 0:2d3a72735265 | 4 | |
Albinarackal | 0:2d3a72735265 | 5 | DigitalIn PIR(D4); |
Albinarackal | 1:054435f5db1c | 6 | DigitalOut LED(D10); |
Albinarackal | 0:2d3a72735265 | 7 | AnalogIn LDR(A0); |
Albinarackal | 0:2d3a72735265 | 8 | AnalogIn Gas(A1); |
Albinarackal | 1:054435f5db1c | 9 | InterruptIn button1(D8); |
Albinarackal | 1:054435f5db1c | 10 | InterruptIn button2(D6); |
Albinarackal | 0:2d3a72735265 | 11 | |
Albinarackal | 0:2d3a72735265 | 12 | |
Albinarackal | 0:2d3a72735265 | 13 | Serial pc(USBTX,USBRX); |
Albinarackal | 0:2d3a72735265 | 14 | Serial esp_serial(PA_13,PA_14); |
Albinarackal | 0:2d3a72735265 | 15 | |
Albinarackal | 0:2d3a72735265 | 16 | int val,ldr_val,gas_val; |
Albinarackal | 0:2d3a72735265 | 17 | int dist; |
Albinarackal | 0:2d3a72735265 | 18 | |
Albinarackal | 0:2d3a72735265 | 19 | HCSR04 ultrasonic(D9,D7); //trigger,echo |
Albinarackal | 0:2d3a72735265 | 20 | Servo servo(D14); |
Albinarackal | 0:2d3a72735265 | 21 | |
Albinarackal | 1:054435f5db1c | 22 | |
Albinarackal | 1:054435f5db1c | 23 | void open() |
Albinarackal | 1:054435f5db1c | 24 | { |
Albinarackal | 1:054435f5db1c | 25 | pc.printf("open"); |
Albinarackal | 1:054435f5db1c | 26 | servo.position(180); |
Albinarackal | 1:054435f5db1c | 27 | } |
Albinarackal | 1:054435f5db1c | 28 | |
Albinarackal | 1:054435f5db1c | 29 | |
Albinarackal | 1:054435f5db1c | 30 | void close() |
Albinarackal | 1:054435f5db1c | 31 | { |
Albinarackal | 1:054435f5db1c | 32 | pc.printf("close"); |
Albinarackal | 1:054435f5db1c | 33 | servo.position(0); |
Albinarackal | 1:054435f5db1c | 34 | } |
Albinarackal | 1:054435f5db1c | 35 | |
Albinarackal | 0:2d3a72735265 | 36 | int main() |
Albinarackal | 0:2d3a72735265 | 37 | { |
Albinarackal | 0:2d3a72735265 | 38 | pc.baud(9600); |
Albinarackal | 0:2d3a72735265 | 39 | esp_serial.baud(115200); |
Albinarackal | 0:2d3a72735265 | 40 | pc.printf("begin"); |
Albinarackal | 0:2d3a72735265 | 41 | |
Albinarackal | 1:054435f5db1c | 42 | button1.rise(&open); |
Albinarackal | 1:054435f5db1c | 43 | button2.rise(&close); |
Albinarackal | 1:054435f5db1c | 44 | |
Albinarackal | 0:2d3a72735265 | 45 | while(1) { |
Albinarackal | 0:2d3a72735265 | 46 | |
Albinarackal | 0:2d3a72735265 | 47 | val = PIR.read(); |
Albinarackal | 0:2d3a72735265 | 48 | if(val==1) { |
Albinarackal | 0:2d3a72735265 | 49 | |
Albinarackal | 0:2d3a72735265 | 50 | pc.printf("Motion Detected\n"); |
Albinarackal | 0:2d3a72735265 | 51 | dist = ultrasonic.distance(1); |
Albinarackal | 0:2d3a72735265 | 52 | pc.printf("the distance is %d cm\n",dist); |
Albinarackal | 0:2d3a72735265 | 53 | esp_serial.printf("U"); |
Albinarackal | 0:2d3a72735265 | 54 | esp_serial.printf("%d",dist); |
Albinarackal | 0:2d3a72735265 | 55 | esp_serial.putc('\0'); |
Albinarackal | 0:2d3a72735265 | 56 | |
Albinarackal | 1:054435f5db1c | 57 | if (dist>10.0) { |
Albinarackal | 0:2d3a72735265 | 58 | servo.position(180); |
Albinarackal | 0:2d3a72735265 | 59 | wait(5); |
Albinarackal | 0:2d3a72735265 | 60 | servo.position(0); |
Albinarackal | 0:2d3a72735265 | 61 | } |
Albinarackal | 0:2d3a72735265 | 62 | |
Albinarackal | 0:2d3a72735265 | 63 | } else { |
Albinarackal | 0:2d3a72735265 | 64 | pc.printf("Motion ended\n"); |
Albinarackal | 0:2d3a72735265 | 65 | |
Albinarackal | 0:2d3a72735265 | 66 | } |
Albinarackal | 0:2d3a72735265 | 67 | |
Albinarackal | 0:2d3a72735265 | 68 | |
Albinarackal | 0:2d3a72735265 | 69 | |
Albinarackal | 0:2d3a72735265 | 70 | ldr_val = LDR.read_u16(); |
Albinarackal | 0:2d3a72735265 | 71 | |
Albinarackal | 0:2d3a72735265 | 72 | pc.printf("ldr: %d\n",ldr_val); |
Albinarackal | 0:2d3a72735265 | 73 | esp_serial.printf("L"); |
Albinarackal | 0:2d3a72735265 | 74 | esp_serial.printf("%d",ldr_val); |
Albinarackal | 0:2d3a72735265 | 75 | esp_serial.putc('\0'); |
Albinarackal | 1:054435f5db1c | 76 | if(ldr_val <=300) |
Albinarackal | 1:054435f5db1c | 77 | LED = 1; |
Albinarackal | 1:054435f5db1c | 78 | |
Albinarackal | 1:054435f5db1c | 79 | else |
Albinarackal | 1:054435f5db1c | 80 | LED = 0; |
Albinarackal | 1:054435f5db1c | 81 | wait(2); |
Albinarackal | 0:2d3a72735265 | 82 | |
Albinarackal | 0:2d3a72735265 | 83 | |
Albinarackal | 0:2d3a72735265 | 84 | gas_val = Gas.read_u16(); |
Albinarackal | 0:2d3a72735265 | 85 | pc.printf("gas: %d\n",gas_val); |
Albinarackal | 0:2d3a72735265 | 86 | esp_serial.printf("G"); |
Albinarackal | 0:2d3a72735265 | 87 | esp_serial.printf("%d",gas_val); |
Albinarackal | 0:2d3a72735265 | 88 | esp_serial.putc('\0'); |
Albinarackal | 1:054435f5db1c | 89 | //wait(10); |
Albinarackal | 0:2d3a72735265 | 90 | } |
Albinarackal | 1:054435f5db1c | 91 | } |
Albinarackal | 1:054435f5db1c | 92 | |
Albinarackal | 1:054435f5db1c | 93 |