dad

Dependencies:   mbed-os FXAS21000 LED_Bar FXOS8700Q

Committer:
mukundy8
Date:
Wed Apr 27 00:22:09 2022 +0000
Revision:
4:91893b37450e
Parent:
3:9ab65e6d4cd4
final;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
priyank12p 0:838685d68d89 1 #include "mbed.h"
priyank12p 0:838685d68d89 2 #include "FXOS8700Q.h"
priyank12p 0:838685d68d89 3 #include "FXAS21000.h"
mukundy8 2:d08625231a9f 4 #include "hcsr04.h"
maeconn 3:9ab65e6d4cd4 5 #include "LED_Bar.h"
mukundy8 4:91893b37450e 6 #include <stdio.h>
mukundy8 4:91893b37450e 7 #include <string.h>
mukundy8 4:91893b37450e 8 //Colours for printf
mukundy8 4:91893b37450e 9 #define cyan "\033[0;36m" /* 0 -> normal; 36 -> cyan */
mukundy8 4:91893b37450e 10 #define green "\033[4;32m" /*4 -> underline ; 32 -> green */
mukundy8 4:91893b37450e 11 #define blue "\033[9;34m" /*9 -> strike ; 34 -> blue */
mukundy8 4:91893b37450e 12 #define KWHT "\x1B[37m"
mukundy8 4:91893b37450e 13 #define none "\033[0m" /* to flush the previous property */
mukundy8 4:91893b37450e 14
mukundy8 4:91893b37450e 15 //IO declarations
mukundy8 4:91893b37450e 16 Serial pc(USBTX, USBRX); //PC UART
mukundy8 4:91893b37450e 17 //DigitalOut led1(LED1);
mukundy8 4:91893b37450e 18 DigitalOut GREEN(LED2); //Input Mode
mukundy8 4:91893b37450e 19 DigitalOut BLUE(LED3); //output mode
mukundy8 4:91893b37450e 20 DigitalIn sw2(SW2); //INPUT MODE SELECT
mukundy8 4:91893b37450e 21 DigitalIn sw3(SW3); //Output mode select
mukundy8 4:91893b37450e 22 //Bluetooth module declaration
mukundy8 4:91893b37450e 23 Serial HC06(PTC15, PTC14); //BT TX, RX
mukundy8 4:91893b37450e 24 char snd[512], rcv[1000]; //send and receive buffer
mukundy8 4:91893b37450e 25
priyank12p 0:838685d68d89 26 I2C i2c(PTE25, PTE24);
priyank12p 0:838685d68d89 27 Serial pc(USBTX,USBRX);
priyank12p 0:838685d68d89 28 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors
priyank12p 0:838685d68d89 29 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
priyank12p 0:838685d68d89 30 FXAS21000 gyro(D14,D15);
mukundy8 2:d08625231a9f 31 HCSR04 usensor1(D8,D9); //ECHO Pin=D9, TRIG Pin=D8
mukundy8 2:d08625231a9f 32 HCSR04 usensor2(D7,D6); //ECHO Pin=D7, TRIG Pin=D6
maeconn 3:9ab65e6d4cd4 33 LED_Bar bar(D5,D4);
maeconn 3:9ab65e6d4cd4 34 DigitalOut buzzer(D2);
maeconn 3:9ab65e6d4cd4 35 int buzz_on=1, buzz_off=0;
mukundy8 2:d08625231a9f 36
priyank12p 0:838685d68d89 37 int main(void)
priyank12p 0:838685d68d89 38 {
mukundy8 2:d08625231a9f 39 int num = 0;
mukundy8 2:d08625231a9f 40 int distance1, distance2;
maeconn 3:9ab65e6d4cd4 41 int led_speed;
mukundy8 2:d08625231a9f 42 float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2;
mukundy8 2:d08625231a9f 43 char snd[255],rcv[1000]; //snd: send command to ESP8266
mukundy8 2:d08625231a9f 44 //rcv: receive response from ESP8266
mukundy8 2:d08625231a9f 45
mukundy8 2:d08625231a9f 46 //Ultrasound Sensor (HC-SR04) #1 Initialization
mukundy8 2:d08625231a9f 47 int a = 30;
priyank12p 0:838685d68d89 48 float gyro_data[3];
priyank12p 0:838685d68d89 49 motion_data_units_t acc_data, mag_data;
priyank12p 0:838685d68d89 50 motion_data_counts_t acc_raw, mag_raw;
priyank12p 0:838685d68d89 51 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
priyank12p 0:838685d68d89 52 int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int;
priyank12p 0:838685d68d89 53 acc.enable();
priyank12p 0:838685d68d89 54 mag.enable();
mukundy8 4:91893b37450e 55 usensor1.start();
mukundy8 4:91893b37450e 56 wait_ms(500);
maeconn 3:9ab65e6d4cd4 57 //DigitalOut Red(LED1);
maeconn 3:9ab65e6d4cd4 58 // Red=1;
maeconn 3:9ab65e6d4cd4 59 // DigitalOut Blue(LED3);
maeconn 3:9ab65e6d4cd4 60 // DigitalOut Green(LED2);
maeconn 3:9ab65e6d4cd4 61 // Blue=1;
maeconn 3:9ab65e6d4cd4 62 // Green=1;
mukundy8 1:7a49fd0692fd 63
priyank12p 0:838685d68d89 64 while (true) {
priyank12p 0:838685d68d89 65 // counts based results
priyank12p 0:838685d68d89 66 acc.getAxis(acc_raw);
priyank12p 0:838685d68d89 67 mag.getAxis(mag_raw);
priyank12p 0:838685d68d89 68 acc.getX(raX);
priyank12p 0:838685d68d89 69 acc.getY(raY);
priyank12p 0:838685d68d89 70 acc.getZ(raZ);
priyank12p 0:838685d68d89 71 mag.getX(rmX);
priyank12p 0:838685d68d89 72 mag.getY(rmY);
priyank12p 0:838685d68d89 73 mag.getZ(rmZ);
priyank12p 0:838685d68d89 74 // unit based results
priyank12p 0:838685d68d89 75 acc.getAxis(acc_data);
priyank12p 0:838685d68d89 76 mag.getAxis(mag_data);
priyank12p 0:838685d68d89 77 acc.getX(faX);
priyank12p 0:838685d68d89 78 acc.getY(faY);
priyank12p 0:838685d68d89 79 acc.getZ(faZ);
priyank12p 0:838685d68d89 80 mag.getX(fmX);
priyank12p 0:838685d68d89 81 mag.getY(fmY);
priyank12p 0:838685d68d89 82 mag.getZ(fmZ);
mukundy8 1:7a49fd0692fd 83 pc.printf("FXOSQ8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f", acc.getX(faX),acc.getY(faY),acc.getZ(faZ));
mukundy8 1:7a49fd0692fd 84 pc.printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag.getX(fmX),mag.getY(fmY), mag.getZ(fmZ));
priyank12p 0:838685d68d89 85 gyro.ReadXYZ(gyro_data);
mukundy8 1:7a49fd0692fd 86 pc.printf("FXAS21000 Gyro: X=%6.2f Y=%6.2f Z=%6.2f\r\n", gyro_data[0],gyro_data[1], gyro_data[2]);
mukundy8 1:7a49fd0692fd 87
maeconn 3:9ab65e6d4cd4 88
mukundy8 4:91893b37450e 89
maeconn 3:9ab65e6d4cd4 90
maeconn 3:9ab65e6d4cd4 91 //Calculating Distance Percentage Remaining for Sensor # 1
maeconn 3:9ab65e6d4cd4 92 distance1 = usensor1.get_dist_cm();
maeconn 3:9ab65e6d4cd4 93 dist_remaining1 = a-distance1;
maeconn 3:9ab65e6d4cd4 94 dist_percent1 = (dist_remaining1/30)*100;
mukundy8 4:91893b37450e 95 //
maeconn 3:9ab65e6d4cd4 96 ////LED and Tera Term Output
mukundy8 4:91893b37450e 97 // if (distance1<30 && distance2<30) {
mukundy8 4:91893b37450e 98 ////
mukundy8 4:91893b37450e 99 // pc.printf("You are colse!\n\r");}
maeconn 3:9ab65e6d4cd4 100 // pc.putc(pc.getc() + 1);}
maeconn 3:9ab65e6d4cd4 101 // //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2);
maeconn 3:9ab65e6d4cd4 102 // } else {
maeconn 3:9ab65e6d4cd4 103 // //GLed = 1;
maeconn 3:9ab65e6d4cd4 104 //// BLed = 1;
maeconn 3:9ab65e6d4cd4 105 //// RLed = 0;
maeconn 3:9ab65e6d4cd4 106 // pc.printf("You are far!\n\r"); while(1) {
maeconn 3:9ab65e6d4cd4 107 // pc.putc(pc.getc() + 1);}
maeconn 3:9ab65e6d4cd4 108 // }
mukundy8 1:7a49fd0692fd 109
mukundy8 4:91893b37450e 110 if((abs(acc.getX(faX)) > 1.5 || abs(acc.getZ(faZ)) > 1.3 || abs(acc.getY(faY)) > 0.1) || (distance1>30 && distance2>30))
mukundy8 1:7a49fd0692fd 111 {
mukundy8 1:7a49fd0692fd 112
mukundy8 2:d08625231a9f 113 //Red = 0;
mukundy8 2:d08625231a9f 114 // Blue = 1;
mukundy8 2:d08625231a9f 115 // Green = 1;
maeconn 3:9ab65e6d4cd4 116
maeconn 3:9ab65e6d4cd4 117 // pc.printf("Going too fast!\n\r"); //while(1) {
maeconn 3:9ab65e6d4cd4 118 // pc.putc(pc.getc() + 1);}
maeconn 3:9ab65e6d4cd4 119
maeconn 3:9ab65e6d4cd4 120 buzzer=buzz_on;
maeconn 3:9ab65e6d4cd4 121 wait(0.5);
maeconn 3:9ab65e6d4cd4 122 buzzer=buzz_off;
maeconn 3:9ab65e6d4cd4 123 wait(0.5);
maeconn 3:9ab65e6d4cd4 124
maeconn 3:9ab65e6d4cd4 125 for(led_speed=0; led_speed<=10; led_speed++){
maeconn 3:9ab65e6d4cd4 126 bar.setLevel(led_speed);
maeconn 3:9ab65e6d4cd4 127 wait(0.001);
maeconn 3:9ab65e6d4cd4 128 }
mukundy8 4:91893b37450e 129
mukundy8 4:91893b37450e 130 HC06.printf("\nWe are in danger, sending sms now\n");
mukundy8 4:91893b37450e 131 \
mukundy8 1:7a49fd0692fd 132
mukundy8 1:7a49fd0692fd 133 }
mukundy8 1:7a49fd0692fd 134
mukundy8 1:7a49fd0692fd 135
mukundy8 1:7a49fd0692fd 136 else{
mukundy8 1:7a49fd0692fd 137
mukundy8 2:d08625231a9f 138 //Red = 1;
mukundy8 2:d08625231a9f 139 // Green = 0;
mukundy8 2:d08625231a9f 140 // Blue = 1;
maeconn 3:9ab65e6d4cd4 141
mukundy8 4:91893b37450e 142 // pc.printf("You are safe!\n\r"); //while(1) {
maeconn 3:9ab65e6d4cd4 143 // pc.putc(pc.getc() + 1);}
maeconn 3:9ab65e6d4cd4 144
maeconn 3:9ab65e6d4cd4 145 for(led_speed=0; led_speed<=10; led_speed++){
maeconn 3:9ab65e6d4cd4 146 bar.setLevel(led_speed);
mukundy8 4:91893b37450e 147 wait(1);
maeconn 3:9ab65e6d4cd4 148 }
maeconn 3:9ab65e6d4cd4 149
mukundy8 1:7a49fd0692fd 150 };
priyank12p 0:838685d68d89 151 wait(1.0f);
priyank12p 0:838685d68d89 152 }
priyank12p 0:838685d68d89 153 }