Bluetooth Controlled Robot with IR Sensor
Dependencies: Motor RemoteIR mbed
main.cpp@0:590527c95423, 2016-10-31 (annotated)
- Committer:
- ashatune
- Date:
- Mon Oct 31 13:49:57 2016 +0000
- Revision:
- 0:590527c95423
for notebook page
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashatune | 0:590527c95423 | 1 | #include "mbed.h" |
ashatune | 0:590527c95423 | 2 | #include "Motor.h" |
ashatune | 0:590527c95423 | 3 | #include "ReceiverIR.h" |
ashatune | 0:590527c95423 | 4 | #include "Speaker.h" |
ashatune | 0:590527c95423 | 5 | |
ashatune | 0:590527c95423 | 6 | //Asha Harris, Teferi Taylor |
ashatune | 0:590527c95423 | 7 | //Hamblen ECE 4180 Lab 4 IoT Robot and IR sensor program |
ashatune | 0:590527c95423 | 8 | // 10/27/16 |
ashatune | 0:590527c95423 | 9 | |
ashatune | 0:590527c95423 | 10 | AnalogIn irSens(p15); |
ashatune | 0:590527c95423 | 11 | Speaker mySpeaker(p22); |
ashatune | 0:590527c95423 | 12 | |
ashatune | 0:590527c95423 | 13 | BusOut myled(LED1,LED2,LED3,LED4); |
ashatune | 0:590527c95423 | 14 | Serial blue(p28,p27); |
ashatune | 0:590527c95423 | 15 | |
ashatune | 0:590527c95423 | 16 | //motor initializations.. |
ashatune | 0:590527c95423 | 17 | Motor A(p25, p6, p5); // pwm, fwd, rev, can brake |
ashatune | 0:590527c95423 | 18 | Motor B(p26, p7, p8); // pwm, fwd, rev, can brake |
ashatune | 0:590527c95423 | 19 | int main() |
ashatune | 0:590527c95423 | 20 | { |
ashatune | 0:590527c95423 | 21 | char bnum=0; |
ashatune | 0:590527c95423 | 22 | char bhit=0; |
ashatune | 0:590527c95423 | 23 | while(1) { |
ashatune | 0:590527c95423 | 24 | //mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 25 | if (blue.getc()=='!') { |
ashatune | 0:590527c95423 | 26 | if (blue.getc()=='B') { //button data packet |
ashatune | 0:590527c95423 | 27 | bnum = blue.getc(); //button number |
ashatune | 0:590527c95423 | 28 | bhit = blue.getc(); //1=hit, 0=release |
ashatune | 0:590527c95423 | 29 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
ashatune | 0:590527c95423 | 30 | myled = bnum - '0'; //current button number will appear on LEDs |
ashatune | 0:590527c95423 | 31 | switch (bnum) { |
ashatune | 0:590527c95423 | 32 | case '1': //number button 1 |
ashatune | 0:590527c95423 | 33 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 34 | myled[0] = 1; |
ashatune | 0:590527c95423 | 35 | } else { |
ashatune | 0:590527c95423 | 36 | myled[0] = 0; |
ashatune | 0:590527c95423 | 37 | } |
ashatune | 0:590527c95423 | 38 | break; |
ashatune | 0:590527c95423 | 39 | case '2': //number button 2 |
ashatune | 0:590527c95423 | 40 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 41 | myled[1] = 1; |
ashatune | 0:590527c95423 | 42 | } else { |
ashatune | 0:590527c95423 | 43 | myled[1] = 0; |
ashatune | 0:590527c95423 | 44 | } |
ashatune | 0:590527c95423 | 45 | break; |
ashatune | 0:590527c95423 | 46 | case '3': //number button 3 |
ashatune | 0:590527c95423 | 47 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 48 | myled[2] = 1; |
ashatune | 0:590527c95423 | 49 | } else { |
ashatune | 0:590527c95423 | 50 | myled[2] = 0; |
ashatune | 0:590527c95423 | 51 | } |
ashatune | 0:590527c95423 | 52 | break; |
ashatune | 0:590527c95423 | 53 | case '4': //number button 4 |
ashatune | 0:590527c95423 | 54 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 55 | myled[3] = 1; |
ashatune | 0:590527c95423 | 56 | } else { |
ashatune | 0:590527c95423 | 57 | myled[3] = 0; |
ashatune | 0:590527c95423 | 58 | } |
ashatune | 0:590527c95423 | 59 | break; |
ashatune | 0:590527c95423 | 60 | case '5': //button 5 up arrow "forward" |
ashatune | 0:590527c95423 | 61 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 62 | A.speed(0.4); |
ashatune | 0:590527c95423 | 63 | B.speed(0.4); |
ashatune | 0:590527c95423 | 64 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); //speaker plays tone at frequency corresponding to IR sensor readings |
ashatune | 0:590527c95423 | 65 | } else { |
ashatune | 0:590527c95423 | 66 | |
ashatune | 0:590527c95423 | 67 | //A.stop(1); |
ashatune | 0:590527c95423 | 68 | //B.stop(1); |
ashatune | 0:590527c95423 | 69 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 70 | } |
ashatune | 0:590527c95423 | 71 | break; |
ashatune | 0:590527c95423 | 72 | case '6': //button 6 down arrow "reverse" |
ashatune | 0:590527c95423 | 73 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 74 | A.speed(-0.4); |
ashatune | 0:590527c95423 | 75 | B.speed(-0.4); |
ashatune | 0:590527c95423 | 76 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 77 | } else { |
ashatune | 0:590527c95423 | 78 | //A.stop(1); |
ashatune | 0:590527c95423 | 79 | //B.stop(1); |
ashatune | 0:590527c95423 | 80 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 81 | } |
ashatune | 0:590527c95423 | 82 | break; |
ashatune | 0:590527c95423 | 83 | case '7': //button 7 left arrow "left" |
ashatune | 0:590527c95423 | 84 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 85 | A.speed(.5); |
ashatune | 0:590527c95423 | 86 | B.speed(-.5); |
ashatune | 0:590527c95423 | 87 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 88 | } else { |
ashatune | 0:590527c95423 | 89 | A.speed(0); |
ashatune | 0:590527c95423 | 90 | B.speed(0); |
ashatune | 0:590527c95423 | 91 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 92 | |
ashatune | 0:590527c95423 | 93 | } |
ashatune | 0:590527c95423 | 94 | break; |
ashatune | 0:590527c95423 | 95 | case '8': //button 8 right arrow "right" |
ashatune | 0:590527c95423 | 96 | if (bhit=='1') { |
ashatune | 0:590527c95423 | 97 | B.speed(.5); |
ashatune | 0:590527c95423 | 98 | A.speed(-.5); |
ashatune | 0:590527c95423 | 99 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 100 | } else { |
ashatune | 0:590527c95423 | 101 | B.speed(0); |
ashatune | 0:590527c95423 | 102 | A.speed(0); |
ashatune | 0:590527c95423 | 103 | mySpeaker.PlayNote(969.0*irSens,0.6,0.1); |
ashatune | 0:590527c95423 | 104 | } |
ashatune | 0:590527c95423 | 105 | break; |
ashatune | 0:590527c95423 | 106 | default: |
ashatune | 0:590527c95423 | 107 | break; |
ashatune | 0:590527c95423 | 108 | } |
ashatune | 0:590527c95423 | 109 | } |
ashatune | 0:590527c95423 | 110 | } |
ashatune | 0:590527c95423 | 111 | } |
ashatune | 0:590527c95423 | 112 | } |
ashatune | 0:590527c95423 | 113 | } |