Arduino蓝牙小车的代码
时间: 2024-06-17 08:04:07 浏览: 180
蓝牙小车代码
Arduino蓝牙小车的代码主要分为两部分:Arduino板子的代码和手机APP的控制代码。
Arduino板子的代码主要负责读取蓝牙信号,并根据信号来控制小车的动作,以下是一个简单的示例代码:
```
int leftMotorPin1 = 8; //左电机引脚1
int leftMotorPin2 = 9; //左电机引脚2
int rightMotorPin1 = 10; //右电机引脚1
int rightMotorPin2 = 11; //右电机引脚2
void setup() {
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
Serial.begin(9600); //设置串口通信波特率为9600
}
void loop() {
if (Serial.available() > 0) { //如果串口接收到数据
int command = Serial.read(); //读取数据
switch (command) {
case 'F': //前进
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
break;
case 'B': //后退
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
break;
case 'L': //左转
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
break;
case 'R': //右转
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
break;
case 'S': //停止
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
break;
default:
break;
}
}
}
```
手机APP的代码主要负责通过蓝牙与Arduino板子进行通信,并发送控制指令。以下是一个简单的Android APP示例代码:
```
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "不支持蓝牙", Toast.LENGTH_SHORT).show();
finish();
return;
}
if (!bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
return;
}
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("HC-06")) { //HC-06是蓝牙模块的名称,需要根据实际情况修改
try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
} catch (IOException e) {
Toast.makeText(this, "连接失败", Toast.LENGTH_SHORT).show();
finish();
return;
}
break;
}
}
Button forwardButton = findViewById(R.id.forward_button);
forwardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCommand('F');
}
});
Button backwardButton = findViewById(R.id.backward_button);
backwardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCommand('B');
}
});
Button leftButton = findViewById(R.id.left_button);
leftButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCommand('L');
}
});
Button rightButton = findViewById(R.id.right_button);
rightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCommand('R');
}
});
Button stopButton = findViewById(R.id.stop_button);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCommand('S');
}
});
}
private void sendCommand(char command) {
try {
outputStream.write(command);
outputStream.flush();
} catch (IOException e) {
Toast.makeText(this, "发送指令失败", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (bluetoothSocket != null) {
bluetoothSocket.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文