1
votes

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.

2

2 Answers

0
votes

The sketch looks good and the Android logic looks good. Assuming your connections are solid between the Bluetooth module and the Arduino, and you are paired, I would be troubleshooting what exactly you are sending through the Bluetooth socket. I found some source code for a project exactly like yours, albeit in Java: https://github.com/Mayoogh/Arduino-Bluetooth-Basic/blob/master/LED-master/app/src/main/java/com/led_on_off/led/ledControl.java

I would verify that you're doing the Bluetooth connection properly, and that the data you send is correct. I noticed what you log is different than what you actually send (on Android). I also noticed something about setting the SPP UUID, not sure what that is but it sounds important.

0
votes

After much hair pulling. I finally figured out that the issue was not with my Kotling code or the Arduino code. The issue was the Bluetooth shield I was using.

I do not know why, but replacing it with another and making no code changes, it all started working as expected.