For use in my acoustic touchscreen project, found here: http://hackaday.io/project/1990-Low-Cost-Touchscreen-Anywhere

Dependencies:   mbed

Committer:
ThatcherC
Date:
Sat Aug 09 15:04:11 2014 +0000
Revision:
2:2ccbee38f4a8
Parent:
1:62933cace87a
Child:
3:5d221e8f4012
Changed serial baud rate to 115200 bps to try to alleviate a mysterious halting problem. Serial output is also now in microseconds, not seconds.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThatcherC 0:d678323f9a72 1 #include "mbed.h"
ThatcherC 0:d678323f9a72 2
ThatcherC 2:2ccbee38f4a8 3 Serial pc(dp16,dp15);
ThatcherC 0:d678323f9a72 4 DigitalOut myled(dp1);
ThatcherC 0:d678323f9a72 5 DigitalOut led2(dp4);
ThatcherC 0:d678323f9a72 6 InterruptIn int1(dp14);
ThatcherC 0:d678323f9a72 7 InterruptIn int2(dp13);
ThatcherC 0:d678323f9a72 8 Timer t;
ThatcherC 0:d678323f9a72 9 Timeout reset;
ThatcherC 0:d678323f9a72 10 bool first = true;
ThatcherC 0:d678323f9a72 11 bool timeAvailable = false;
ThatcherC 0:d678323f9a72 12
ThatcherC 0:d678323f9a72 13 void reset1();
ThatcherC 0:d678323f9a72 14 void reset2();
ThatcherC 0:d678323f9a72 15
ThatcherC 0:d678323f9a72 16 float timeArray[3]; //important. this stores the delays between vibration detections
ThatcherC 0:d678323f9a72 17 //right now only indices 0 and 1 are used
ThatcherC 0:d678323f9a72 18 void flip1(){
ThatcherC 0:d678323f9a72 19 if(first){
ThatcherC 0:d678323f9a72 20 t.start();
ThatcherC 0:d678323f9a72 21 int1.fall(NULL);
ThatcherC 0:d678323f9a72 22 first = false;
ThatcherC 0:d678323f9a72 23 reset.attach(&reset1,0.01);
ThatcherC 0:d678323f9a72 24 }else{
ThatcherC 0:d678323f9a72 25 t.stop();
ThatcherC 0:d678323f9a72 26 timeArray[0]=t.read();
ThatcherC 0:d678323f9a72 27 reset.detach();
ThatcherC 0:d678323f9a72 28 timeAvailable=true;
ThatcherC 0:d678323f9a72 29 first = true;
ThatcherC 0:d678323f9a72 30 }
ThatcherC 0:d678323f9a72 31 myled = 1;
ThatcherC 0:d678323f9a72 32 }
ThatcherC 0:d678323f9a72 33
ThatcherC 0:d678323f9a72 34 void flip2(){
ThatcherC 0:d678323f9a72 35 if(first){
ThatcherC 0:d678323f9a72 36 t.start();
ThatcherC 0:d678323f9a72 37 int2.fall(NULL);
ThatcherC 0:d678323f9a72 38 first = false;
ThatcherC 0:d678323f9a72 39 reset.attach(&reset2,0.01);
ThatcherC 0:d678323f9a72 40 }else{
ThatcherC 0:d678323f9a72 41 t.stop();
ThatcherC 0:d678323f9a72 42 timeArray[1]=t.read();
ThatcherC 0:d678323f9a72 43 reset.detach();
ThatcherC 0:d678323f9a72 44 timeAvailable=true;
ThatcherC 0:d678323f9a72 45 first = true;
ThatcherC 0:d678323f9a72 46 }
ThatcherC 0:d678323f9a72 47 led2 = 1;
ThatcherC 0:d678323f9a72 48 }
ThatcherC 0:d678323f9a72 49
ThatcherC 0:d678323f9a72 50 void reset1(){
ThatcherC 0:d678323f9a72 51 t.stop();
ThatcherC 0:d678323f9a72 52 t.reset();
ThatcherC 0:d678323f9a72 53 first = true;
ThatcherC 0:d678323f9a72 54 int1.fall(&flip1);
ThatcherC 0:d678323f9a72 55 }
ThatcherC 0:d678323f9a72 56
ThatcherC 0:d678323f9a72 57 void reset2(){
ThatcherC 0:d678323f9a72 58 t.stop();
ThatcherC 0:d678323f9a72 59 t.reset();
ThatcherC 0:d678323f9a72 60 first = true;
ThatcherC 0:d678323f9a72 61 int2.fall(&flip2);
ThatcherC 0:d678323f9a72 62 }
ThatcherC 0:d678323f9a72 63
ThatcherC 0:d678323f9a72 64 int main() {
ThatcherC 2:2ccbee38f4a8 65 pc.baud(115200);
ThatcherC 2:2ccbee38f4a8 66 pc.printf("Ready\n");
ThatcherC 0:d678323f9a72 67 int1.fall(&flip1);
ThatcherC 0:d678323f9a72 68 int2.fall(&flip2);
ThatcherC 0:d678323f9a72 69 timeArray[0] = 0;
ThatcherC 0:d678323f9a72 70 timeArray[1] = 0;
ThatcherC 0:d678323f9a72 71 timeArray[2] = 0; //for now this isn't used (only two sensors)
ThatcherC 0:d678323f9a72 72 while(1){
ThatcherC 0:d678323f9a72 73 if(timeAvailable){
ThatcherC 2:2ccbee38f4a8 74 int tA0 = int(timeArray[0]*1000000);
ThatcherC 2:2ccbee38f4a8 75 int tA1 = int(timeArray[1]*1000000);
ThatcherC 2:2ccbee38f4a8 76 pc.printf("[%i, %i]\n",tA0,tA1);
ThatcherC 0:d678323f9a72 77 t.reset();
ThatcherC 0:d678323f9a72 78 timeArray[0] = 0;
ThatcherC 0:d678323f9a72 79 timeArray[1] = 0;
ThatcherC 0:d678323f9a72 80 timeArray[2] = 0; //for now this isn't used (only two sensors)
ThatcherC 0:d678323f9a72 81 timeAvailable=false;
ThatcherC 0:d678323f9a72 82 wait(.1);
ThatcherC 0:d678323f9a72 83 int1.fall(&flip1);
ThatcherC 0:d678323f9a72 84 int2.fall(&flip2);
ThatcherC 0:d678323f9a72 85 }
ThatcherC 0:d678323f9a72 86 }
ThatcherC 0:d678323f9a72 87 }