簡體   English   中英

如何讀取伺服位置並將其發送到Android以進行TextView?

[英]How to read servo position and send it to Android for TextView?

我發送命令通過Android移動伺服,然后伺服通過藍牙將位置發送到Android。 如何讀取伺服位置並將其發送到Android以獲取TextView?

這是我使用的代碼。 值100可以打開,值150可以關閉LED,因為1-90用於伺服。 首次連接Android時如何發布伺服位置?

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

Servo servo;

int bluetoothTx = 10;
int bluetoothRx = 11;
int data ;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() 

{
  pinMode(13, OUTPUT);
  servo.attach(9);
  bluetooth.begin(9600); 
  delay(100);
}


void loop() 
{
  if(bluetooth.available() > 0)  
  { 
    data = bluetooth.read();

    int servopos;
    int servopos1;
    servopos = data;
    servopos1 = data + 9;

    if(data == 100){            
    digitalWrite(13, HIGH); 
    }else if(data == 150){       
    digitalWrite(13, LOW);                               
    }else if (data > 100){ 
    Serial.println(servopos);
    servo.write(' ');
    }else if (data < 100){
    Serial.println(servopos);
    servo.write(servopos1);

}
  }
} 

您正在使用哪種伺服器?
如果是模擬伺服,則無法獲取伺服位置,但可以將伺服狀態保存為變量,然后將其提供給android應用。

同樣,存儲(模擬)伺服位置的正確值的最接近方法是將其發送到最小或最大位置(假設它不是連續自由旋轉的伺服),然后存儲對伺服位置所做的任何更改。
這很有經驗,但應該可以解決問題。

要從arduino獲取信息,您是否嘗試過SoftwareSerial print()方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM