Android和Arduino伺服电机蓝牙串口

时间:2015-01-21 14:03:27

标签: android arduino android-bluetooth

我希望在Arduino伺服电机和Android蓝牙之间进行通信。 实际上我想从我的Android设备发送1到180,而Arduino伺服电机运行此代码。

我确信arduino的这段代码是真的:

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 
int motor = 0;

void setup() 
{   
  Serial.begin(9600);  // initialize serial:  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

  Serial.print("Arduino control Servo Motor Connected OK");
  Serial.print('\n');
  Serial.println(" ");
} 

void loop() 
{  
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    motor = Serial.parseInt(); 

    // do it again:
    pos = Serial.parseInt(); 

     Serial.print("Data is: : ");
     Serial.println(pos);


    // look for the newline. That's the end of your  sentence:
    if (Serial.read() == '\n') {

       myservo.write(pos);              // tell servo to go to position in variable 'pos' 
       delay(15);                       // waits 15ms for the servo to reach the position

      // print the three numbers in one string as hexadecimal:
     // Serial.print("Data Response : ");
     // Serial.print(motor, HEX);
     // Serial.print(pos, HEX);
    }
  }
}

但我不知道如何从Android设备发送使用此代码的数据! 我是Android程序员,我研究了更多的代码,但是当我发送字符串代码时,伺服电机不起作用。

0 个答案:

没有答案