質問

I am trying to make two-way bluetooth communication between Android and Arduino using Processing for Android. I have success transferring data from the Android to the Arduino with serial.begin(9600). And I have success transferring data from the Arduino to the Android by using SoftwareSerial in the Arduino program and bluetooth.begin(9600) in place of serial.begin(9600).

However, when trying to transfer data from the Android to the Arduino using bluetooth.x commands, it does not work. Here is the Arduino code:

  if (bluetooth.available()) // Wait until a character is received
  {
    char val = (char)bluetooth.read();
    //Serial.println(val);

    switch(val) // Perform an action depending on the command
    {
      case 'w'://turn the light on when a 'w' is received
      on();
      break;

      case 'q'://turn the light off when a 'q' is received
      off();
      break;

      //default://otherwise remain in the previous state
      //idle();
      break;
    }
  }

The on() and off() functions switch on and off an LED on the Arduino. As mentioned, this works when I'm using serial.x commands and not bluetooth.x commands. Also, I am using Ketai for Processing for Android. I am using Processing 2.0.1, Arduino 1.0.5, Android 2.3.6.

Here is the relevant beginning code:

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(0,1);  //TX 0, RX 1
役に立ちましたか?

解決

A little more code would be greatly appreciated...

Have you included something like that?

#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

EDIT:

That's similar to what I use. You first upload the code without the bluetooth wired and then wire the bluetooth. Then you can simply use Serial.doSomething() because you are using the same pins, you don't need #include <SoftwareSerial.h>. But you need to make sure that the baudrate is the same.

You can try this code to make sure it works fine:

void setup(){

    Serial.begin(9600); // or wathever your bluetooth module baudrate is

}

void loop(){

    Serial.println("Hello World!"); // to make sure it works.
    delay(500);

}

You should also make sure you you Arduino is connected to the computer via bluetooth.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top