Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

FLORA BLUEFRUIT MODULE COMMUNICATION
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

FLORA BLUEFRUIT MODULE COMMUNICATION

by Peter009 on Sun Apr 04, 2021 4:45 pm

Hello, I am very new to wearables and microcontrollers in general.

I am trying to create a simple sketch where I use the Flora Bluefruit module with the Flora board to turn on and off the built-in LED on the Flora board by sending a 1 or a 0 respectively through Bluetooth using the Bluefruit LE Connect app for Android.

I am able to receive and send information both ways, however when I try to "act" on that information (meaning that I want to turn on and off the LED instead of just showing the data on the serial monitor) nothing happens, and I cannot figure out why it is that it will not work. Can you please guide me in the right direction?

The following is my code for my sketch, it is very rough because I have only copied and pasted the code I needed from one of the examples:
Code: Select all | TOGGLE FULL SIZE
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_UART.h"
#define BLUEFRUIT_HWSERIAL_NAME      Serial1
#define BLUEFRUIT_UART_MODE_PIN        12

#define LED_PIN 7

Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);

void setup() {
  // put your setup code here, to run once:

  while (!Serial);  // required for Flora & Micro
  delay(500);

  pinMode(7, OUTPUT);
  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example"));
  Serial.println(F("------------------------------------------------"));

   if ( !ble.begin(true) )
  {
    Serial.println(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

     Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ){
      Serial.println(F("Couldn't factory reset"));
    }
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();
  ble.verbose(false);  // debug info is a little annoying after this point!
  ble.setMode(BLUEFRUIT_MODE_DATA);
 
}

void loop() {
  // put your main code here, to run repeatedly:

 while ( ble.available() )
  {
    int c = ble.read();

    if((char)c == 1) {
      digitalWrite(LED_PIN, HIGH);
    } else {
      digitalWrite(LED_PIN, LOW);
    }

    Serial.println((char)c);

}
 
}

Peter009
 
Posts: 15
Joined: Fri Sep 22, 2017 5:04 pm

Re: FLORA BLUEFRUIT MODULE COMMUNICATION

by dastels on Mon Apr 05, 2021 11:36 am

If printing the input character works, i.e. prints "0" or "1", then all you need is to put the character you compare with in single quotes.

Replace:
Code: Select all | TOGGLE FULL SIZE
if((char)c == 1) {

with:
Code: Select all | TOGGLE FULL SIZE
if((char)c == '1') {


Dave

dastels
 
Posts: 6080
Joined: Tue Oct 20, 2015 3:22 pm

Re: FLORA BLUEFRUIT MODULE COMMUNICATION

by Peter009 on Tue Apr 06, 2021 10:03 pm

Wrapping the character with single quotes did the trick, it's working as expected now.

Thank you for your input!

Peter009
 
Posts: 15
Joined: Fri Sep 22, 2017 5:04 pm

Re: FLORA BLUEFRUIT MODULE COMMUNICATION

by dastels on Wed Apr 07, 2021 10:14 am

Cool. In C/C++ 1 is the integer with value 1, whereas '1' is the character 1 which is a byte with the value of 49 (assuming ASCII encoding).

Dave

dastels
 
Posts: 6080
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.