I did it
Dependencies: Motor Servo mbed
main.cpp@0:8c126e8b4c23, 2017-10-05 (annotated)
- Committer:
- goodguy8791
- Date:
- Thu Oct 05 18:42:42 2017 +0000
- Revision:
- 0:8c126e8b4c23
I did it
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
goodguy8791 | 0:8c126e8b4c23 | 1 | // Demonstrates the use of Bluetooth with each of the systems functionalities: |
goodguy8791 | 0:8c126e8b4c23 | 2 | // Cup dispenser, dispensing drink 1, dispensing drink 2, and conveyor belt. |
goodguy8791 | 0:8c126e8b4c23 | 3 | // This code: |
goodguy8791 | 0:8c126e8b4c23 | 4 | // - dispenses a cup when button 1 is pressed |
goodguy8791 | 0:8c126e8b4c23 | 5 | // - dispenses drink 1 when button 2 is pressed |
goodguy8791 | 0:8c126e8b4c23 | 6 | // - dispenses drink 2 when button 3 is pressed |
goodguy8791 | 0:8c126e8b4c23 | 7 | // - moves converyor belt for 4 seconds when button 4 is pressed |
goodguy8791 | 0:8c126e8b4c23 | 8 | |
goodguy8791 | 0:8c126e8b4c23 | 9 | #include "mbed.h" |
goodguy8791 | 0:8c126e8b4c23 | 10 | #include "Motor.h" |
goodguy8791 | 0:8c126e8b4c23 | 11 | #include "Servo.h" |
goodguy8791 | 0:8c126e8b4c23 | 12 | |
goodguy8791 | 0:8c126e8b4c23 | 13 | Servo poker(p22); |
goodguy8791 | 0:8c126e8b4c23 | 14 | DigitalIn stby(p25); |
goodguy8791 | 0:8c126e8b4c23 | 15 | Motor tread(p24, p12, p11); // pwm, fwd, rev (H-bridge) |
goodguy8791 | 0:8c126e8b4c23 | 16 | Motor pump(p26, p13, p14); // pwm, fwd, rev (H-bridge) |
goodguy8791 | 0:8c126e8b4c23 | 17 | DigitalOut tread2(p21); // MOSFET |
goodguy8791 | 0:8c126e8b4c23 | 18 | BusOut myled(LED1,LED2,LED3,LED4); // check for which button is pressed |
goodguy8791 | 0:8c126e8b4c23 | 19 | Serial blue(p28,p27); // Bluetooth |
goodguy8791 | 0:8c126e8b4c23 | 20 | float cupos = 0; |
goodguy8791 | 0:8c126e8b4c23 | 21 | void cupdispense(); |
goodguy8791 | 0:8c126e8b4c23 | 22 | void drink1(); |
goodguy8791 | 0:8c126e8b4c23 | 23 | void drink2(); |
goodguy8791 | 0:8c126e8b4c23 | 24 | void treads(float time); |
goodguy8791 | 0:8c126e8b4c23 | 25 | |
goodguy8791 | 0:8c126e8b4c23 | 26 | int main() |
goodguy8791 | 0:8c126e8b4c23 | 27 | { |
goodguy8791 | 0:8c126e8b4c23 | 28 | stby.mode(PullUp); |
goodguy8791 | 0:8c126e8b4c23 | 29 | poker = cupos; |
goodguy8791 | 0:8c126e8b4c23 | 30 | char bnum=0; |
goodguy8791 | 0:8c126e8b4c23 | 31 | char bhit=0; |
goodguy8791 | 0:8c126e8b4c23 | 32 | while(1) { |
goodguy8791 | 0:8c126e8b4c23 | 33 | if (cupos >=1){ |
goodguy8791 | 0:8c126e8b4c23 | 34 | cupos = 0; |
goodguy8791 | 0:8c126e8b4c23 | 35 | poker = cupos; |
goodguy8791 | 0:8c126e8b4c23 | 36 | } |
goodguy8791 | 0:8c126e8b4c23 | 37 | if (blue.getc()=='!') { |
goodguy8791 | 0:8c126e8b4c23 | 38 | if (blue.getc()=='B') { //button data packet |
goodguy8791 | 0:8c126e8b4c23 | 39 | bnum = blue.getc(); //button number |
goodguy8791 | 0:8c126e8b4c23 | 40 | bhit = blue.getc(); //1=hit, 0=release |
goodguy8791 | 0:8c126e8b4c23 | 41 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
goodguy8791 | 0:8c126e8b4c23 | 42 | myled = bnum - '0'; //current button number will appear on LEDs |
goodguy8791 | 0:8c126e8b4c23 | 43 | switch (bnum) { |
goodguy8791 | 0:8c126e8b4c23 | 44 | //case '1': //number button 1 |
goodguy8791 | 0:8c126e8b4c23 | 45 | // if (bhit=='1') { |
goodguy8791 | 0:8c126e8b4c23 | 46 | // //add hit code here |
goodguy8791 | 0:8c126e8b4c23 | 47 | // cupdispense(); |
goodguy8791 | 0:8c126e8b4c23 | 48 | // |
goodguy8791 | 0:8c126e8b4c23 | 49 | // } else { |
goodguy8791 | 0:8c126e8b4c23 | 50 | // //add release code here |
goodguy8791 | 0:8c126e8b4c23 | 51 | // } |
goodguy8791 | 0:8c126e8b4c23 | 52 | // break; |
goodguy8791 | 0:8c126e8b4c23 | 53 | case '2': //number button 2 |
goodguy8791 | 0:8c126e8b4c23 | 54 | if (bhit=='1') { |
goodguy8791 | 0:8c126e8b4c23 | 55 | //add hit code here |
goodguy8791 | 0:8c126e8b4c23 | 56 | drink1(); |
goodguy8791 | 0:8c126e8b4c23 | 57 | } else { |
goodguy8791 | 0:8c126e8b4c23 | 58 | //add release code here |
goodguy8791 | 0:8c126e8b4c23 | 59 | } |
goodguy8791 | 0:8c126e8b4c23 | 60 | break; |
goodguy8791 | 0:8c126e8b4c23 | 61 | case '3': //number button 3 |
goodguy8791 | 0:8c126e8b4c23 | 62 | if (bhit=='1') { |
goodguy8791 | 0:8c126e8b4c23 | 63 | //add hit code here |
goodguy8791 | 0:8c126e8b4c23 | 64 | drink2(); |
goodguy8791 | 0:8c126e8b4c23 | 65 | } else { |
goodguy8791 | 0:8c126e8b4c23 | 66 | //add release code here |
goodguy8791 | 0:8c126e8b4c23 | 67 | } |
goodguy8791 | 0:8c126e8b4c23 | 68 | break; |
goodguy8791 | 0:8c126e8b4c23 | 69 | case '4': //number button 4 |
goodguy8791 | 0:8c126e8b4c23 | 70 | if (bhit=='1') { |
goodguy8791 | 0:8c126e8b4c23 | 71 | //add hit code here |
goodguy8791 | 0:8c126e8b4c23 | 72 | treads(4); |
goodguy8791 | 0:8c126e8b4c23 | 73 | } else { |
goodguy8791 | 0:8c126e8b4c23 | 74 | //add release code here |
goodguy8791 | 0:8c126e8b4c23 | 75 | } |
goodguy8791 | 0:8c126e8b4c23 | 76 | break; |
goodguy8791 | 0:8c126e8b4c23 | 77 | |
goodguy8791 | 0:8c126e8b4c23 | 78 | default: |
goodguy8791 | 0:8c126e8b4c23 | 79 | break; |
goodguy8791 | 0:8c126e8b4c23 | 80 | } |
goodguy8791 | 0:8c126e8b4c23 | 81 | } |
goodguy8791 | 0:8c126e8b4c23 | 82 | } |
goodguy8791 | 0:8c126e8b4c23 | 83 | } |
goodguy8791 | 0:8c126e8b4c23 | 84 | } |
goodguy8791 | 0:8c126e8b4c23 | 85 | } |
goodguy8791 | 0:8c126e8b4c23 | 86 | |
goodguy8791 | 0:8c126e8b4c23 | 87 | void cupdispense(){ |
goodguy8791 | 0:8c126e8b4c23 | 88 | //cupos += .3; |
goodguy8791 | 0:8c126e8b4c23 | 89 | // poker = cupos; |
goodguy8791 | 0:8c126e8b4c23 | 90 | // wait(0.1); |
goodguy8791 | 0:8c126e8b4c23 | 91 | for(float p=0; p<1; p += 0.05) { |
goodguy8791 | 0:8c126e8b4c23 | 92 | poker = p; |
goodguy8791 | 0:8c126e8b4c23 | 93 | wait(1); |
goodguy8791 | 0:8c126e8b4c23 | 94 | } |
goodguy8791 | 0:8c126e8b4c23 | 95 | poker = 0; |
goodguy8791 | 0:8c126e8b4c23 | 96 | } |
goodguy8791 | 0:8c126e8b4c23 | 97 | |
goodguy8791 | 0:8c126e8b4c23 | 98 | void drink1(){ |
goodguy8791 | 0:8c126e8b4c23 | 99 | pump.speed(1); |
goodguy8791 | 0:8c126e8b4c23 | 100 | wait(2); |
goodguy8791 | 0:8c126e8b4c23 | 101 | pump.speed(0); |
goodguy8791 | 0:8c126e8b4c23 | 102 | |
goodguy8791 | 0:8c126e8b4c23 | 103 | } |
goodguy8791 | 0:8c126e8b4c23 | 104 | |
goodguy8791 | 0:8c126e8b4c23 | 105 | void drink2(){ |
goodguy8791 | 0:8c126e8b4c23 | 106 | pump.speed(-1); |
goodguy8791 | 0:8c126e8b4c23 | 107 | wait(2); |
goodguy8791 | 0:8c126e8b4c23 | 108 | pump.speed(0); |
goodguy8791 | 0:8c126e8b4c23 | 109 | } |
goodguy8791 | 0:8c126e8b4c23 | 110 | |
goodguy8791 | 0:8c126e8b4c23 | 111 | void treads(float time) { |
goodguy8791 | 0:8c126e8b4c23 | 112 | tread.speed(1); |
goodguy8791 | 0:8c126e8b4c23 | 113 | tread2 = 1; |
goodguy8791 | 0:8c126e8b4c23 | 114 | wait(time); |
goodguy8791 | 0:8c126e8b4c23 | 115 | tread.speed(0); |
goodguy8791 | 0:8c126e8b4c23 | 116 | tread2 = 0; |
goodguy8791 | 0:8c126e8b4c23 | 117 | } |
goodguy8791 | 0:8c126e8b4c23 | 118 |