如何通过蓝牙从Unity c#android应用程序发送字符串到Arduino?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何通过蓝牙从Unity c#android应用程序发送字符串到Arduino?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2063字,纯文字阅读大概需要3分钟。
内容图文
![如何通过蓝牙从Unity c#android应用程序发送字符串到Arduino?](/upload/InfoBanner/zyjiaocheng/717/b85c27ec7ae6428a8d7dcae0fbb20254.jpg)
当我尝试发送我的字符串并在arduino上验证它时,它不会传递任何if语句. device.send()是我在一个名为Android&的资产包中使用的方法.微调控制器/蓝牙技术调整.如何从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);
}
解决方法:
我能够解决它.请记住,我使用的是名为Android& amp;的Un??ity资产.微控制器/蓝牙技术调整,这是device.send()的来源.
C#代码
device.send(System.Text.Encoding.ASCII.GetBytes ("0,1" + '\n'));
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!");
}
}
内容总结
以上是互联网集市为您收集整理的如何通过蓝牙从Unity c#android应用程序发送字符串到Arduino?全部内容,希望文章能够帮你解决如何通过蓝牙从Unity c#android应用程序发送字符串到Arduino?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。