Using namespace to separate two "functions" in one ino file

I am trying to use namespace to isolate two "functions" in one ino file. I am using namespace because there appears to be a conflict between "bluefruit.h" and <Adafruit_CircuitPlayground.h>. This seems to work, however I have three variables in the first function: BLEDfu, BLEDis, and BLEUart that are throwing errors when I try to use these variables in the body of the first namespace. FYI - when I move these variables inside of my Setup() routine, the variable no longer throw the errors but now the scope of these variables is inside the Setup() routine. I have copied the error messages after the code.

</>
namespace BLE_sketch
{
#include <bluefruit.h>

#define MAX_PRPH_CONNECTION 1
uint8_t connection_count = 0;

Blockquote - here are the three variables in question

// BLE Service
BLEDfu bledfu; // OTA DFU service
BLEDis bledis; // device information
BLEUart bleuart;

Blockquote

void setup()
{
Serial.begin(115200);
Bluefruit.autoConnLed(true);

// Initialize Bluefruit with max concurrent connections as Peripheral = 1, Central = 0
Bluefruit.begin(MAX_PRPH_CONNECTION, 0);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("Bluefruit52");

void connect_callback(uint16_t conn_handle);
Bluefruit.Periph.setConnectCallback(connect_callback);
void disconnect_callback(uint16_t conn_handle, uint8_t reason);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

bledfu.begin();

// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52");
bledis.begin();

// Configure and Start BLE Uart Service
bleuart.begin();

// Set up and start advertising
void startAdv();
startAdv();
}

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

// Include bleuart 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();

// Start Advertising

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
}

// print a string to Serial Uart and BLE Uart
void printAll(uint8_t* buf, int count)
{
if (Serial)
Serial.write(buf, count);

// Send to all connected centrals
for (uint8_t conn_hdl = 0; conn_hdl < MAX_PRPH_CONNECTION; conn_hdl++)
{
bleuart.write(conn_hdl, buf, count);
}
}

void loop()
{
uint8_t buf[64];
int count;

// Forward data from HW Serial to BLEUART
if (Serial)
while (Serial.available())
{
// Delay to wait for enough input
delay(2);
count = Serial.readBytes(buf, sizeof(buf) - 1);

  printAll(buf, count);
}

// Forward from BLEUART to HW Serial
while ( bleuart.available() )
{
count = bleuart.read(buf, sizeof(buf));
printAll(buf, count);

}
}

// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);

char central_name[32] = { 0 };
connection->getPeerName(central_name, sizeof(central_name));

Serial.print("Connected to ");
Serial.print(central_name);
Serial.println(" : bleuart_multi");
Serial.println("9,10");

connection_count++;
Serial.print("Connection count: ");
Serial.println(connection_count);

// Keep advertising if not reaching max
if (connection_count < MAX_PRPH_CONNECTION)
{
Serial.println("Keep advertising");
Bluefruit.Advertising.start(0);
}
} // connect_callback

void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;

connection_count--;
}
} //End of namespace BLE_sketch

namespace CircuitLED
{
#include <Adafruit_CircuitPlayground.h>

int BoardON = 0; // if on (1) normal routine is running. If off (0) then only the accelerameter
// is being checked. As soon as it returns a (1), process full routine.
// If accelerometer returns 2 or three, turn routine off (set BoardON to 0).
int Acc_input = 0;
//int i = 0;
void AccInterrupt(void);
unsigned long clock_1, clock_2; // These are used to determine of the Accelerometer has been tapped within 1 second to pause the circleLED processes
int minAngle; // minimum angle for this session
int maxAngle; // maximim angle for this session
int alertVisual; // Visual alert setting - 0 = none, 1 = pattern 1, 2 pattern 3
int alertAudio; // 1 = visual, 2 = sound, 3= visual and sound
double PlayNote[10] = {440, 493.88, 523.25, 587.33, 659.26, 698.46, 783.99, 880, 987.77, 1046.5};

void setup() {
// put your setup code here, to run once:
CircuitPlayground.begin();
Serial.begin(9600);
CircuitPlayground.setBrightness(3);
CircuitPlayground.setPixelColor(1, 0, 0, 0);
delay(1000);
CircuitPlayground.setPixelColor(0, 0, 0, 0);
delay(1000);
Serial.println("LED Test");

clock_1 = 0;
clock_2 = 0;
CircuitPlayground.setAccelRange(LIS3DH_RANGE_4_G);
attachInterrupt(digitalPinToInterrupt(27), AccInterrupt, RISING);
CircuitPlayground.setAccelTap(1, 100);
}

void loop() {
int rate = 100;

int AND_Request(void);
int bnum = (int)AND_Request();

void circleLED(int, int, int, int, int);

circleLED(bnum, rate, 125, 0, 0);
circleLED(2, rate, 125, 0, 0);
circleLED(3, rate, 125, 0, 0);
circleLED(4, rate, 125, 0, 0);

}

void circleLED(int d, int rate, int r, int g, int b)
{
int lednum = 0;

if (BoardON == 1)

switch (d) {
  case 1:
    if (BoardON == 1)
      //circle the leds displaying each pixel individually
      for (lednum = 0; lednum < 10; lednum++)
      {
        if (lednum != 0)
          CircuitPlayground.setPixelColor(lednum - 1, 0, 0, 0);
        CircuitPlayground.setPixelColor(lednum, r, g, b);
        CircuitPlayground.playTone(PlayNote[lednum], 50);
        delay(rate);
      }
    break;

}

if (!BoardON)
CircuitPlayground.clearPixels();
}

void AccInterrupt()
{

const unsigned long period = 1000;

if (!BoardON) // If the board is off...
{
clock_1 = clock_2 = 0; // Reset the clocks
BoardON = 1; // turn it on
}
else
// Board is on
if (clock_1) // Already have single hit
{
clock_2 = millis();
if ((clock_2 - clock_1) > period) // was there more than 1 second between taps?
{ clock_1 = clock_2; // make clock_1 = new reading: clock_2
clock_2 = 0; // leave the process running
}
else
{
BoardON = 0; // Turn processes off
CircuitPlayground.clearPixels();// Turn off all the lights
}
}
else
clock_1 = millis();
return;
}
} // End of namespace

</> Error:
Arduino: 1.8.13 (Windows 10), Board: "Adafruit Circuit Playground Bluefruit, 0.3.2 SoftDevice s140 6.1.1, Level 0 (Release)"

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: sketch\BLE_LE_partner_sketch_V2.ino.cpp.o: in function `__static_initialization_and_destruction_0':

C:\Users\mjhol\Documents\Arduino\BLE_LE_partner_sketch_V2/BLE_LE_partner_sketch_V2.ino:10: undefined reference to `BLE_sketch::BLEDfu::BLEDfu()'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\Documents\Arduino\BLE_LE_partner_sketch_V2/BLE_LE_partner_sketch_V2.ino:11: undefined reference to `BLE_sketch::BLEDis::BLEDis()'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\Documents\Arduino\BLE_LE_partner_sketch_V2/BLE_LE_partner_sketch_V2.ino:12: undefined reference to `BLE_sketch::BLEUart::BLEUart(unsigned short)'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\Documents\Arduino\BLE_LE_partner_sketch_V2/BLE_LE_partner_sketch_V2.ino:12: undefined reference to `BLE_sketch::BLEUart::~BLEUart()'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: sketch\BLE_LE_partner_sketch_V2.ino.cpp.o: in function `BLE_sketch::BLEDfu::~BLEDfu()':

C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\libraries\Bluefruit52Lib\src/services/BLEDfu.h:44: undefined reference to `BLE_sketch::BLECharacteristic::~BLECharacteristic()'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\libraries\Bluefruit52Lib\src/services/BLEDfu.h:44: undefined reference to `vtable for BLE_sketch::BLEDfu'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Temp\arduino_build_276326/..\arduino_cache_787534\core\core_adafruit_nrf52_cplaynrf52840_softdevice_s140v6,debug_l0_9d00e8c1b7e19a0cd42199da1a8b8c93.a(main.cpp.o): in function `loop_task(void*)':

C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:52: undefined reference to `setup'

Multiple libraries were found for "Adafruit_CircuitPlayground.h"

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: undefined reference to `loop'

Used: C:\Users\mjhol\Documents\Arduino\libraries\arduino_539478

Not used: C:\Program Files (x86)\Arduino\libraries\Adafruit_Circuit_Playground

Not used: C:\Users\mjhol\Documents\Arduino\libraries\arduino_512257

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: undefined reference to `loop'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: undefined reference to `loop'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: undefined reference to `loop'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: undefined reference to `loop'

c:/users/mjhol/appdata/local/arduino15/packages/adafruit/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\mjhol\AppData\Local\Temp\arduino_build_276326/..\arduino_cache_787534\core\core_adafruit_nrf52_cplaynrf52840_softdevice_s140v6,debug_l0_9d00e8c1b7e19a0cd42199da1a8b8c93.a(main.cpp.o):C:\Users\mjhol\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\0.20.5\cores\nRF5/main.cpp:61: more undefined references to `loop' follow

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Adafruit Circuit Playground Bluefruit.
</>

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

First of all, thanks for responding. I don’t use this forum very often but it has been helpful in the past. Regarding “How to get the best...”, I’m a bit confused. I have provided (I thought) a good technical request. I put my source code in code blocks (</>) and identified the specific area of the problem. I have included the error output from the compile, again in a code block (</> again). In looking at it again, I see two code segments that are slightly shaded. Is this the problem or should the entire thing be shaded? I will remove this post but would really appreciate a better understanding on what was wrong with my post.

Thanks again for the feedback!

Mark Holten

Hi — look carefully at the output, you have not...

I will remove this post

--> don't, just fix it.

to fix: edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)
you'll probably need to remove the embedded block quotes you added

———

PS/ I'm unsure what you are trying to do, seems you have two loops and two setup in your .ino

Thanks again, I see what I did wrong. I have used the </> to show where the code is as well as another </> around the error messages from the compile. Regarding what I'm trying to do...I have developed a small app on an Android phone. This sends data via BLEUart to the Arduino. I want to use the data to determine and execute a LED sequence then send data back to the Arduino. So I know now that what I as intending to do with the namespace will not work. But first priority is to figure out how to have <bluefruit.h> and <Adafruit_CircuitPlayground.h> coexisting. Should I delete this post and open a new once since the original "problem" is no longer the problem?

Yes a new post probably would be more appropriate

Make sure you mention which hardware you have (is it a CircuitPlayground hardware ?)

Yes it is. Thanks again!

Mark

Would you happen to know where I can find BLEUart examples for the Arduino?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.