Skip to content

Instantly share code, notes, and snippets.

@Zokol
Created January 22, 2014 21:24
Show Gist options
  • Save Zokol/8567597 to your computer and use it in GitHub Desktop.
Save Zokol/8567597 to your computer and use it in GitHub Desktop.
Sample program to demonstrate Bluetooth serial reading on Arduino.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 4
#define OLED_CS 3
#define OLED_CLK 52
#define OLED_MOSI 51
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
int led = 30;
int cursor_y = 0;
int cursor_x = 0;
boolean clear_dsp = false;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial to PC communication at 9600 bits per second:
Serial.begin(9600);
// initialize serial to Bluetooth at 9600 bits per second:
Serial1.begin(38400);
initUART();
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.println("HELLO");
display.display();
delay(10000);
display.clearDisplay();
}
// the loop routine runs over and over again forever:
void loop() {
if(Serial1.available()) {
if(clear_dsp){
display.clearDisplay();
clear_dsp = false;
}
digitalWrite(led, HIGH);
char bt_data = (char)Serial1.read();
Serial.write(bt_data);
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(cursor_y,cursor_x);
if(SSD1306_LCDWIDTH - 11 < cursor_y){
cursor_x = cursor_x + 16;
cursor_y = 0;
}
else{
cursor_y = cursor_y + 12;
}
display.println(bt_data);
display.display();
}
else {
display.display();
digitalWrite(led, LOW);
cursor_y = 0;
cursor_x = 0;
clear_dsp = true;
}
}
//Initializes UART in the BT module
void initUART() {
Serial1.write("AT+ORGL");
Serial1.write(0x0d);
Serial1.write(0x0a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment