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.
main.cpp@1:41844163cb7f, 2019-03-26 (annotated)
- Committer:
- lmpell
- Date:
- Tue Mar 26 17:45:39 2019 +0000
- Revision:
- 1:41844163cb7f
- Parent:
- 0:755a36a9fc5c
Added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lmpell | 0:755a36a9fc5c | 1 | #include "mbed.h" |
lmpell | 0:755a36a9fc5c | 2 | |
lmpell | 1:41844163cb7f | 3 | //tx, rx, baud rate |
lmpell | 1:41844163cb7f | 4 | Serial pc(USBTX, USBRX,9600); |
lmpell | 1:41844163cb7f | 5 | Serial blue(PTC15, PTC14, 115200); // for comm with bluetooth hc06 |
lmpell | 1:41844163cb7f | 6 | Serial other(PTC17, PTC16, 115200);// for comm with S32K |
lmpell | 0:755a36a9fc5c | 7 | Ticker locker; |
lmpell | 0:755a36a9fc5c | 8 | |
lmpell | 1:41844163cb7f | 9 | char prev = 'z'; //init states for use to determine if car needs to be locked |
lmpell | 0:755a36a9fc5c | 10 | char current = 'y'; |
lmpell | 0:755a36a9fc5c | 11 | bool nflag = true; |
lmpell | 0:755a36a9fc5c | 12 | |
lmpell | 1:41844163cb7f | 13 | // A callback function used to determine if car needs to be locked |
lmpell | 1:41844163cb7f | 14 | // it is called |
lmpell | 0:755a36a9fc5c | 15 | void lockCar() |
lmpell | 0:755a36a9fc5c | 16 | { |
lmpell | 0:755a36a9fc5c | 17 | pc.printf("ticker\r\n\r"); |
lmpell | 0:755a36a9fc5c | 18 | char send[] = "bj"; |
lmpell | 1:41844163cb7f | 19 | |
lmpell | 1:41844163cb7f | 20 | //checks to see if not already locked |
lmpell | 0:755a36a9fc5c | 21 | if(nflag) |
lmpell | 0:755a36a9fc5c | 22 | { |
lmpell | 1:41844163cb7f | 23 | //if no new key is pressed lock car |
lmpell | 0:755a36a9fc5c | 24 | if(prev == current) |
lmpell | 0:755a36a9fc5c | 25 | { |
lmpell | 1:41844163cb7f | 26 | //stops motor. |
lmpell | 0:755a36a9fc5c | 27 | for(int i = 1; i< 50; i++){other.putc(send[1]);} |
lmpell | 0:755a36a9fc5c | 28 | wait(5); |
lmpell | 1:41844163cb7f | 29 | //locks car |
lmpell | 0:755a36a9fc5c | 30 | for(int i = 1; i< 50; i++){other.putc(send[0]);} |
lmpell | 0:755a36a9fc5c | 31 | prev = 'z'; |
lmpell | 0:755a36a9fc5c | 32 | nflag = false; |
lmpell | 0:755a36a9fc5c | 33 | } |
lmpell | 0:755a36a9fc5c | 34 | else |
lmpell | 0:755a36a9fc5c | 35 | { |
lmpell | 0:755a36a9fc5c | 36 | prev = current; |
lmpell | 0:755a36a9fc5c | 37 | } |
lmpell | 0:755a36a9fc5c | 38 | } |
lmpell | 0:755a36a9fc5c | 39 | } |
lmpell | 0:755a36a9fc5c | 40 | |
lmpell | 0:755a36a9fc5c | 41 | int main() |
lmpell | 0:755a36a9fc5c | 42 | { |
lmpell | 1:41844163cb7f | 43 | int i = 0;//init variables |
lmpell | 0:755a36a9fc5c | 44 | int j = 0; |
lmpell | 0:755a36a9fc5c | 45 | char hold[100] ="String"; |
lmpell | 1:41844163cb7f | 46 | //attaches the callback function to a timer |
lmpell | 1:41844163cb7f | 47 | locker.attach(&lockCar, 20.0); |
lmpell | 0:755a36a9fc5c | 48 | pc.printf(hold); |
lmpell | 1:41844163cb7f | 49 | //controlling loop |
lmpell | 0:755a36a9fc5c | 50 | while (true) |
lmpell | 0:755a36a9fc5c | 51 | { |
lmpell | 1:41844163cb7f | 52 | //read from bluetooth if something is avalible |
lmpell | 0:755a36a9fc5c | 53 | while(blue.readable()) |
lmpell | 0:755a36a9fc5c | 54 | { |
lmpell | 1:41844163cb7f | 55 | //puts characters from bluetooth in buffer |
lmpell | 0:755a36a9fc5c | 56 | hold[j++] = blue.getc(); |
lmpell | 0:755a36a9fc5c | 57 | i = 10; |
lmpell | 0:755a36a9fc5c | 58 | nflag = true; |
lmpell | 0:755a36a9fc5c | 59 | } |
lmpell | 0:755a36a9fc5c | 60 | |
lmpell | 1:41844163cb7f | 61 | //if buffer has elements |
lmpell | 0:755a36a9fc5c | 62 | while(i) |
lmpell | 0:755a36a9fc5c | 63 | { |
lmpell | 1:41844163cb7f | 64 | //put null at end of string |
lmpell | 0:755a36a9fc5c | 65 | hold[j] = '\0'; |
lmpell | 1:41844163cb7f | 66 | //send element to S32K |
lmpell | 0:755a36a9fc5c | 67 | other.putc(hold[0]); |
lmpell | 1:41844163cb7f | 68 | //update flag for locking system |
lmpell | 0:755a36a9fc5c | 69 | current = hold[0]; |
lmpell | 1:41844163cb7f | 70 | //display info to tera term |
lmpell | 0:755a36a9fc5c | 71 | pc.printf("%d:", i); |
lmpell | 0:755a36a9fc5c | 72 | pc.printf(hold); |
lmpell | 0:755a36a9fc5c | 73 | pc.putc('\n'); |
lmpell | 0:755a36a9fc5c | 74 | pc.putc('\r'); |
lmpell | 0:755a36a9fc5c | 75 | i--; |
lmpell | 0:755a36a9fc5c | 76 | } |
lmpell | 0:755a36a9fc5c | 77 | |
lmpell | 0:755a36a9fc5c | 78 | j = 0; |
lmpell | 0:755a36a9fc5c | 79 | i=0; |
lmpell | 0:755a36a9fc5c | 80 | } |
lmpell | 0:755a36a9fc5c | 81 | } |