Problem timming the interval betwen two bluetooth inputs

Hey guys,

I’m working on a project that uses Bluetooth module and needs to time the interval between a press and a release of the button on the smartphone.

Starting with the beginning , I used Bluetooth Electronic app from Google Appstore to create a custom remote control for my project, and I created a button which, on press sends the character “D” to Arduino, and on release sends the character “d”.

What i need to know is the time elapsed between the recieve of D and the recieve of d.

Here’s my code:

#include <SoftwareSerial.h>
unsigned long start;
unsigned long endt;
unsigned long tot;

SoftwareSerial b(0, 1); //for Bluetooth

void setup() {

  Serial.begin(9600);
  b.begin(9600);
}

void loop() {
  unsigned long timer = millis();
  int c;
  if (b.available())
    c = b.read();

  if (c == 65) {
    start = millis();

  }

  if (c == 100) {
    endt = millis();

    tot = endt - start;
    Serial.println (tot);

  }

}

How can i modify this code to show me the time elapsed between the button press and the button release (EG 340, 830 or whatever value is) ?

Right now it shows me :

1200
5466
32000

etc… , when the button press is less than 2 seconds every time.

Which Arduino board are you using ?
Most use pins 0 and 1 for hardware Serial but you are using them for SoftwareSerial

I created a button which, on press sends the character "D" to Arduino,

  if (c == 65)You appear to be testing for an A. If you used a char variable then you could text explicitly of 'A' and 'd' which would be easier to understand

Ok, i rewrite the code now, with the command recieved from bluetooth as character. But now, it show me only weird stuff on the serial monitor.
The board used is Arduino Uno and the code is below:

#include <SoftwareSerial.h>

unsigned long start; // store time value when "D" is received
unsigned long endt;  // store time value wen "d" is received
unsigned long tot;  //total time while the button is pressed


SoftwareSerial BT(0, 1);

void setup() {
  // put your setup code here, to run once:


Serial.begin(9600);
BT.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
unsigned long timer1 = millis();
char c;
if (BT.available())
c = BT.read();
if (c == "D" || c == "d") {
  Serial.println ("Valid command aquired"); //only for display purpose
}

 if (c!=0) {

if (c ="D") {
  start = millis ();
  Serial.println (c);
}

if (c="d") {
  endt = millis();
  Serial.println (c);
  tot = endt - start;
  Serial.println ("Pressed time is: ");
  Serial.print(tot);


}
}
}

But now, the result on the serial monitor when i press and release button, as below:

Pressed time is:
0’
=
Pressed time is:
0’
=
Pressed time is:
0’
=
Pressed time is:
0’
=
Pressed time is:
0’

Any kind of help is welcomed…

Problem solved; the code is below if someone else needs it sometime:

#include <SoftwareSerial.h>
unsigned long timpD;
unsigned long timpdo;
unsigned long dif;
unsigned long lcm = 0;
unsigned long inte = 700;

byte val = 0;

SoftwareSerial BT(2, 3);

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
BT.begin(9600);
}



void loop() {
  // put your main code here, to run repeatedly:
unsigned long stt = millis();
int c;


if (BT.available())
c = BT.read();

if (c==68 || c ==100 )  {
  Serial.println ("SSS");
  }
if (stt - lcm > inte && c == 0) {
   lcm = stt;
}
  
if (c!=0) {
  lcm = stt;
if (c==68) {
  
  timpD = millis();
  Serial.println(timpD);

}

if (c == 100) {
  
  timpdo = millis();
  Serial.println(timpdo);
  dif = timpdo - timpD;
  Serial.println (dif);
  
  
  
    
  }
  
}


}


[\code]