This is program to simulate the functions of a car.
Dependencies: DigitalOutEx DebounceIn Ultrasonic TextLCD DCMotor
main.cpp@0:707b06553f35, 2021-01-06 (annotated)
- Committer:
- 3ghost17
- Date:
- Wed Jan 06 06:50:35 2021 +0000
- Revision:
- 0:707b06553f35
This is a car simulator.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
3ghost17 | 0:707b06553f35 | 1 | /* mbed Microcontroller Library |
3ghost17 | 0:707b06553f35 | 2 | * Copyright (c) 2019 ARM Limited |
3ghost17 | 0:707b06553f35 | 3 | * SPDX-License-Identifier: Apache-2.0 |
3ghost17 | 0:707b06553f35 | 4 | |
3ghost17 | 0:707b06553f35 | 5 | ECE595 Design with embeded systems |
3ghost17 | 0:707b06553f35 | 6 | Project: Car simulator |
3ghost17 | 0:707b06553f35 | 7 | by: Charles White & Lammar Lee |
3ghost17 | 0:707b06553f35 | 8 | |
3ghost17 | 0:707b06553f35 | 9 | Car simulator able to simulate some of the basic peripherals |
3ghost17 | 0:707b06553f35 | 10 | |
3ghost17 | 0:707b06553f35 | 11 | |
3ghost17 | 0:707b06553f35 | 12 | */ |
3ghost17 | 0:707b06553f35 | 13 | |
3ghost17 | 0:707b06553f35 | 14 | #include "mbed.h" |
3ghost17 | 0:707b06553f35 | 15 | #include <iostream> |
3ghost17 | 0:707b06553f35 | 16 | #include <string> |
3ghost17 | 0:707b06553f35 | 17 | #include "DCMotor.h" |
3ghost17 | 0:707b06553f35 | 18 | #include "DigitalOutEx.h" |
3ghost17 | 0:707b06553f35 | 19 | #include "TextLCD.h" |
3ghost17 | 0:707b06553f35 | 20 | #include "Ultrasonic.h" |
3ghost17 | 0:707b06553f35 | 21 | #include "DebounceIn.h" |
3ghost17 | 0:707b06553f35 | 22 | #include "platform/mbed_thread.h" |
3ghost17 | 0:707b06553f35 | 23 | |
3ghost17 | 0:707b06553f35 | 24 | // Blinking rate in milliseconds |
3ghost17 | 0:707b06553f35 | 25 | #define BLINKING_RATE_MS 500 |
3ghost17 | 0:707b06553f35 | 26 | |
3ghost17 | 0:707b06553f35 | 27 | //Push Buttons |
3ghost17 | 0:707b06553f35 | 28 | DebounceIn StartBtn(D0); //Start Btn |
3ghost17 | 0:707b06553f35 | 29 | DebounceIn My_Btn2(D2); // Motor Btn |
3ghost17 | 0:707b06553f35 | 30 | DebounceIn BtnBlnkL(D6); //Blinker liket Btn |
3ghost17 | 0:707b06553f35 | 31 | |
3ghost17 | 0:707b06553f35 | 32 | //Light system |
3ghost17 | 0:707b06553f35 | 33 | DigitalOutEx StartLED(D1); |
3ghost17 | 0:707b06553f35 | 34 | DigitalOutEx BlnkL(D4); |
3ghost17 | 0:707b06553f35 | 35 | |
3ghost17 | 0:707b06553f35 | 36 | //sensor |
3ghost17 | 0:707b06553f35 | 37 | DebounceIn backUpBtn(D15); |
3ghost17 | 0:707b06553f35 | 38 | DigitalIn echo(D5); |
3ghost17 | 0:707b06553f35 | 39 | DigitalOutEx trigger(D7); // |
3ghost17 | 0:707b06553f35 | 40 | DigitalOutEx backLight(LED1); //used for reverse |
3ghost17 | 0:707b06553f35 | 41 | |
3ghost17 | 0:707b06553f35 | 42 | |
3ghost17 | 0:707b06553f35 | 43 | //Motor |
3ghost17 | 0:707b06553f35 | 44 | DigitalOutEx DCM(D3); |
3ghost17 | 0:707b06553f35 | 45 | |
3ghost17 | 0:707b06553f35 | 46 | //LCD |
3ghost17 | 0:707b06553f35 | 47 | TextLCD lcd(D8, D9, D10, D11, D12, D13); |
3ghost17 | 0:707b06553f35 | 48 | |
3ghost17 | 0:707b06553f35 | 49 | bool status; |
3ghost17 | 0:707b06553f35 | 50 | |
3ghost17 | 0:707b06553f35 | 51 | //proto |
3ghost17 | 0:707b06553f35 | 52 | void backUp(void); |
3ghost17 | 0:707b06553f35 | 53 | void blinkLights(void); |
3ghost17 | 0:707b06553f35 | 54 | void LCD(const char*); |
3ghost17 | 0:707b06553f35 | 55 | void accelerate(void); |
3ghost17 | 0:707b06553f35 | 56 | void startVic(void); |
3ghost17 | 0:707b06553f35 | 57 | |
3ghost17 | 0:707b06553f35 | 58 | //back up function |
3ghost17 | 0:707b06553f35 | 59 | Ultrasonic sensor(D7, D5); |
3ghost17 | 0:707b06553f35 | 60 | bool btnStatus = false; |
3ghost17 | 0:707b06553f35 | 61 | void backUp(void){ |
3ghost17 | 0:707b06553f35 | 62 | if (backUpBtn.read() == 0){ |
3ghost17 | 0:707b06553f35 | 63 | btnStatus = !btnStatus; |
3ghost17 | 0:707b06553f35 | 64 | } |
3ghost17 | 0:707b06553f35 | 65 | if (btnStatus == true){ |
3ghost17 | 0:707b06553f35 | 66 | backLight = !backLight; |
3ghost17 | 0:707b06553f35 | 67 | lcd.printf("Distance: %d\n",sensor.read_cm()); |
3ghost17 | 0:707b06553f35 | 68 | accelerate(); |
3ghost17 | 0:707b06553f35 | 69 | blinkLights(); |
3ghost17 | 0:707b06553f35 | 70 | wait_us(50); |
3ghost17 | 0:707b06553f35 | 71 | } |
3ghost17 | 0:707b06553f35 | 72 | } |
3ghost17 | 0:707b06553f35 | 73 | |
3ghost17 | 0:707b06553f35 | 74 | //blinker |
3ghost17 | 0:707b06553f35 | 75 | void blinkLights(void){ |
3ghost17 | 0:707b06553f35 | 76 | if(BtnBlnkL.read() == 1){ |
3ghost17 | 0:707b06553f35 | 77 | if(BlnkL.is_flashing()){ |
3ghost17 | 0:707b06553f35 | 78 | BlnkL.stop_flashing(); |
3ghost17 | 0:707b06553f35 | 79 | wait_us(50); |
3ghost17 | 0:707b06553f35 | 80 | } |
3ghost17 | 0:707b06553f35 | 81 | else{ |
3ghost17 | 0:707b06553f35 | 82 | BlnkL.flash(); |
3ghost17 | 0:707b06553f35 | 83 | wait_us(50); |
3ghost17 | 0:707b06553f35 | 84 | } |
3ghost17 | 0:707b06553f35 | 85 | } |
3ghost17 | 0:707b06553f35 | 86 | } |
3ghost17 | 0:707b06553f35 | 87 | |
3ghost17 | 0:707b06553f35 | 88 | //allow to print on the LCD |
3ghost17 | 0:707b06553f35 | 89 | void LCD(const char* text){ |
3ghost17 | 0:707b06553f35 | 90 | //clear the screen |
3ghost17 | 0:707b06553f35 | 91 | lcd.cls(); |
3ghost17 | 0:707b06553f35 | 92 | //enter text |
3ghost17 | 0:707b06553f35 | 93 | lcd.printf(text); |
3ghost17 | 0:707b06553f35 | 94 | }; |
3ghost17 | 0:707b06553f35 | 95 | |
3ghost17 | 0:707b06553f35 | 96 | void accelerate(void){ |
3ghost17 | 0:707b06553f35 | 97 | if(My_Btn2.read() == 1){ |
3ghost17 | 0:707b06553f35 | 98 | DCM.write(0); |
3ghost17 | 0:707b06553f35 | 99 | } |
3ghost17 | 0:707b06553f35 | 100 | else{ |
3ghost17 | 0:707b06553f35 | 101 | DCM.write(1); |
3ghost17 | 0:707b06553f35 | 102 | } |
3ghost17 | 0:707b06553f35 | 103 | }; |
3ghost17 | 0:707b06553f35 | 104 | |
3ghost17 | 0:707b06553f35 | 105 | /*it will allow all the systems to be functional. |
3ghost17 | 0:707b06553f35 | 106 | *the start button need to be press in order to start the car. |
3ghost17 | 0:707b06553f35 | 107 | *if the start button is pressed again it will shut down the system.*/ |
3ghost17 | 0:707b06553f35 | 108 | void startVic(void){ |
3ghost17 | 0:707b06553f35 | 109 | if (StartBtn.read()== 1){ |
3ghost17 | 0:707b06553f35 | 110 | status = !status; |
3ghost17 | 0:707b06553f35 | 111 | } |
3ghost17 | 0:707b06553f35 | 112 | //it will allow all the systems to function. |
3ghost17 | 0:707b06553f35 | 113 | if (status == true){ |
3ghost17 | 0:707b06553f35 | 114 | StartLED = 1; |
3ghost17 | 0:707b06553f35 | 115 | LCD("System\nRunning\n"); |
3ghost17 | 0:707b06553f35 | 116 | } |
3ghost17 | 0:707b06553f35 | 117 | //anyperiferical connected will not work. |
3ghost17 | 0:707b06553f35 | 118 | else{ |
3ghost17 | 0:707b06553f35 | 119 | StartLED = 0; |
3ghost17 | 0:707b06553f35 | 120 | LCD("Push button\nto start\n"); |
3ghost17 | 0:707b06553f35 | 121 | } |
3ghost17 | 0:707b06553f35 | 122 | if (status){ |
3ghost17 | 0:707b06553f35 | 123 | accelerate(); |
3ghost17 | 0:707b06553f35 | 124 | blinkLights(); |
3ghost17 | 0:707b06553f35 | 125 | backUp(); |
3ghost17 | 0:707b06553f35 | 126 | } |
3ghost17 | 0:707b06553f35 | 127 | }; |
3ghost17 | 0:707b06553f35 | 128 | |
3ghost17 | 0:707b06553f35 | 129 | |
3ghost17 | 0:707b06553f35 | 130 | |
3ghost17 | 0:707b06553f35 | 131 | int main() |
3ghost17 | 0:707b06553f35 | 132 | { |
3ghost17 | 0:707b06553f35 | 133 | StartBtn.mode(PullDown); |
3ghost17 | 0:707b06553f35 | 134 | backUpBtn.mode(PullDown); |
3ghost17 | 0:707b06553f35 | 135 | My_Btn2.mode(PullDown); |
3ghost17 | 0:707b06553f35 | 136 | BtnBlnkL.mode(PullDown); |
3ghost17 | 0:707b06553f35 | 137 | lcd.cls(); |
3ghost17 | 0:707b06553f35 | 138 | lcd.printf("System\nInitializing\n"); |
3ghost17 | 0:707b06553f35 | 139 | ThisThread::sleep_for(600); |
3ghost17 | 0:707b06553f35 | 140 | while(true){ |
3ghost17 | 0:707b06553f35 | 141 | startVic(); |
3ghost17 | 0:707b06553f35 | 142 | ThisThread::sleep_for(125); |
3ghost17 | 0:707b06553f35 | 143 | } |
3ghost17 | 0:707b06553f35 | 144 | |
3ghost17 | 0:707b06553f35 | 145 | } |