Go Down

Topic: can't sending message by bluetooth (Read 54 times) previous topic - next topic

ju1101

Hi,
I'm making a sorting hat that when button push, the servo will moving and sending the message to app.
My problem is i pushing the button but app will receive the message after a very long time, or even don't get it! (I'm sure about my bluetooth connection, servo will moving, and app can receive another message)
Can anyone tell me what happen to my code? thanks.

Here is my code:
Code: [Select]

#include <Servo.h>
#include<SoftwareSerial.h>

SoftwareSerial I2CBT(10,11); //Bluetooth:hc-06

Servo myservo1;
Servo myservo2;

int btn=0;
const int TX=10;
const int Rx=11;

byte serial_code;

void setup()
{
  Serial.begin(9600);
  I2CBT.begin(57600);
  pinMode(13,INPUT);
}

void loop()
{
  btn=digitalRead(13);
 
   if (btn == HIGH) {
   
    //star the servo
    myservo1.attach(3, 500, 2400);
    myservo2.attach(5, 500, 2400);

    //sending the messege to phone(!problem here!)
    I2CBT.write(3);
   
    Serial.println("yes");
    //-----------------------------------------
    //伺服馬達轉動控制
    for(int i = 1200; i <= 1500; i+=15){
      myservo2.writeMicroseconds(i);
      delay(50);
    }
    for(int i = 1000; i <= 1500; i+=15){
      myservo1.writeMicroseconds(i);
      delay(50);
    }
    for(int i = 1500; i >= 1000; i-=15){
      myservo1.writeMicroseconds(i);
      delay(50);
    }
    for(int i = 1500; i >= 1200; i-=15){
      myservo2.writeMicroseconds(i);
      delay(50);
    }
   }
    else
    {
      Serial.println("no");

      //sending the messege to phone(this can work!)
      I2CBT.write(2);
    }
   
}


and my app:


PaulS

Code: [Select]
SoftwareSerial I2CBT(10,11); //Bluetooth:hc-06

Servo myservo1;
Servo myservo2;

int btn=0;
const int TX=10;
const int Rx=11;

Use the pin numbers, and then give them names. How useful.

How is your switch wired? You are probably flooding the phone app with a bunch of NOs. You should only send a message when the state of the switch changes. Look at the state change detection example, and wire your switch properly.

Nick_Pyner

Your code is confusing but you appear to be running software serial at 57600. This is a bad idea. Don't expect any joy over 38400, and you probably don't need to run it over 9600 anyway, so you might as well reset it at that speed. This presupposes you configured the HC-06 to run at 57600 in the first place.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy