2
votes

I'm trying to transmit data from my PC to an Arduino board. I'm able to see what the Arduino sends to the PC using Tera Term but I don't get any data from PC to Arduino. I've tried changing the pins and everything out there (even testing another bluetooth board). I'm using an Arduino Mega 2560.

Here is the code:

#include <AFMotor.h>
#include <NewPing.h>
#include <SoftwareSerial.h>

#define LED 52

#define RxD 17 
#define TxD 14 

SoftwareSerial blueToothSerial(RxD,TxD);

void setup() {
  blueToothSerial.begin(9600);
  blueToothSerial.println("Bluetooth On please press 1 or 0 blink LED ...");
  pinMode(LED, OUTPUT);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
}

byte BluetoothData;
void loop()
{
  if (blueToothSerial.available())
  {
    BluetoothData = blueToothSerial.read();
    if(BluetoothData=='1'){ // if number 1 pressed...
      digitalWrite(LED, 1);
      blueToothSerial.println("LED  On D13 ON ! ");
    }
    if (BluetoothData=='0'){ // if number 0 pressed...
      digitalWrite(LED, 0);
      blueToothSerial.println("LED  On D13 Off ! ");
    }
  }
  delay(100); // prepare for next data...
}
1

1 Answers

0
votes

According to this documentation for the SoftwareSerial library, pin 17 on the Arduino Mega 2560 does not support change interrupts, so it wont work with your Blutooth device. Instead choose an Rx pin listed in the documentation (e.g. 0, 11, 12, 13, 14, 15).

Also for the Arduino to receive bluetooth data the SoftwareSerial listen method should be used. Try modifying your code so that it uses this method e.g.

void loop()
{
  blueToothSerial.listen();
  if (blueToothSerial.available() > 0)
  {
    BluetoothData = blueToothSerial.read();
    if(BluetoothData=='1'){   // if number 1 pressed ....
      digitalWrite(LED, 1);
      blueToothSerial.println("LED  On D13 ON ! ");
    }
    if (BluetoothData=='0'){// if number 0 pressed ....
      digitalWrite(LED, 0);
      blueToothSerial.println("LED  On D13 Off ! ");
    }
  }
}

Note that when using listen() I don't think the call to delay() will be necessary.