This is the beginning of a project and I have run up against an issue. Right now, my app only does two things. Detects and connects to a specific SparkFun Bluetooth module/sheild, that is controlled by an Arduino Due. This works fine.
The other thing it is supposed to do(but is not), is to turn on/off an LED light that is being controlled by the Arduino as well. I'll layout the code below and some logs.
Arduino Sketch
char Incoming_value = 0; //Variable for storing Incoming_value
const int pwmPin = 13;
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data
transmission
pinMode(pwmPin, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0)
{
Incoming_value = Serial.read(); //Read incoming data, store it into variable Incoming_value
Serial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor
Serial.print("\n"); //New line
if(Incoming_value == '1') //Checks whether value of Incoming_value is equal to 1
digitalWrite(pwmPin, HIGH); //If value is 1 then LED turns ON
else if(Incoming_value == '0') //Checks whether value of Incoming_value is equal to 0
digitalWrite(pwmPin, LOW); //If value is 0 then LED turns OFF
}
}
I am not using an emulator. This is being tested on a phone. Below is the relevant bit of code. Reminder, this is written in Kotlin.
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...........
<snip>
...........
button_led_on.setOnClickListener { sendCommand("1")}
button_led_off.setOnClickListener { sendCommand("0")}
}
private fun sendCommand(input: String) {
if (bluetoothSocket != null) {
try {
bluetoothSocket!!.outputStream.write(input.toByteArray())
Log.i(LOGTAG, "Button clicked, info sent: $input")
} catch (e: IOException) {
e.printStackTrace()
}
}
}
As mentioned, everything compiles and runs just fine. Bluetooth portion works, Buttons all work and seem to be working as they should.
Clicking either the 'led on' or 'led off' buttons in the app show the following in the logs
Logcat
2019-09-24 09:47:15.891 9403-9403/com.example.pigcatcher D/MainActivity: Button clicked, info sent: 1
2019-09-24 09:47:24.975 9403-9403/com.example.pigcatcher D/MainActivity: Button clicked, info sent: 0
Nothing happens on the Arduino side. Looking at the Serial monitor on the Arduino, I only see the following when I click the 'led off' button in the app. Nothing shows up when clicking the 'led on' button.
09:47:24.994 -> 255
I feel like I don't have something configured correctly on the Arduino...but I have no clue what that might be. I have tried different pins as suggested in a few other post, but that does not work either.