Issues with the nRF52840 and a neopixel strip

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

Here is my code so far

Code: Select all

#include <Adafruit_NeoPixel.h>
#include <Adafruit_BluefruitLE_nRF52.h>
#include <Adafruit_BANNED.h>

#define PIXEL_PIN    10
#define PIXEL_COUNT  60

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_BluefruitLE_nRF52 ble;

void setup() {
  pixels.begin();
  ble.begin();
  ble.setConnectCallback(connect_callback);
  ble.setDisconnectCallback(disconnect_callback);
  ble.setBleGattRxCallback(rx_callback);
}

void loop() {
  ble.poll();
}

void connect_callback(uint16_t conn_handle) {
  ble.print("Connected");
}

void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  ble.print("Disconnected");
}

void rx_callback(uint16_t conn_handle) {
  while (ble.available()) {
    Adafruit_BLEMIDI_Message msg = ble.read();

    if (msg.type == BLEMIDI_TYPE_NOTE_ON) {
      uint8_t r = msg.channel;
      uint8_t g = msg.note;
      uint8_t b = msg.velocity;
      set_pixels_color(r, g, b);
    }
  }
}

void set_pixels_color(uint8_t r, uint8_t g, uint8_t b) {
  for (int i = 0; i < pixels.numPixels(); i++) {
    pixels.setPixelColor(i, pixels.Color(r, g, b));
  }
  pixels.show();
}
No matter what I do I can not get it to compile out correctly. My goal is to get this working with the bluefruit connect app so I can control it with my phone. Any help will be appreciated.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by dastels »

Have you tested with a simpler program? Like the strandtest example.

Dave

User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

dastels wrote: Fri Mar 31, 2023 10:33 am Have you tested with a simpler program? Like the strandtest example.

Dave
Yes I have and it worked out perfectly. I was able to light up the entire strand any color I wanted.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by dastels »

OK, so we can rule out a hardware problem.

Can you add a Serial.print in rx_callback to verify it's getting called as expected?

Dave

User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

dastels wrote: Fri Mar 31, 2023 12:26 pm OK, so we can rule out a hardware problem.

Can you add a Serial.print in rx_callback to verify it's getting called as expected?

Dave
When I go to compile it adding the Serial.print it throws out an error saying I dont have a the Adafruit_BluefruitLE_nRF52.h. But I have that library installed and it shows that it is in the IDE manager.

C:\Users\deell\AppData\Local\Temp\.arduinoIDE-unsaved2023231-920904-yop77h.o137q\bleuart\bleuart.ino:2:10: fatal error: Adafruit_BluefruitLE_nRF52.h: No such file or directory
2 | #include <Adafruit_BluefruitLE_nRF52.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: Adafruit_BluefruitLE_nRF52.h: No such file or directory

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by dastels »

Weird. Serial is part of the Arduino core.

Maybe try doing something with the #13 LED or Neopixel (I forget what that board has... likely both) to indicate that that callback is, indeed, getting called.

Dave

User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

dastels wrote: Fri Mar 31, 2023 3:46 pm Weird. Serial is part of the Arduino core.

Maybe try doing something with the #13 LED or Neopixel (I forget what that board has... likely both) to indicate that that callback is, indeed, getting called.

Dave
It still throws that same error from my previous post.

User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

I have rewritten the code to

Code: Select all

#include <bluefruit.h>

#define PIN 15  //The pin to which your strip data in pin is connected
#define NUMPIXELS 12 //Edit this to match the number of pixels in your strip
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW+ NEO_KHZ800);
// OTA DFU service
BLEDfu bledfu;

// Uart over BLE service
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[];

void setup(void)
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Serial.println(F("Adafruit Bluefruit52 Controller App Example"));
  Serial.println(F("-------------------------------------------"));

  Bluefruit.begin();
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

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

  // Set up and start advertising
  startAdv();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
  Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
  Serial.println();  
}

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();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   * 
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html   
   */
  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  
}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  // Wait for new data to arrive
  uint8_t len = readPacket(&bleuart, 500);
  if (len == 0) return;

  // Got a packet!
  // printHex(packetbuffer, len);

  // Color
  if (packetbuffer[1] == 'C') {
    uint8_t red = packetbuffer[2];
    uint8_t green = packetbuffer[3];
    uint8_t blue = packetbuffer[4];
    uint8_t white = packetbuffer[0];
    Serial.print ("WRGB #");
    if (red < 0x10) Serial.print("0");
    Serial.print(red, HEX);
    if (green < 0x10) Serial.print("0");
    Serial.print(green, HEX);
    if (blue < 0x10) Serial.print("0");
    Serial.println(blue, HEX);
    if (white < 0x10) Serial.print("0");
    Serial.println(white, HEX);
    for(int i=0;i<NUMPIXELS;i++) {  //loop through all pixels setting to selected color
     pixels.setPixelColor(i, pixels.Color(red, green, blue, white));
    pixels.show(); 
     }
  }

  // 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");
    } else {
      Serial.println(" released");
    }
  }

  // GPS Location
  if (packetbuffer[1] == 'L') {
    float lat, lon, alt;
    lat = parsefloat(packetbuffer+2);
    lon = parsefloat(packetbuffer+6);
    alt = parsefloat(packetbuffer+10);
    Serial.print("GPS Location\t");
    Serial.print("Lat: "); Serial.print(lat, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print("Lon: "); Serial.print(lon, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print(alt, 4); Serial.println(" meters");
  }

  // Accelerometer
  if (packetbuffer[1] == 'A') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Accel\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Magnetometer
  if (packetbuffer[1] == 'M') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Mag\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Gyroscope
  if (packetbuffer[1] == 'G') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Gyro\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Quaternions
  if (packetbuffer[1] == 'Q') {
    float x, y, z, w;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    w = parsefloat(packetbuffer+14);
    Serial.print("Quat\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.print('\t');
    Serial.print(w); Serial.println();
  }
}
but now when ever I go to compile it I get this error

Code: Select all

c:\Users\deell\Documents\Arduino\libraries\Adafruit_BANNED\src\Adafruit_BLE_UART.cpp:24:10: fatal error: ble_system.h: No such file or directory
   24 | #include <ble_system.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
Multiple libraries were found for "Adafruit_BANNED.h"
  Used: C:\Users\deell\Documents\Arduino\libraries\Adafruit_BANNED
  Not used: C:\Users\deell\AppData\Local\Arduino15\packages\adafruit\hardware\nrf52\1.4.0\libraries\Adafruit_BANNED
exit status 1

Compilation error: exit status 1
All of my libarys are up to date and I am on a fresh install of the Arduino IDE

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by dastels »

That's some sort of installation/configuration issue.

From the "multiple library" complaint it appears that you have Arduino code/libraries in your Documents directory as well as your AppData\Local\Arduino15 directory. That could be the source of the error: the file is in one place but the compiler is looking in the other.

Dave

User avatar
Thenewclarence
 
Posts: 14
Joined: Mon Mar 14, 2022 11:16 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by Thenewclarence »

I have switched over to using Mu and coding in python to work around the errors in the Arduino IDE.

Code: Select all

import time
import board
import neopixel
import adafruit_bluefruit_connect as btle

# Configure the neopixel strip
NUM_PIXELS = 7
pixel_pin = board.D10
ORDER = neopixel.RGB
pixels = neopixel.NeoPixel(
    pixel_pin, NUM_PIXELS, brightness=0.2, auto_write=False, pixel_order=ORDER
)

# Set the colors for the neopixels
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
PURPLE = (255, 0, 255)
CYAN = (0, 255, 255)
WHITE = (255, 255, 255)

# Initialize the BLE connection
ble = btle.BluefruitLE()
uart = ble.UARTService()

# Define the UART command for setting the neopixel color
def set_color(r, g, b):
    command = "color,{},{},{}\n".format(r, g, b)
    uart.write(command.encode("utf-8"))


# Loop forever, waiting for UART commands from the Bluefruit Connect app
while True:
    # Connect to Bluefruit Connect
    ble.start_advertising()
    while not ble.connected:
        pass
    ble.stop_advertising()

    # Loop forever, processing UART commands from Bluefruit Connect
    while ble.connected:
        # Check for incoming UART data
        if uart.in_waiting:
            # Read the incoming UART data
            command = uart.read().decode("utf-8").strip()
            print("Received command:", command)
            # Parse the command and set the neopixel color
            if command.startswith("color"):
                values = command.split(",")[1:]
                if len(values) == 3:
                    r, g, b = [int(value) for value in values]
                    pixels.fill((r, g, b))
                    pixels.show()
        time.sleep(0.01)
This is my code and now I am getting a error staying

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 21, in <module>
AttributeError: 'module' object has no attribute 'BluefruitLE'

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Issues with the nRF52840 and a neopixel strip

Post by dastels »

That's because there's nothing named BluefruitLE in the adafruit_bluefruit_connect module.

Have a look at some of the BLE/NeoPixel projects. E.g. https://learn.adafruit.com/circuitpytho ... opixel-hat.

There's also this bluetooth with CircuitPython on the nRF52840 guide: https://learn.adafruit.com/circuitpython-nrf52840.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”