Seth Forslund / Mbed 2 deprecated ECE595_VehicleBrakingsystem

Dependencies:   mbed mbed-http ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 Electronically Controlled Intelligent Shelves
00003 Developed by: Priyank Kalgaonkar
00004 **/
00005 
00006 #include "mbed.h"
00007 #include "hcsr04.h"
00008 #include "math.h"
00009 #include "RGBLED.h"
00010 
00011 //Setup RGB led using PWM pins and class
00012 RGBLed myRGBled(D6,D5,D3); //RGB PWM pins
00013 
00014 DigitalIn sw2(SW2);
00015 DigitalOut RLed(LED1);                      //Onboard Red LED = Shelf Out of Stock
00016 DigitalOut GLed(LED2);                      //Onboard Green LED = All OK
00017 DigitalOut BLed(LED3);                      //Onboard Blue LED for Wifi Tx Indication
00018 HCSR04 usensor1(D8,D9);                     //ECHO Pin=D9, TRIG Pin=D8
00019 Serial pc(USBTX,USBRX);                     //Serial Communication with PC
00020 
00021 
00022 void wifi_send(void);;                      //Connect and Push Data Channel to Cloud Server
00023 
00024 int num = 0;
00025 int distance1, distance2;
00026 float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2;
00027 char snd[255],rcv[1000];                    //snd: send command to ESP8266
00028                                             //rcv: receive response from ESP8266
00029 
00030 int main()
00031 {
00032     pc.baud(115200);                        //Baud Rate of 115200 for Tera Term
00033 
00034     pc.printf("Initial Setup\r\n");
00035  
00036     while(num<1000000000000)
00037     {
00038         num=num+1;
00039     //checking for switch value to detect traffic light 
00040         if(sw2 == 0) {
00041         myRGBled.write(1.0,0.0,0.0); //red
00042         printf("Red light detected; applying breaks\n\r");
00043         wait(1.0);
00044         }else{
00045      myRGBled.write(0.0,1.0,0.0); //green
00046         
00047     //Ultrasound Sensor (HC-SR04) #1 Initialization
00048         usensor1.start();
00049         wait_ms(500);
00050         
00051     //Calculating Distance for Sensor # 1
00052         distance1 = usensor1.get_dist_cm();
00053 
00054     //LED and Tera Term Output
00055             if (distance1<10) {
00056                 RLed = 1;
00057                 BLed = 0;
00058                 GLed = 0;
00059                 printf("Emergency Brakes Engaged %u\n\r", distance1);
00060                 wait(1.0);
00061             } else {
00062                 GLed = 1;
00063                 BLed = 0;
00064                 RLed = 0;
00065                 printf("Distance: %u\n\r", distance1); 
00066             }    //end nested-if
00067         }   //end if
00068     }   //end while
00069 }   //end main