如何使用arduino代码来分离从蓝牙发送的字符串和整数?

时间:2017-07-30 15:02:01

标签: android bluetooth arduino-uno

如何编写代码来分离从蓝牙发送的字符串和整数来控制伺服和led。我发送'a'和'b'用于开/关led,而整数1-90用于移动servopos?这是我的代码哪里出错了?

#include <SoftwareSerial.h>

#include <Servo.h>
Servo servo;


int bluetoothTx = 10;
int bluetoothRx = 11;
char data = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() 

{
  pinMode(13, OUTPUT);
  servo.attach(9);
  //setup usb serial connection to computer
  Serial.begin(9600);  
  bluetooth.begin(9600);
   delay(100);
  //setup bluetooth serial connection to android
}


void loop() 
{
  if(Serial.available() > 0)  
  { 
     data = Serial.read();      
    Serial.print(data);        
    if(data == 'a')            
      digitalWrite(13, HIGH);  
    else if(data == 'b')       
      digitalWrite(13, LOW);   
    if(bluetooth.available()> 0 )
  {   

    int servopos = bluetooth.read();
    int servopos1;

    servopos1 = servopos + 4 ;

    Serial.println(servopos);
    servo.write(servopos1);

  }} 
}

0 个答案:

没有答案