It begins

Dependencies:   SDFileSystem mbed-rtos mbed wave_player

Fork of BAT_senior_design by BAT

Committer:
goodguy8791
Date:
Wed Oct 18 14:30:55 2017 +0000
Revision:
11:3ce185f6f898
Parent:
10:e8d53d3177bc
Deleted Functions.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aismail1997 0:9eda4611081a 1 #include "mbed.h"
aismail1997 6:1aa86ad19af9 2 #include "rtos.h"
aismail1997 9:418a4437a693 3 #include "wave_player.h"
aismail1997 9:418a4437a693 4 #include "SDFileSystem.h"
aismail1997 3:9ed43e974156 5
aismail1997 0:9eda4611081a 6 PwmOut myservo(p21);
aismail1997 0:9eda4611081a 7 DigitalIn pb1 (p20);
aismail1997 0:9eda4611081a 8 //AnalogIn linpot(p20);
aismail1997 0:9eda4611081a 9 Serial pc(USBTX, USBRX);
aismail1997 4:cc181f8f2bd1 10 //DigitalOut myled(LED1);
aismail1997 4:cc181f8f2bd1 11 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 4:cc181f8f2bd1 12 AnalogOut DACout(p26);
aismail1997 4:cc181f8f2bd1 13 wave_player waver(&DACout);
aismail1997 4:cc181f8f2bd1 14
aismail1997 6:1aa86ad19af9 15 // add start, mode, reset buttons
aismail1997 9:418a4437a693 16 int start = 0;
aismail1997 9:418a4437a693 17 int submit = 0;
goodguy8791 10:e8d53d3177bc 18 // It begins
aismail1997 9:418a4437a693 19 // FUNCTIONS
aismail1997 6:1aa86ad19af9 20
aismail1997 9:418a4437a693 21 void moveServoIn(PwmOut servo, DigitalIn pb)
aismail1997 9:418a4437a693 22 {
aismail1997 9:418a4437a693 23 while(pb == 1)
aismail1997 9:418a4437a693 24 //myled = 1;
aismail1997 9:418a4437a693 25 // rotate 90 degrees one way
aismail1997 9:418a4437a693 26 for(int i=3; i<=7; i++) {
aismail1997 9:418a4437a693 27 servo = i/100.0;
aismail1997 9:418a4437a693 28 wait(0.01);
aismail1997 9:418a4437a693 29 }
aismail1997 9:418a4437a693 30 }
aismail1997 9:418a4437a693 31
aismail1997 9:418a4437a693 32 void moveServoOut(PwmOut servo, DigitalIn pb)
aismail1997 9:418a4437a693 33 {
aismail1997 9:418a4437a693 34 while(pb == 1)
aismail1997 9:418a4437a693 35 //myled = 0;
aismail1997 9:418a4437a693 36 for(int i=7; i>3; i--) {
aismail1997 9:418a4437a693 37 servo = i/100.0;
aismail1997 9:418a4437a693 38 wait(0.01);
aismail1997 9:418a4437a693 39 }
aismail1997 9:418a4437a693 40 }
aismail1997 9:418a4437a693 41
aismail1997 9:418a4437a693 42 void playSound(wave_player waver)
aismail1997 9:418a4437a693 43 {
aismail1997 9:418a4437a693 44 FILE *wave_file;
aismail1997 9:418a4437a693 45 wave_file=fopen("/sd/police_siren.wav","r");
aismail1997 9:418a4437a693 46 waver.play(wave_file);
aismail1997 9:418a4437a693 47 fclose(wave_file);
aismail1997 9:418a4437a693 48 }
aismail1997 6:1aa86ad19af9 49
aismail1997 6:1aa86ad19af9 50
aismail1997 6:1aa86ad19af9 51 // THREADS
aismail1997 6:1aa86ad19af9 52
aismail1997 6:1aa86ad19af9 53
aismail1997 6:1aa86ad19af9 54 void button_thread()
aismail1997 4:cc181f8f2bd1 55 {
aismail1997 6:1aa86ad19af9 56
aismail1997 4:cc181f8f2bd1 57 }
aismail1997 4:cc181f8f2bd1 58
aismail1997 6:1aa86ad19af9 59 void submit_thread()
aismail1997 4:cc181f8f2bd1 60 {
aismail1997 6:1aa86ad19af9 61
aismail1997 4:cc181f8f2bd1 62 }
aismail1997 4:cc181f8f2bd1 63
aismail1997 6:1aa86ad19af9 64 void start_thread()
aismail1997 4:cc181f8f2bd1 65 {
aismail1997 6:1aa86ad19af9 66 // read pb_start
aismail1997 6:1aa86ad19af9 67 // if 1
aismail1997 6:1aa86ad19af9 68 start == 1;
aismail1997 6:1aa86ad19af9 69 // else 0
aismail1997 4:cc181f8f2bd1 70 }
aismail1997 0:9eda4611081a 71
aismail1997 0:9eda4611081a 72 int main()
aismail1997 0:9eda4611081a 73 {
aismail1997 6:1aa86ad19af9 74 // SETUP
aismail1997 0:9eda4611081a 75 // button up: state = 0, button halfway: state = 1; button down: state = 2
aismail1997 0:9eda4611081a 76 int state = 2;
aismail1997 4:cc181f8f2bd1 77 // pull up the pushbutton to prevent bouncing
aismail1997 0:9eda4611081a 78 pb1.mode(PullUp);
aismail1997 0:9eda4611081a 79 wait(.001);
aismail1997 4:cc181f8f2bd1 80 // servo begins at 30 degrees
aismail1997 0:9eda4611081a 81 for(int i=0; i<=3; i++) {
aismail1997 0:9eda4611081a 82 myservo = i/100.0;
aismail1997 0:9eda4611081a 83 wait(0.01);
aismail1997 0:9eda4611081a 84 }
aismail1997 4:cc181f8f2bd1 85
aismail1997 6:1aa86ad19af9 86 // MAIN THREAD
aismail1997 6:1aa86ad19af9 87 while(true) {
aismail1997 6:1aa86ad19af9 88
aismail1997 6:1aa86ad19af9 89 // start threads for reset, mode, start
aismail1997 6:1aa86ad19af9 90 Thread t1(start_thread);
aismail1997 6:1aa86ad19af9 91
aismail1997 6:1aa86ad19af9 92 // setup SDcard and Speaker
aismail1997 6:1aa86ad19af9 93
aismail1997 6:1aa86ad19af9 94
aismail1997 6:1aa86ad19af9 95 // when started
aismail1997 6:1aa86ad19af9 96 while (start == 0) {}
aismail1997 6:1aa86ad19af9 97 Thread t2(button_thread);
aismail1997 6:1aa86ad19af9 98 Thread t3(submit_thread);
aismail1997 6:1aa86ad19af9 99
aismail1997 6:1aa86ad19af9 100 // when submitted
aismail1997 6:1aa86ad19af9 101 while (submit == 0) {}
aismail1997 6:1aa86ad19af9 102
aismail1997 6:1aa86ad19af9 103
aismail1997 6:1aa86ad19af9 104 // start button threads and submit thread
aismail1997 6:1aa86ad19af9 105 // if submit close button threads and submit thread
aismail1997 6:1aa86ad19af9 106 // check result, speaker result
aismail1997 6:1aa86ad19af9 107 // save results
aismail1997 6:1aa86ad19af9 108
aismail1997 6:1aa86ad19af9 109
aismail1997 6:1aa86ad19af9 110
aismail1997 6:1aa86ad19af9 111
aismail1997 0:9eda4611081a 112 // Servo code
aismail1997 0:9eda4611081a 113 if (pb1 == 1 && state == 2) {
aismail1997 6:1aa86ad19af9 114 moveServoIn(myservo, pb1);
aismail1997 0:9eda4611081a 115 state = 1;
aismail1997 0:9eda4611081a 116 }
aismail1997 0:9eda4611081a 117 // rotate 90 degrees other way
aismail1997 0:9eda4611081a 118 if (pb1 == 1 && state == 1) {
aismail1997 6:1aa86ad19af9 119 moveServoOut(myservo, pb1);
aismail1997 0:9eda4611081a 120 state = 2;
aismail1997 0:9eda4611081a 121 }
aismail1997 4:cc181f8f2bd1 122
aismail1997 4:cc181f8f2bd1 123 // SD card code
aismail1997 4:cc181f8f2bd1 124
aismail1997 4:cc181f8f2bd1 125 // speaker code
aismail1997 0:9eda4611081a 126
aismail1997 0:9eda4611081a 127 // read linear potentiometer
aismail1997 0:9eda4611081a 128 //if (linpot < 0.5) {
aismail1997 0:9eda4611081a 129 //float potval = linpot;
aismail1997 0:9eda4611081a 130 //pc.printf("linear pot: %f\n", potval);
aismail1997 0:9eda4611081a 131 }
aismail1997 0:9eda4611081a 132 }