Arduino - Android通信SeekBar价值

时间:2016-04-28 12:10:36

标签: android bluetooth arduino seekbar dataformat

我有一个通过蓝牙与Arduino通信的应用程序。我想用seekbar改变直流电机的速度,没有任何关于通信的问题,问题是格式我认为,因为当我移动条形图时,我检查Arduino串口屏幕的结果,并看到不同的符号! “#'$)(*<:并且其中一些是普通字符,如A,B,C ......我只需要0到255之间的整数找到并尝试these answer但它没有帮助

这是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 Part

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);
}

1 个答案:

答案 0 :(得分:1)

我写了一个类来在Android和Arduino之间建立蓝牙连接。我的代码在github上:

<强> https://github.com/omaflak/Bluetooth-Android

我还在我的博客上写了一个教程,你可能想看看它,有Arduino的代码和一切:

<强> https://causeyourestuck.io/2015/12/14/communication-between-android-and-hc-06-module/

祝你好运!