Charles White / Mbed OS Car_Simulator

Dependencies:   DigitalOutEx DebounceIn Ultrasonic TextLCD DCMotor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  
00005  ECE595 Design with embeded systems
00006  Project: Car simulator
00007  by: Charles White & Lammar Lee
00008  
00009  Car simulator able to simulate some of the basic peripherals
00010   
00011  
00012  */
00013 
00014 #include "mbed.h"
00015 #include <iostream>
00016 #include <string>
00017 #include "DCMotor.h"
00018 #include "DigitalOutEx.h"
00019 #include "TextLCD.h"
00020 #include "Ultrasonic.h"
00021 #include "DebounceIn.h"
00022 #include "platform/mbed_thread.h"
00023 
00024 // Blinking rate in milliseconds
00025 #define BLINKING_RATE_MS                                                    500
00026 
00027 //Push Buttons
00028 DebounceIn StartBtn(D0);    //Start Btn
00029 DebounceIn My_Btn2(D2);      // Motor Btn
00030 DebounceIn BtnBlnkL(D6);     //Blinker liket Btn
00031 
00032 //Light system 
00033 DigitalOutEx StartLED(D1);
00034 DigitalOutEx BlnkL(D4);
00035 
00036 //sensor
00037 DebounceIn backUpBtn(D15);
00038 DigitalIn echo(D5);
00039 DigitalOutEx trigger(D7); //
00040 DigitalOutEx backLight(LED1); //used for reverse
00041 
00042 
00043 //Motor
00044 DigitalOutEx DCM(D3);
00045 
00046 //LCD
00047 TextLCD lcd(D8, D9, D10, D11, D12, D13);
00048 
00049 bool status;
00050 
00051 //proto
00052 void backUp(void);
00053 void blinkLights(void);
00054 void LCD(const char*);
00055 void accelerate(void);
00056 void startVic(void);
00057 
00058 //back up function
00059 Ultrasonic sensor(D7, D5);
00060 bool btnStatus = false;
00061 void backUp(void){
00062     if (backUpBtn.read() == 0){ 
00063         btnStatus = !btnStatus;
00064     }
00065     if (btnStatus == true){
00066         backLight = !backLight;
00067         lcd.printf("Distance: %d\n",sensor.read_cm());
00068         accelerate();
00069         blinkLights();
00070         wait_us(50);
00071     }
00072 }
00073 
00074 //blinker
00075 void blinkLights(void){
00076     if(BtnBlnkL.read() == 1){
00077         if(BlnkL.is_flashing()){
00078             BlnkL.stop_flashing();
00079             wait_us(50);
00080         }
00081         else{
00082             BlnkL.flash();
00083             wait_us(50);
00084         }
00085     }
00086 }
00087 
00088 //allow to print on the LCD   
00089 void LCD(const char* text){
00090     //clear the screen
00091     lcd.cls();
00092     //enter text
00093     lcd.printf(text);
00094 };
00095 
00096 void accelerate(void){
00097     if(My_Btn2.read() == 1){
00098         DCM.write(0);
00099     }
00100     else{
00101         DCM.write(1);
00102     }
00103 };
00104 
00105 /*it will allow all the systems to be functional.
00106 *the start button need to be press in order to start the car.
00107 *if the start button is pressed again it will shut down the system.*/
00108 void startVic(void){
00109     if (StartBtn.read()== 1){
00110         status = !status;
00111     }
00112     //it will allow all the systems to function. 
00113     if (status == true){
00114         StartLED = 1;
00115         LCD("System\nRunning\n");
00116     }
00117     //anyperiferical connected will not work.
00118     else{
00119         StartLED = 0;
00120        LCD("Push button\nto start\n");
00121     }  
00122     if (status){
00123         accelerate();
00124         blinkLights();
00125         backUp();
00126     }  
00127 };
00128 
00129 
00130 
00131 int main()
00132 {
00133     StartBtn.mode(PullDown);
00134     backUpBtn.mode(PullDown);
00135     My_Btn2.mode(PullDown);
00136     BtnBlnkL.mode(PullDown);
00137     lcd.cls();
00138     lcd.printf("System\nInitializing\n");
00139     ThisThread::sleep_for(600);
00140     while(true){
00141         startVic();
00142         ThisThread::sleep_for(125);
00143     }               
00144      
00145 }