Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-os FXAS21000 LED_Bar FXOS8700Q
main.cpp
00001 #include "mbed.h" 00002 #include "FXOS8700Q.h" 00003 #include "FXAS21000.h" 00004 #include "hcsr04.h" 00005 #include "LED_Bar.h" 00006 I2C i2c(PTE25, PTE24); 00007 Serial pc(USBTX,USBRX); 00008 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors 00009 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1); 00010 FXAS21000 gyro(D14,D15); 00011 HCSR04 usensor1(D8,D9); //ECHO Pin=D9, TRIG Pin=D8 00012 HCSR04 usensor2(D7,D6); //ECHO Pin=D7, TRIG Pin=D6 00013 LED_Bar bar(D5,D4); 00014 DigitalOut buzzer(D2); 00015 int buzz_on=1, buzz_off=0; 00016 00017 int main(void) 00018 { 00019 int num = 0; 00020 int distance1, distance2; 00021 int led_speed; 00022 float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2; 00023 char snd[255],rcv[1000]; //snd: send command to ESP8266 00024 //rcv: receive response from ESP8266 00025 00026 //Ultrasound Sensor (HC-SR04) #1 Initialization 00027 int a = 30; 00028 float gyro_data[3]; 00029 motion_data_units_t acc_data, mag_data; 00030 motion_data_counts_t acc_raw, mag_raw; 00031 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float; 00032 int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int; 00033 acc.enable(); 00034 mag.enable(); 00035 //DigitalOut Red(LED1); 00036 // Red=1; 00037 // DigitalOut Blue(LED3); 00038 // DigitalOut Green(LED2); 00039 // Blue=1; 00040 // Green=1; 00041 00042 while (true) { 00043 // counts based results 00044 acc.getAxis(acc_raw); 00045 mag.getAxis(mag_raw); 00046 acc.getX(raX); 00047 acc.getY(raY); 00048 acc.getZ(raZ); 00049 mag.getX(rmX); 00050 mag.getY(rmY); 00051 mag.getZ(rmZ); 00052 // unit based results 00053 acc.getAxis(acc_data); 00054 mag.getAxis(mag_data); 00055 acc.getX(faX); 00056 acc.getY(faY); 00057 acc.getZ(faZ); 00058 mag.getX(fmX); 00059 mag.getY(fmY); 00060 mag.getZ(fmZ); 00061 pc.printf("FXOSQ8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f", acc.getX(faX),acc.getY(faY),acc.getZ(faZ)); 00062 pc.printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag.getX(fmX),mag.getY(fmY), mag.getZ(fmZ)); 00063 gyro.ReadXYZ(gyro_data); 00064 pc.printf("FXAS21000 Gyro: X=%6.2f Y=%6.2f Z=%6.2f\r\n", gyro_data[0],gyro_data[1], gyro_data[2]); 00065 00066 00067 usensor1.start(); 00068 wait_ms(500); 00069 00070 //Calculating Distance Percentage Remaining for Sensor # 1 00071 distance1 = usensor1.get_dist_cm(); 00072 dist_remaining1 = a-distance1; 00073 dist_percent1 = (dist_remaining1/30)*100; 00074 00075 ////LED and Tera Term Output 00076 // if (distance1<30 && distance2<30) { 00077 // 00078 // pc.printf("You are colse!\n\r"); while(1) { 00079 // pc.putc(pc.getc() + 1);} 00080 // //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2); 00081 // } else { 00082 // //GLed = 1; 00083 //// BLed = 1; 00084 //// RLed = 0; 00085 // pc.printf("You are far!\n\r"); while(1) { 00086 // pc.putc(pc.getc() + 1);} 00087 // } 00088 00089 if((abs(acc.getX(faX)) > 1.5 || abs(acc.getZ(faZ)) > 1.3 || abs(acc.getY(faY)) > 0.1) || (distance1<30 && distance2<30)) 00090 { 00091 00092 //Red = 0; 00093 // Blue = 1; 00094 // Green = 1; 00095 00096 // pc.printf("Going too fast!\n\r"); //while(1) { 00097 // pc.putc(pc.getc() + 1);} 00098 00099 buzzer=buzz_on; 00100 wait(0.5); 00101 buzzer=buzz_off; 00102 wait(0.5); 00103 00104 for(led_speed=0; led_speed<=10; led_speed++){ 00105 bar.setLevel(led_speed); 00106 wait(0.001); 00107 } 00108 00109 } 00110 00111 00112 else{ 00113 00114 //Red = 1; 00115 // Green = 0; 00116 // Blue = 1; 00117 00118 // pc.printf("You are safe!\n\r"); while(1) { 00119 // pc.putc(pc.getc() + 1);} 00120 00121 for(led_speed=0; led_speed<=10; led_speed++){ 00122 bar.setLevel(led_speed); 00123 wait(0.2); 00124 } 00125 00126 }; 00127 wait(1.0f); 00128 } 00129 }
Generated on Wed Jul 13 2022 21:34:24 by
1.7.2