element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Cant receive data from arduino via bluetooth
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 8 replies
  • Subscribers 392 subscribers
  • Views 1392 views
  • Users 0 members are here
Related

Cant receive data from arduino via bluetooth

happyforever
happyforever over 7 years ago

I can receive data from android but cant send data to android, please help me to solve. The below is my code.

Board I used is arduino uno and external module is HC-06.


#include <SoftwareSerial.h>

 

int bluetoothTx = 13;  // TX-O pin of bluetooth mate, Arduino D2

int bluetoothRx = 14;  // RX-I pin of bluetooth mate, Arduino D3

 

char incomingByte; // incoming data

int sensorPin = A0;  

int ledPin = 11;      // select the pin for the LED

int ledPin2 = 12;

int sensorValue = 0;  // variable to store the value coming from the sensor

int rTrigPin = 6;

int rEchoPin = 7;

int trigPin = 9;

int echoPin = 10;

int lTrigPin = 4;

int lEchoPin = 5;

 

// defines variables

long rDuration;

int rDistance;

long duration;

int distance;

long lDuration;

int lDistance;

 

 

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

 

void setup() {

 

  Serial.begin(115200);

 

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps

  //bluetooth.print("$");  // Print three times individually

  //bluetooth.print("$");

  //bluetooth.print("$");  // Enter command mode

  //delay(100);  // Short delay, wait for the Mate to send back CMD

  //bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity

  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably

  bluetooth.begin(9600);  // Start bluetooth serial at 9600

 

  pinMode(rTrigPin, OUTPUT); // Sets the rTrigPin as an Output

  pinMode(rEchoPin, INPUT); // Sets the rEchoPin as an Input

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  pinMode(lTrigPin, OUTPUT); // Sets the lTrigPin as an Output

  pinMode(lEchoPin, INPUT); // Sets the lEchoPin as an Input

 

 

  // declare the ledPin as an OUTPUT:

  pinMode(ledPin, OUTPUT);

  pinMode(ledPin2, OUTPUT);

}

 

/*void loop() {

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin);

  // turn the ledPin on

  digitalWrite(ledPin, LOW);

  // stop the program for <sensorValue> milliseconds:

  delay(sensorValue);

  // turn the ledPin off:

  digitalWrite(ledPin, HIGH);

  // stop the program for for <sensorValue> milliseconds:

  delay(sensorValue);

  }*/

 

void loop()

{  

        digitalWrite(rTrigPin, LOW);

        delayMicroseconds(2);

        digitalWrite(rTrigPin, HIGH);

        delayMicroseconds(10);

        digitalWrite(rTrigPin, LOW);

        rDuration = pulseIn(rEchoPin, HIGH);

        rDistance = (rDuration / 2) / 29.1;

    

        digitalWrite(trigPin, LOW);

        delayMicroseconds(2);

        digitalWrite(trigPin, HIGH);

        delayMicroseconds(10);

        digitalWrite(trigPin, LOW);

        duration = pulseIn(echoPin, HIGH);

        distance = (duration / 2) / 29.1;

    

        digitalWrite(lTrigPin, LOW);

        delayMicroseconds(2);

        digitalWrite(lTrigPin, HIGH);

        delayMicroseconds(10);

        digitalWrite(lTrigPin, LOW);

        lDuration = pulseIn(lEchoPin, HIGH);

        lDistance = (lDuration / 2) / 29.1;

    

        sensorValue = analogRead(sensorPin);

 

 

          if (rDistance <= 221 || distance <= 221 || lDistance <= 221)

        {

          if (sensorValue <= 140)

          {

            digitalWrite(ledPin, LOW);

            digitalWrite(ledPin2, LOW);

          }

          else

          {

            digitalWrite(ledPin, HIGH);

            digitalWrite(ledPin2, HIGH);

          }

          

        }

        else if (rDistance > 221 || distance > 221 || lDistance > 221)

        {

          if (sensorValue <= 140)

          {

            digitalWrite(ledPin, LOW);

            digitalWrite(ledPin2, LOW);

          }

          else

          {

            digitalWrite(ledPin, HIGH);

            digitalWrite(ledPin2, HIGH);

          }

        }

 

  if (bluetooth.available()) // If the bluetooth sent any characters

  {

    // Send any characters the bluetooth prints to the serial monitor

  

    Serial.println((char)bluetooth.read());

    incomingByte = bluetooth.read();

 

 

  

    if(incomingByte == '1') {

 

 

         Serial.println("Music is pause and now you are in dangerous situation.");  // print message

         //bluetooth.print("1");

       

         //delay(1000);

    }

 

    if(incomingByte == '0') {

    

          digitalWrite(ledPin, HIGH);

          digitalWrite(ledPin2, HIGH);

          Serial.println("Alert signal OFF and music would not pause when in dangerous situation.");  // print message

          //bluetooth.print("0");

          //delay(1000);

          //break;

        

    }

  }

 

 

  if (Serial.available()>0) // If stuff was typed in the serial monitor

  {

    int bytes=Serial.available();

    //Serial.readBytes(buffer, startPosition, bytes);

 

    bluetooth.print((char)Serial.read());

  

  }

 

}



  • Sign in to reply
  • Cancel

Top Replies

  • gadget.iom
    gadget.iom over 7 years ago +2
    Hello happyforever I have replied to your other post here: Sending serial data from Arduino to Android via Bluetooth - NOT ANSWERED Yet
  • neilk
    neilk over 7 years ago +2
    Hi happyforever It's often a good idea to test out various components of a program separately and when they all work, combine them together slowly, making sure each addition still works. Your question…
  • neilk
    neilk over 7 years ago in reply to gadget.iom +1
    Hi gadget.iom - sorry Neil
Parents
  • gadget.iom
    gadget.iom over 7 years ago

    Hello happyforever

     

    I have replied to your other post here:

    Sending serial data from Arduino to Android via Bluetooth - NOT ANSWERED Yet

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • gadget.iom
    gadget.iom over 7 years ago

    Hello happyforever

     

    I have replied to your other post here:

    Sending serial data from Arduino to Android via Bluetooth - NOT ANSWERED Yet

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube