当我尝试发送字符串并且在Arduino上进行验证时,它无法通过任何if语句。device.send()是我从名为Android&Microcontrollers / Bluetooth的资产包中使用的方法,由Tech Tweaking提供。我如何将来自Unity C# Android应用程序的字符串发送到我的Arduino并通过if语句?
Unity C# 代码
device.send(System.Text.Encoding.ASCII.GetBytes("0,0"));
Arduino 代码
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10, 9); // RX, TX
//int LED = 13; // the on-board LED
String Data; // the data received
int LED = 12; // LED on bread board
int height;
void setup() {
Bluetooth.begin(38400);
Serial.begin(38400);
Serial.println("Waiting for command...");
Bluetooth.println("Send 1 to turn on the LED. Send 0 to turn Off");
pinMode(LED,OUTPUT);
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(100); // 10 rpm
}
void loop() {
if (Bluetooth.available()){ //wait for data received
Data=Bluetooth.read();
if(Data == "0,1"){
digitalWrite(LED,1);
Serial.println("LED On!");
Bluetooth.println("LED On!");
Serial.println("Single coil steps");
myMotor->step(500, FORWARD, SINGLE);
}
else if(Data == "0,0"){
digitalWrite(LED,0);
Serial.println("LED Off!");
Bluetooth.println("LED Off!");
myMotor->step(500, BACKWARD, SINGLE);
}
else{;}
delay(100);
}
char
。字符数组也可能起作用。 - Markus Deibel