Chr*_*lez 5 c# android arduino unity-game-engine
当我尝试发送字符串并在 arduino 上验证它时,它不会传递任何 if 语句。device.send() 是我从 Tech Tweaking 的名为 Android & Microcontrollers / Bluetooth 的资产包中使用的方法。如何将字符串从 Unity C# Android 应用程序发送到 arduino 并传递 if 语句?
统一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);
}
Run Code Online (Sandbox Code Playgroud)
我能够解决它。请记住,我使用的是由 Tech Tweaking 提供的名为 Android 和微控制器/蓝牙的 Unity 资源,这就是 device.send() 的来源。
C# 代码
device.send(System.Text.Encoding.ASCII.GetBytes ("0,1" + '\n'));
Run Code Online (Sandbox Code Playgroud)
Arduino代码
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10, 9); // RX, TX
String data = "";
int LED = 12;
void setup() {
//Bluetooth module baud rate which I set using AT commands
Bluetooth.begin(38400);
//Serial baud rate which I use to communicate with the Serial Monitor in the Arduino IDE
Serial.begin(9600);
Serial.println("Waiting for command...");
pinMode(LED,OUTPUT);
}
void loop() {
if(Bluetooth.available() > 0) {
data = Bluetooth.readStringUntil('\n');
if (data == "0,1") {
digitalWrite(LED,1);
Serial.println("LED ON!");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2517 次 |
| 最近记录: |