Measuring time difference between analogue IR sensors


i attempting build project emulate drum pad when finished, projection drum pad. have designed method of reading ir reflections using several sharp gp2y0 analogue output sensors.

in order attain velocity value, place 1 sensor above other , have code decipher difference between 2 (time difference) find out how user has hit surface.

currently have code working single sensor, adding second layer velocity proving difficult. code @ moment looks movement between distances , output midi signal, signal sensor.

here code, appreciated:

code: [select]
#include <midi.h>
midi_create_default_instance();

int currentvala = 0;
int previousvala = 0;
int vala = 0;                         // value store sensor a
const int valchangea = 40;    //motion change value
int del = 0;                          //variable delay value


void setup(){
   midi.begin(midi_channel_omni);    // launch midi on channels
   serial.begin(115200);
   int del = analogread(a0);
}

void loop(){  
 currentvala = analogread(a4);  
 vala = abs(previousvala - currentvala);
 
 if(currentvala <= previousvala + valchangea || currentvala >= previousvala - valchangea){ //if change large enough
 
 if (currentvala < 500 && currentvala > 450){ //drum pad 1 parameters (20cm away, pad size 5cm)
   
   if (vala >= valchangea){ //if there has been enough of difference in movement
       midi.sendnoteon(30,100,1);  // send note (pitch 42, velo 100 on channel 4)
       delay(500)/*delay(del)*/;   // delay value variable (pot in a0)
       midi.sendnoteoff(30,100,1);   // stop note
   }
 }
 
 else if (currentvala < 400 && currentvala > 350){ //drum pad 2 parameters (30 cm away, pad size 5cm)
   if (vala >= valchangea){
       midi.sendnoteon(50,100,1);
       delay(500)/*delay(del)*/;
       midi.sendnoteoff(50,100,1);
   }
 }
 
 else if (currentvala < 300 && currentvala > 260){ //drum pad 3 parameters (40 cm away, pad size 5cm)
   if (vala >= valchangea){
       midi.sendnoteon(70,100,1);
       delay(500)/*delay(del)*/;
       midi.sendnoteoff(70,100,1);
   }
 }
   else if (currentvala < 250 && currentvala > 200){ //drum pad 4 parameters (50 cm away, pad size 5cm)
   if (vala >= valchangea){
       midi.sendnoteon(90,100,1);
       delay(500)/*delay(del)*/;
       midi.sendnoteoff(90,100,1);
   }
 }
previousvala = currentvala; //set previous value @ end of loop
//serial.println(currentvala);
}
}

slight update: have tried using millis() function time constants of 2 sensors compared (where difference = velocity value):

code: [select]
long sensoratime = 0l;
long sensorbtime = 0l;
long difference = 0l;

difference = sensoratime - sensorbtime;

sensoratime = millis()

-rest of code-

sensorbtime;



i know wrong feel i'm getting bit closer....


Arduino Forum > Using Arduino > Sensors > Measuring time difference between analogue IR sensors


arduino

Comments