Hi guys, I need help regarding a project to sense hand swipe direction. The code I have currently is to record the time whereby the sensor switches off. Any idea to modify the code to record the time whereby the sensor switches on? Appreciate your feedbacks thanks!
#define sIRA 10
#define sIRB 8
#define sIRC 9
#define LED1 3
#define LED2 4
#define LED3 5
char orders[3] = {'0','0','0'};
int indexOrder = 0;
//speed of detection Ms
int Captspeed = 1;
long tA;
long tB;
long tC;
void setup() {
Serial.begin (9600);
for(int i = 8;i<=10;i++)
pinMode(i, INPUT);
for(int i = 3;i<=5;i++)
pinMode(i, OUTPUT);
}
void loop() {
//Reset Command after 800 Msecond
if(millis()%1000 > 800)
ResetCommand();
//If we have 3 signals , Calculate Command
if(tA > 0 && tB> 0 && tC>0)
{
int command = CalculateCommand();
if(command <=0)
DoCommand(command);
}
//check for sensors
if(indexOrder > 2)
indexOrder = 0;
int vA = digitalRead(sIRA);
if(tA == 0 && vA == 0) tA = millis();
int vB = digitalRead(sIRB);
if(tB == 0 && vB == 0) tB = millis();
int vC = digitalRead(sIRC);
if(tC==0 && vC == 0) tC = millis();
//Stop Command
if(vA == 0 && vB == 0 && vC == 0 )
{
// DoCommand(0);
}
delay(Captspeed);
}
int CalculateCommand(void)
{
Serial.print("tA = ");
Serial.println(tA);
Serial.print("tB = ");
Serial.println(tB);
Serial.print("tC = ");
Serial.println(tC);
delay(1);
if((tA <= tB) && tA <= tC)
return -3 ;// FORWRD
if((tB <= tC) && tB <= tA)
return -2 ;// LEFT
if((tC <= tB) && tC <= tA)
return -1 ;//RIGHT
//Etc
return 1;
}
void DoCommand(int command)
{
switch (command)
{
case 0: Serial.println("Stopped");delay(500);break;
case -1: Serial.println("Turn RIGHT ");digitalWrite(LED1,HIGH); delay(1000);break;
case -2: Serial.println("Turn LEFT ");digitalWrite(LED2,HIGH); delay(1000);break;
case -3: Serial.println("GO FORWARD ");digitalWrite(LED3,HIGH); delay(1000);break;
case -4: Serial.println(" GO BACK "); delay(1000);break;
default : Serial.println("NO Command detected");break;
}
ResetCommand();
}
void ResetCommand()
{
for(int i = 3;i<=5;i++)
{
digitalWrite(i,LOW);//ALL LEDs
}
tA = 0;//Reset time A
tB = 0;//Reset time B
tC = 0;//Reset time C
}