/*
== MASTER CODE ==
*/
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ledPin 9
int state = 0;
int Vry = 0;
int Vrx = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop() {
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
// Controlling the LED
/*if (state == '1') {
digitalWrite(ledPin, HIGH); // LED ON
state = 0;
}
else if (state == '0') {
digitalWrite(ledPin, LOW); // LED ON
state = 0;
}
*/
// Reading the potentiometer
//Vry = analogRead(A0);
/*
Vrx = analogRead(A1);
int VrxMapped = map(Vrx, 0, 1023, 0, 255);
//int Vry_mapped = map(Vrx, 0, 1023, 0, 255);
//int Vrx_mapped = map(Vry, 0, 1023, 0, 255);
Serial.print("Vrx");
Serial.println(VrxMapped);
//Serial.print("Vry");
//Serial.println(Vry);
*/
Vrx = analogRead(A1);
BTSerial.write(Vrx);
Serial.print("Vrx: ");
Serial.println(Vrx);
//BTSerial.write(Vry);
delay(2000);
}
,从代码如下,
/*
== SLAVE CODE ==
*/
#include <SoftwareSerial.h>
#define button 8
SoftwareSerial BTSerial(5, 3); // RX | TX
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
int enB = 11;
int in3 = 7;
int in4 = 6;
int jx = A0;
int jy = A1;
int mx = 0; //right motor
int my = 0; //left motor
int state = 0;
int i = 0;
int buttonState = 0;
int ledPin = 13;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
BTSerial.begin(38400); // HC-05 default speed
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
//my = analogRead(BTSerial.read());
// mx = analogRead(BTSerial.read());
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
int state = BTSerial.read(); // Reads the data from the serial port
//Serial.print('y');
//Serial.println(my+100,DEC);
Serial.print('x');
Serial.println(state);
}
if (i == 2){
i=0;
}
/*int mapx = map(mx,0,1023,0,255);
int mapy = map(my,0,1023,0,255);
if (mapx>127)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA,mapx);
}
else
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA,127-mapx);
}
if (mapy>127)
{
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB,mapy);
}
else
{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB,127-mapy);
}
delay(1000);
*/
/*Serial.println(state);
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
if (state > 120){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
}
else{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
}
// Controlling the servo motor
// Reading the button
buttonState = digitalRead(button);
if (buttonState == HIGH) {
BTSerial.write('1'); // Sends '1' to the master to turn on LED
}
else {
BTSerial.write('0');
}
*/
delay(2000);
}
这里的问题如下, 如果我从主站发送1,我在奴隶端获得130,我不知道串行通信如何工作以及如何通过蓝牙设备接收数据!
答案 0 :(得分:0)
读取功能中的蓝牙序列返回字符。