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

Dependencies:   mbed

Committer:
ThatcherC
Date:
Sat Jul 26 16:58:52 2014 +0000
Revision:
0:d678323f9a72
Child:
1:62933cace87a
First commit! This code works properly when set up. Monitor the serial port at 9600 baud and you should see the delay between vibrations in seconds sent as an array.

Who changed what in which revision?

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