I have written the following code below using the SoftwareSerial.h library
void loop(){
//BT LED READ
if(BTSerial.available())
{
int c = BTSerial.read();
Serial.println(c);
Serial.print("\n");
if (c == 1)
digitalWrite(ledpin, HIGH);
else if (c == 0)
digitalWrite(ledpin, LOW);
//BUTTON PRESS CODE
buttonState = digitalRead(inputPin);
if(buttonState == HIGH)
{
digitalWrite(outputLed, HIGH);
BTSerial.print(22);
}
else
{
digitalWrite(outputLed, LOW);
}
}
//Serial.println(digitalRead(state));
}
To accompany my code, I have written a mobile application in Xamarin studio. What my code does, is that it accepts a call from the application (either 0 or 1 byte) which makes it turn on an LED.
Secondly, what I'm trying to accomplish, and yet struggle with, is that I want the Arduino to notify my application (or write... I'm really not sure what this library allows me to do since I have a hard time finding some elaborate documentation) when a button on my breadboard is pressed - it turns on an LED too when pressed.
I realized that if I encapsulate the code for the button press inside BTSerial.available()
it doesn't work, meaning that it doesn't send any data to my application, nor does the LED turn on when I press the button. - It does though still accept parameters being sent to it.
Vise versa, if I don't encapsulate my button code inside the BTSerial.available()
I can write data to my phone, but my phone cannot send data to the BT device.
What is going on here, and why can't I access the two functionalities simultaneous, and what is a potential fix?