Skip to content

Instantly share code, notes, and snippets.

@bauerjj
Created April 25, 2016 05:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bauerjj/424442ed08647a26652f9fe0736bfa28 to your computer and use it in GitHub Desktop.
Save bauerjj/424442ed08647a26652f9fe0736bfa28 to your computer and use it in GitHub Desktop.
Arduino code to accompany the tutorial found here: http://mcuhq.com/27/simple-android-bluetooth-application-with-arduino-example
/**
* Simple routine that performs the following:
* 1. Configures the software UART on pins 2 and 4 (RX,TX)
* 2. Increments a 32-bit variable every 500ms
* 4. If it receives a '1' character from bluetooth, it toggles an LED
*
* @author Justin Bauer - mcuhq.com
* @date 4.24.2016
*/
#include <SoftwareSerial.h> // use the software uart
SoftwareSerial bluetooth(2, 4); // RX, TX
unsigned long previousMillis = 0; // will store last time
const long interval = 500; // interval at which to delay
static uint32_t tmp; // increment this
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
if(bluetooth.read() == '1'){ // did we receive this character?
digitalWrite(13,!digitalRead(13)); // if so, toggle the onboard LED
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
bluetooth.print(tmp++); // print this to bluetooth module
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment