Not receiving data from arduino

hi , I`m building a push up counter up using arduino HC-06 bluetooth & ultrasonic sensor HC-SR04

I successfully connect the arduino to the app but the counter is not changing! could use some help
this part of the code:

#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
int vibePin = 4; // Vibrabion Pin
SoftwareSerial BT(0, 1); // RX, TX Bluetooth
int BTdata = 0 ; // app variable
long duration, distance; // distance variables
boolean trigUp = false; // upper body trigger
boolean trigDown = false; // lower body trigger
float Cnt=0; // Push Up counter

void Pushup()
{
while (BTdata == '1')
{
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH); // receive, convert time (us) to cm
distance = duration * 0.034/2;
if (distance >= 30 && distance <=45) //upper body trigger
{
trigUp = true;
delay(1000);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH); // receive, convert time (us) to cm
distance = duration * 0.034/2;
if (distance >= 10 && distance <=20) // lower body trigger
{
trigDown = true;
}
else if (distance < 10 || distance > 45) //vibration trigger
{
digitalWrite(vibePin,HIGH);
}
if (trigUp == true && trigDown == true)
{
Cnt = Cnt +1;
trigUp = false;
trigDown = false;
}
Serial.write(Cnt);
BTdata == BT.read();
if (BTdata == '0');
{
exit;
}
}
BTdata == BT.read();
if (BTdata == '3')
{
Cnt = 0;
Serial.write(Cnt);
}
}
}

Hi Haltham

To send to the App use:
Serial.print(Cnt)
Serial.println()

In App Inventor, set the DelimiterByte to 10 (on Screen Init). Also, collect the sent data thus:

However, this is only half the story - you are sending and receiving. What is the exact model of your Arduino?

I`m using ardunio uno, why half the story ???
why Serial println() is essential?
my counter variable Cnt is float, is it ok to set the receiving data on app as text ?

Hi

Next time you reply, use the reply button just below the post you are replying to, as this notifies the author of the post.

OK, Arduino Uno has only one serial channel, but you are sending and receiving, so in an ideal world you would use two channels. You can use the Arduino Software Serial Library to add a second channel.

It tells the App "end of data packet" - note that you must set the delimiter in the App as already mentioned.

It certainly is - though counting is normally done with integers.......

Note that you also need to synchronise the App Receive time interval with the Arduino Loop time interval. Firstly, the Arduino interval should be set to the slowest practical. The App interval should be approx 20% faster.

Would you like an example AI Project + Sketch? Just basics upon which you can build?

I`d be glad to

BT_Basic_Setup_Receive.aia (8.5 KB)
ArduinoToApp1val.txt (796 Bytes)

Some hints and tips on my website:
https://www.professorcad.co.uk/appinventortips#TipsMicrocontroller
https://www.professorcad.co.uk/appinventortips#TipsBluetooth

1 Like