Last active
April 22, 2016 08:03
-
-
Save bauerjj/b422e07f74e611e4df3831678bf5ee4b to your computer and use it in GitHub Desktop.
Arduino hc-06 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple routine that performs the following: | |
* 1. Blink the on-board status LED every 500ms | |
* 2. Configures the software UART on pins 2 and 4 (RX,TX) | |
* 3. Continuously sends the string "hello" to the bluetooth module every 500ms | |
* 4. If it receives a character from bluetooth, it echos it back | |
* | |
* @author mcuhq.com | |
*/ | |
#include <SoftwareSerial.h> // use the software uart | |
SoftwareSerial bluetooth(2, 4); // RX, TX | |
unsigned long previousMillis = 0; // will store last time LED was updated | |
const long interval = 500; // interval at which to blink (milliseconds) | |
int ledState = LOW; // ledState used to set the LED | |
void setup() { | |
pinMode(13, OUTPUT); // for LED status | |
bluetooth.begin(9600); // start the bluetooth uart at 9600 which is its default | |
delay(200); // wait for voltage stabilize | |
bluetooth.print("AT+NAMEmcuhq.com"); // place your name in here to configure the bluetooth name. | |
// will require reboot for settings to take affect. | |
delay(3000); // wait for settings to take affect. | |
} | |
void loop() { | |
if (bluetooth.available()) { // check if anything in UART buffer | |
bluetooth.write(bluetooth.read()); // if so, echo it back! | |
} | |
// Blink LED routine from https://www.arduino.cc/en/tutorial/blink | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
// if the LED is off turn it on and vice-versa: | |
if (ledState == LOW) { | |
ledState = HIGH; | |
} else { | |
bluetooth.println("Arduino bluetooth"); // continously send this | |
ledState = LOW; | |
} | |
// set the LED with the ledState of the variable: | |
digitalWrite(13, ledState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment