0
我有一個通過藍牙與Arduino進行通信的應用程序。我想用seekbar改變直流電機的速度,通訊沒有任何問題,問題是我想的格式,因爲當我移動條形碼時,我檢查Arduino串行端口屏幕的結果,並看到不同的符號! 「#」$)(* <:其中有些是正常的字符如A,B,C ......我只需要找到0-255並試圖these answer,但它並沒有幫助之間的整數Arduino - Android通訊SeekBar值
這是Android的一部分
seekbar = (SeekBar)findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int speed= 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
speed = progress;
if(btSocket!=null){
try {
btSocket.getOutputStream().write(speed);
} catch (IOException e){
msg("Error");
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
這是Arduino的部分
int pwm_a = 3;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;
char incomingByte; //incoming data for directions
int speed; // incoming data for speed
void setup()
{
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
}
void loop()
{
if(Serial.available() > 0){ //if the data came
incomingByte = Serial.read();
speed = Serial.read();
if (incomingByte == 'F'){
forward();}
else if (incomingByte == 'B'){
back();}
if (incomingByte == 'R'){
right();}
if (incomingByte == 'L'){
left();}
if (incomingByte == 'S'){
stop();
}
}
}
void forward()
{ digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, speed);
analogWrite(pwm_b, speed);
}
void back()
{ digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, speed);
analogWrite(pwm_b, speed);
}
void right()
{ digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, speed);
analogWrite(pwm_b, speed);
}
void left()
{ digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, speed);
analogWrite(pwm_b, speed);
}
void stop()
{ digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
}
通信以字節完成我想.. –
也是你在兩端使用類似的編碼類型? –
是的,通常在發送方向時我使用btSocket.getOutputStream()。write(「F」.getBytes());同樣我試過btSocket.getOutputStream()。write((byte)speed);但沒有任何改變。還試過String outputData = String.valueOf(speed); btSocket.getOutputStream.write(speed.getBytes()); – ysfcyln