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

Dependencies:   mbed

main.cpp

Committer:
ThatcherC
Date:
2014-07-27
Revision:
1:62933cace87a
Parent:
0:d678323f9a72
Child:
2:2ccbee38f4a8

File content as of revision 1:62933cace87a:

#include "mbed.h"

DigitalOut myled(dp1);
DigitalOut led2(dp4);
InterruptIn int1(dp14);
InterruptIn int2(dp13);
Timer t;
Timeout reset;
bool first = true;
bool timeAvailable = false;

void reset1();
void reset2();

float timeArray[3];  //important. this stores the delays between vibration detections
                   //right now only indices 0 and 1 are used
void flip1(){
    if(first){
        t.start();
        int1.fall(NULL);
        first = false;
        reset.attach(&reset1,0.01);
    }else{
        t.stop();
        timeArray[0]=t.read();
        reset.detach();
        timeAvailable=true;
        first = true;
    }
    myled = 1;
}

void flip2(){
    if(first){
        t.start();
        int2.fall(NULL);
        first = false;
        reset.attach(&reset2,0.01);
    }else{
        t.stop();
        timeArray[1]=t.read();
        reset.detach();
        timeAvailable=true;
        first = true;
    }
    led2 = 1;
}

void reset1(){
    t.stop();
    t.reset();
    first = true;
    int1.fall(&flip1);
}

void reset2(){
    t.stop();
    t.reset();
    first = true;
    int2.fall(&flip2);
}

int main() {
    printf("Ready\n");
    int1.fall(&flip1);
    int2.fall(&flip2);
    timeArray[0] = 0;
    timeArray[1] = 0;
    timeArray[2] = 0;   //for now this isn't used (only two sensors)
    while(1){
        if(timeAvailable){
            printf("[%f, %f]\n",timeArray[0],timeArray[1]);
            t.reset();
            timeArray[0] = 0;
            timeArray[1] = 0;
            timeArray[2] = 0;   //for now this isn't used (only two sensors)
            timeAvailable=false;
            wait(.1);
            int1.fall(&flip1);
            int2.fall(&flip2);
        }
    }
}