It begins
Dependencies: SDFileSystem mbed-rtos mbed wave_player
Fork of BAT_senior_design by
main.cpp@4:cc181f8f2bd1, 2017-10-11 (annotated)
- Committer:
- aismail1997
- Date:
- Wed Oct 11 14:51:39 2017 +0000
- Revision:
- 4:cc181f8f2bd1
- Parent:
- 3:9ed43e974156
- Child:
- 6:1aa86ad19af9
added speaker and SD card
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 0:9eda4611081a | 1 | #include "mbed.h" |
aismail1997 | 4:cc181f8f2bd1 | 2 | #include "wave_player.h" |
aismail1997 | 4:cc181f8f2bd1 | 3 | #include "SDFileSystem.h" |
aismail1997 | 3:9ed43e974156 | 4 | |
aismail1997 | 0:9eda4611081a | 5 | PwmOut myservo(p21); |
aismail1997 | 0:9eda4611081a | 6 | DigitalIn pb1 (p20); |
aismail1997 | 0:9eda4611081a | 7 | //AnalogIn linpot(p20); |
aismail1997 | 0:9eda4611081a | 8 | Serial pc(USBTX, USBRX); |
aismail1997 | 4:cc181f8f2bd1 | 9 | //DigitalOut myled(LED1); |
aismail1997 | 4:cc181f8f2bd1 | 10 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 4:cc181f8f2bd1 | 11 | AnalogOut DACout(p26); |
aismail1997 | 4:cc181f8f2bd1 | 12 | wave_player waver(&DACout); |
aismail1997 | 4:cc181f8f2bd1 | 13 | |
aismail1997 | 4:cc181f8f2bd1 | 14 | void moveServoIn() |
aismail1997 | 4:cc181f8f2bd1 | 15 | { |
aismail1997 | 4:cc181f8f2bd1 | 16 | while(pb1 == 1) |
aismail1997 | 4:cc181f8f2bd1 | 17 | //myled = 1; |
aismail1997 | 4:cc181f8f2bd1 | 18 | // rotate 90 degrees one way |
aismail1997 | 4:cc181f8f2bd1 | 19 | for(int i=3; i<=7; i++) { |
aismail1997 | 4:cc181f8f2bd1 | 20 | myservo = i/100.0; |
aismail1997 | 4:cc181f8f2bd1 | 21 | wait(0.01); |
aismail1997 | 4:cc181f8f2bd1 | 22 | } |
aismail1997 | 4:cc181f8f2bd1 | 23 | } |
aismail1997 | 4:cc181f8f2bd1 | 24 | |
aismail1997 | 4:cc181f8f2bd1 | 25 | void moveServoOut() |
aismail1997 | 4:cc181f8f2bd1 | 26 | { |
aismail1997 | 4:cc181f8f2bd1 | 27 | while(pb1 == 1) |
aismail1997 | 4:cc181f8f2bd1 | 28 | //myled = 0; |
aismail1997 | 4:cc181f8f2bd1 | 29 | for(int i=7; i>3; i--) { |
aismail1997 | 4:cc181f8f2bd1 | 30 | myservo = i/100.0; |
aismail1997 | 4:cc181f8f2bd1 | 31 | wait(0.01); |
aismail1997 | 4:cc181f8f2bd1 | 32 | } |
aismail1997 | 4:cc181f8f2bd1 | 33 | } |
aismail1997 | 4:cc181f8f2bd1 | 34 | |
aismail1997 | 4:cc181f8f2bd1 | 35 | void playSound() |
aismail1997 | 4:cc181f8f2bd1 | 36 | { |
aismail1997 | 4:cc181f8f2bd1 | 37 | FILE *wave_file; |
aismail1997 | 4:cc181f8f2bd1 | 38 | wave_file=fopen("/sd/police_siren.wav","r"); |
aismail1997 | 4:cc181f8f2bd1 | 39 | waver.play(wave_file); |
aismail1997 | 4:cc181f8f2bd1 | 40 | fclose(wave_file); |
aismail1997 | 4:cc181f8f2bd1 | 41 | } |
aismail1997 | 0:9eda4611081a | 42 | |
aismail1997 | 0:9eda4611081a | 43 | int main() |
aismail1997 | 0:9eda4611081a | 44 | { |
aismail1997 | 4:cc181f8f2bd1 | 45 | pc.printf("begin\n"); |
aismail1997 | 4:cc181f8f2bd1 | 46 | |
aismail1997 | 0:9eda4611081a | 47 | // button up: state = 0, button halfway: state = 1; button down: state = 2 |
aismail1997 | 0:9eda4611081a | 48 | int state = 2; |
aismail1997 | 4:cc181f8f2bd1 | 49 | |
aismail1997 | 4:cc181f8f2bd1 | 50 | // pull up the pushbutton to prevent bouncing |
aismail1997 | 0:9eda4611081a | 51 | pb1.mode(PullUp); |
aismail1997 | 0:9eda4611081a | 52 | wait(.001); |
aismail1997 | 4:cc181f8f2bd1 | 53 | |
aismail1997 | 4:cc181f8f2bd1 | 54 | // servo begins at 30 degrees |
aismail1997 | 0:9eda4611081a | 55 | for(int i=0; i<=3; i++) { |
aismail1997 | 0:9eda4611081a | 56 | myservo = i/100.0; |
aismail1997 | 0:9eda4611081a | 57 | wait(0.01); |
aismail1997 | 0:9eda4611081a | 58 | } |
aismail1997 | 4:cc181f8f2bd1 | 59 | |
aismail1997 | 0:9eda4611081a | 60 | while(1) { |
aismail1997 | 0:9eda4611081a | 61 | // Servo code |
aismail1997 | 0:9eda4611081a | 62 | if (pb1 == 1 && state == 2) { |
aismail1997 | 4:cc181f8f2bd1 | 63 | moveServoIn(); |
aismail1997 | 0:9eda4611081a | 64 | state = 1; |
aismail1997 | 0:9eda4611081a | 65 | } |
aismail1997 | 0:9eda4611081a | 66 | // rotate 90 degrees other way |
aismail1997 | 0:9eda4611081a | 67 | if (pb1 == 1 && state == 1) { |
aismail1997 | 4:cc181f8f2bd1 | 68 | moveServoOut(); |
aismail1997 | 0:9eda4611081a | 69 | state = 2; |
aismail1997 | 0:9eda4611081a | 70 | } |
aismail1997 | 4:cc181f8f2bd1 | 71 | |
aismail1997 | 4:cc181f8f2bd1 | 72 | // SD card code |
aismail1997 | 4:cc181f8f2bd1 | 73 | |
aismail1997 | 4:cc181f8f2bd1 | 74 | // speaker code |
aismail1997 | 0:9eda4611081a | 75 | |
aismail1997 | 0:9eda4611081a | 76 | // read linear potentiometer |
aismail1997 | 0:9eda4611081a | 77 | //if (linpot < 0.5) { |
aismail1997 | 0:9eda4611081a | 78 | //float potval = linpot; |
aismail1997 | 0:9eda4611081a | 79 | //pc.printf("linear pot: %f\n", potval); |
aismail1997 | 0:9eda4611081a | 80 | } |
aismail1997 | 0:9eda4611081a | 81 | } |