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 mbed-rtos Motor HC_SR04_Ultrasonic_Library
main.cpp@0:a36c747fb7e0, 2019-12-09 (annotated)
- Committer:
 - tzahi12345
 - Date:
 - Mon Dec 09 04:49:58 2019 +0000
 - Revision:
 - 0:a36c747fb7e0
 
a commit;
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| tzahi12345 | 0:a36c747fb7e0 | 1 | #include "mbed.h" | 
| tzahi12345 | 0:a36c747fb7e0 | 2 | #include "Motor.h" | 
| tzahi12345 | 0:a36c747fb7e0 | 3 | #include "rtos.h" | 
| tzahi12345 | 0:a36c747fb7e0 | 4 | #include "ultrasonic.h" | 
| tzahi12345 | 0:a36c747fb7e0 | 5 | |
| tzahi12345 | 0:a36c747fb7e0 | 6 | // THREADS | 
| tzahi12345 | 0:a36c747fb7e0 | 7 | |
| tzahi12345 | 0:a36c747fb7e0 | 8 | Thread dispenseThread; | 
| tzahi12345 | 0:a36c747fb7e0 | 9 | |
| tzahi12345 | 0:a36c747fb7e0 | 10 | // BLUETOOTH | 
| tzahi12345 | 0:a36c747fb7e0 | 11 | |
| tzahi12345 | 0:a36c747fb7e0 | 12 | Serial blue(p28,p27); | 
| tzahi12345 | 0:a36c747fb7e0 | 13 | |
| tzahi12345 | 0:a36c747fb7e0 | 14 | // MOTOR | 
| tzahi12345 | 0:a36c747fb7e0 | 15 | |
| tzahi12345 | 0:a36c747fb7e0 | 16 | Motor ketch(p21, p11, p12); // pwm, fwd, rev | 
| tzahi12345 | 0:a36c747fb7e0 | 17 | |
| tzahi12345 | 0:a36c747fb7e0 | 18 | // DEBUG | 
| tzahi12345 | 0:a36c747fb7e0 | 19 | |
| tzahi12345 | 0:a36c747fb7e0 | 20 | DigitalOut debugLED(LED1); | 
| tzahi12345 | 0:a36c747fb7e0 | 21 | DigitalOut errorLED(LED4); | 
| tzahi12345 | 0:a36c747fb7e0 | 22 | |
| tzahi12345 | 0:a36c747fb7e0 | 23 | // SONAR | 
| tzahi12345 | 0:a36c747fb7e0 | 24 | int global_distance; | 
| tzahi12345 | 0:a36c747fb7e0 | 25 | |
| tzahi12345 | 0:a36c747fb7e0 | 26 | void dist(int a_distance) | 
| tzahi12345 | 0:a36c747fb7e0 | 27 | { | 
| tzahi12345 | 0:a36c747fb7e0 | 28 | global_distance = a_distance; | 
| tzahi12345 | 0:a36c747fb7e0 | 29 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 30 | ultrasonic mu(p6, p7, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 | 
| tzahi12345 | 0:a36c747fb7e0 | 31 | //have updates every .1 seconds and a timeout after 1 | 
| tzahi12345 | 0:a36c747fb7e0 | 32 | //second, and call dist when the distance changes | 
| tzahi12345 | 0:a36c747fb7e0 | 33 | // Servo ketch(p21); | 
| tzahi12345 | 0:a36c747fb7e0 | 34 | |
| tzahi12345 | 0:a36c747fb7e0 | 35 | |
| tzahi12345 | 0:a36c747fb7e0 | 36 | bool ketchupDispensing = false; | 
| tzahi12345 | 0:a36c747fb7e0 | 37 | |
| tzahi12345 | 0:a36c747fb7e0 | 38 | void dispenseKetchup() { | 
| tzahi12345 | 0:a36c747fb7e0 | 39 | if (ketchupDispensing) { | 
| tzahi12345 | 0:a36c747fb7e0 | 40 | // already dispensing the ketch | 
| tzahi12345 | 0:a36c747fb7e0 | 41 | return; | 
| tzahi12345 | 0:a36c747fb7e0 | 42 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 43 | |
| tzahi12345 | 0:a36c747fb7e0 | 44 | // set dispensing flag | 
| tzahi12345 | 0:a36c747fb7e0 | 45 | ketchupDispensing = true; | 
| tzahi12345 | 0:a36c747fb7e0 | 46 | |
| tzahi12345 | 0:a36c747fb7e0 | 47 | //dispense | 
| tzahi12345 | 0:a36c747fb7e0 | 48 | ketch.speed(1.0f); | 
| tzahi12345 | 0:a36c747fb7e0 | 49 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 50 | |
| tzahi12345 | 0:a36c747fb7e0 | 51 | void stopDispenseKetchup() { | 
| tzahi12345 | 0:a36c747fb7e0 | 52 | if (!ketchupDispensing) { | 
| tzahi12345 | 0:a36c747fb7e0 | 53 | // not dispensing, you can't stop no ketch, so just return | 
| tzahi12345 | 0:a36c747fb7e0 | 54 | return; | 
| tzahi12345 | 0:a36c747fb7e0 | 55 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 56 | |
| tzahi12345 | 0:a36c747fb7e0 | 57 | // unset dispensing flag | 
| tzahi12345 | 0:a36c747fb7e0 | 58 | ketchupDispensing = false; | 
| tzahi12345 | 0:a36c747fb7e0 | 59 | ketch.speed(0.0f); | 
| tzahi12345 | 0:a36c747fb7e0 | 60 | |
| tzahi12345 | 0:a36c747fb7e0 | 61 | // turns debug LEDs off just in case | 
| tzahi12345 | 0:a36c747fb7e0 | 62 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 63 | errorLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 64 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 65 | |
| tzahi12345 | 0:a36c747fb7e0 | 66 | void dispenseSomeKetchup() { | 
| tzahi12345 | 0:a36c747fb7e0 | 67 | if (global_distance < 150) { | 
| tzahi12345 | 0:a36c747fb7e0 | 68 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 69 | printf("In thread, dispensing ketchup\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 70 | dispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 71 | Thread::wait(1750); | 
| tzahi12345 | 0:a36c747fb7e0 | 72 | stopDispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 73 | printf("Stopped dispense\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 74 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 75 | } else { | 
| tzahi12345 | 0:a36c747fb7e0 | 76 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 77 | errorLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 78 | Thread::wait(1750); | 
| tzahi12345 | 0:a36c747fb7e0 | 79 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 80 | errorLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 81 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 82 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 83 | |
| tzahi12345 | 0:a36c747fb7e0 | 84 | void dispenseMuchKetchup() { | 
| tzahi12345 | 0:a36c747fb7e0 | 85 | if (global_distance < 150) { | 
| tzahi12345 | 0:a36c747fb7e0 | 86 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 87 | printf("In thread, dispensing ketchup\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 88 | dispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 89 | Thread::wait(3500); | 
| tzahi12345 | 0:a36c747fb7e0 | 90 | stopDispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 91 | printf("Stopped dispense\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 92 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 93 | } else { | 
| tzahi12345 | 0:a36c747fb7e0 | 94 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 95 | errorLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 96 | Thread::wait(1750); | 
| tzahi12345 | 0:a36c747fb7e0 | 97 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 98 | errorLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 99 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 100 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 101 | |
| tzahi12345 | 0:a36c747fb7e0 | 102 | void dispenseKetchupInfinitely() { | 
| tzahi12345 | 0:a36c747fb7e0 | 103 | if (global_distance < 150) { | 
| tzahi12345 | 0:a36c747fb7e0 | 104 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 105 | printf("In thread, dispensing ketchup continuous\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 106 | dispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 107 | } else { | 
| tzahi12345 | 0:a36c747fb7e0 | 108 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 109 | errorLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 110 | Thread::wait(1750); | 
| tzahi12345 | 0:a36c747fb7e0 | 111 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 112 | errorLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 113 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 114 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 115 | |
| tzahi12345 | 0:a36c747fb7e0 | 116 | int main() | 
| tzahi12345 | 0:a36c747fb7e0 | 117 | { | 
| tzahi12345 | 0:a36c747fb7e0 | 118 | mu.startUpdates();//start measuring the distance | 
| tzahi12345 | 0:a36c747fb7e0 | 119 | debugLED = 1; | 
| tzahi12345 | 0:a36c747fb7e0 | 120 | ketch.speed(1.0f); | 
| tzahi12345 | 0:a36c747fb7e0 | 121 | Thread::wait(1000); | 
| tzahi12345 | 0:a36c747fb7e0 | 122 | ketch.speed(0.0f); | 
| tzahi12345 | 0:a36c747fb7e0 | 123 | debugLED = 0; | 
| tzahi12345 | 0:a36c747fb7e0 | 124 | while(1) { | 
| tzahi12345 | 0:a36c747fb7e0 | 125 | //Do something else here | 
| tzahi12345 | 0:a36c747fb7e0 | 126 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where | 
| tzahi12345 | 0:a36c747fb7e0 | 127 | //the class checks if dist needs to be called. | 
| tzahi12345 | 0:a36c747fb7e0 | 128 | char bnum; | 
| tzahi12345 | 0:a36c747fb7e0 | 129 | if (blue.getc()=='!') { | 
| tzahi12345 | 0:a36c747fb7e0 | 130 | if (blue.getc()=='B') { //button data | 
| tzahi12345 | 0:a36c747fb7e0 | 131 | bnum = blue.getc(); //button number | 
| tzahi12345 | 0:a36c747fb7e0 | 132 | printf("bnum is %c", bnum); | 
| tzahi12345 | 0:a36c747fb7e0 | 133 | if ((bnum>='1')&&(bnum<='4')) //is a number button 1..4 | 
| tzahi12345 | 0:a36c747fb7e0 | 134 | { | 
| tzahi12345 | 0:a36c747fb7e0 | 135 | if (bnum == '1') { | 
| tzahi12345 | 0:a36c747fb7e0 | 136 | // dispense some ketchup | 
| tzahi12345 | 0:a36c747fb7e0 | 137 | printf("Starting thread\n"); | 
| tzahi12345 | 0:a36c747fb7e0 | 138 | dispenseThread.start(dispenseSomeKetchup); | 
| tzahi12345 | 0:a36c747fb7e0 | 139 | } else if (bnum == '2') { | 
| tzahi12345 | 0:a36c747fb7e0 | 140 | // dispense a lot of ketchup | 
| tzahi12345 | 0:a36c747fb7e0 | 141 | dispenseMuchKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 142 | } else if (bnum == '3') { | 
| tzahi12345 | 0:a36c747fb7e0 | 143 | // infinite ketchup | 
| tzahi12345 | 0:a36c747fb7e0 | 144 | dispenseKetchupInfinitely(); | 
| tzahi12345 | 0:a36c747fb7e0 | 145 | } else if (bnum == '4') { | 
| tzahi12345 | 0:a36c747fb7e0 | 146 | // stop dispense ketchup | 
| tzahi12345 | 0:a36c747fb7e0 | 147 | stopDispenseKetchup(); | 
| tzahi12345 | 0:a36c747fb7e0 | 148 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 149 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 150 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 151 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 152 | } | 
| tzahi12345 | 0:a36c747fb7e0 | 153 | } |