We’ve already connected to our Bluefruit feather with Bluetooth before, but when I wrote the sketches that I applied to it, it overwrote the code that allows it to turn on the Bluetooth function. So, today my goal was to get the Bluetooth working.

#include <bluefruit.h>

BLEUart bleuart;

// Function prototypes for packetparser.cpp
uint8_t readPacket (BLEUart *ble_uart, uint16_t timeout);
float parsefloat (uint8_t *buffer);
void printHex (const uint8_t * data, const uint32_t numBytes);

// Packet buffer
extern uint8_t packetbuffer[];

int brightness = 0; // how bright the LED is
int fadeAmount = 10; // how many points to fade the LED by
int highOrNot = 1; // Just a number.
void setup(void) {
Serial.begin(115200);
Serial.println(F(“——————————————-“));

Bluefruit.begin();
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
Bluefruit.setName(“Bluefruit_AlaskaLinuxUser”);

// Configure and start the BLE Uart service
bleuart.begin();

// Set up and start advertising
startAdv();

pinMode(19, OUTPUT); //19 is blue LED, 17 is red LED.
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
}

void startAdv(void) {
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();

// Include the BLE UART (AKA ‘NUS’) 128-bit UUID
Bluefruit.Advertising.addService(bleuart);

// Secondary Scan Response packet (optional)
// Since there is no room for ‘Name’ in Advertising packet
Bluefruit.ScanResponse.addName();

Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don’t stop advertising after n seconds
}

void loop() {

analogWrite(19, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 50) {
fadeAmount = -fadeAmount;
}
// wait for 500 milliseconds to see the dimming effect
delay(1000);
// To print the brightness // Serial.println(brightness);

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 – 1023) to a voltage (0 – 3.6V):
float voltage = sensorValue * (3.6 / 1023.0);
// print out the value you read:
if (voltage >= 0.2) {
Serial.print(“Pin A01: “);Serial.println(voltage);
} else {
Serial.println(“No voltage on pin A01.”);
}

if (highOrNot > 0) {
digitalWrite(11, HIGH);
highOrNot = highOrNot -1;
} else {
digitalWrite(11, LOW);
highOrNot = highOrNot +2;
}
}

All of this came, more or less, straight from the guide on setup. However, I did notice that it had the generic name, so I changed the Bluefruit.setName line to something a little more “me”. The rest of the sketch is the same information as I had before.

Here you can see it show up in the Bluefruit LE app on my phone:

Now I am working on sending data to the board. And that I accomplished like so:

void loop(void) {

analogWrite(19, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 50) {
fadeAmount = -fadeAmount;
}
// wait for 500 milliseconds to see the dimming effect
delay(1000);
// To print the brightness // Serial.println(brightness);

// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 – 1023) to a voltage (0 – 3.6V):
float voltage = sensorValue * (3.6 / 1023.0);
// print out the value you read:
if (voltage >= 0.2) {
Serial.print(“Pin A01: “);Serial.println(voltage);
} else {
Serial.println(“No voltage on pin A01.”);
}

if (highOrNot > 0) {
digitalWrite(11, HIGH);
highOrNot = highOrNot -1;
} else {
digitalWrite(11, LOW);
highOrNot = highOrNot +2;
}

// Wait for new data to arrive
uint8_t len = readPacket(&bleuart, 500);
if (len == 0) return;

// Buttons
if (packetbuffer[1] == ‘B’) {
uint8_t buttnum = packetbuffer[2] – ‘0’;
boolean pressed = packetbuffer[3] – ‘0’;
Serial.print (“Button “); Serial.print(buttnum);
if (pressed) {
Serial.println(” pressed”);
analogWrite(17, 50);
} else {
Serial.println(” released”);
analogWrite(17, 0);
}
} else {
Serial.println(packetbuffer[1]);
}
}

So now, when I open the Bluefruit app, and select my “Bluefruit_AlaskaLinuxUser” board, I can choose the controller option. Essentially, I hijacked the controller button portion of the app, and when you press a button on the controller it now lights up the red LED light! When you release the button, the light goes out! Great!

Linux – keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *