4

When I try to send my string and I verify it on arduino it won't pass any of the if statements. device.send() is a method that i am using from an asset package called Android & Microcontrollers / Bluetooth by Tech Tweaking. How can I send a string from my Unity C# Android app to my arduino and pass the if statements?

Unity C# code

device.send (System.Text.Encoding.ASCII.GetBytes("0,0"));

Arduino code

#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);
}
  • Due to the bluetooth transfer your string might contain a not-printable character like zero termination '\0'. Try trimming the string before checking it or use something like Data.Contains("0,1"). – Markus Deibel Jun 25 '18 at 19:38
  • when i check Data in the Serial.print(Data) all i see is a bunch of question marks that continue producing infinitely. So i don't think Data.Contains("0,1") would work – Christian Velez Jun 25 '18 at 20:06
  • I need to understand if the way that I am sending the string is the correct way or if i need an array of bytes or something else, then I can proceed to the arduino section – Christian Velez Jun 25 '18 at 20:08
  • According to [this answer](https://stackoverflow.com/a/19720372/2137237) you might need to send separate `char`s. A char array might work as well. – Markus Deibel Jun 25 '18 at 20:24

1 Answers1

1

I was able to solve it. Keep in mind that I am using a Unity Asset called Android & Microcontrollers / Bluetooth by Tech Tweaking which is where device.send() comes from.

C# code

device.send(System.Text.Encoding.ASCII.GetBytes ("0,1" + '\n'));

Arduino code

#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!"); 
 }
}