Two Nano 33, trying multiple led control over bluetooth

Hi
Im new to this but i have been trying to modifie the Aurdionble example for led control so i can use 8. i have one board on Led control and the other one is led, pins 2-9 on the led side are connected to control pins on a low side relay control. The example works fine for one but i cant make more buttons work. I have looked almost everywhere but i cant find any answer.

Hi @geirinn

welcome to the arduino forum.

post your complete sketch as a code-section.
How should anybody give any advice by not seeing your code?

best regards Stefan

Its just the basic code, i have tried many ways to program it but not something i can do.

Controler /*
  LED Control

  This example scans for Bluetooth® Low Energy peripherals until one with the advertised service
  "19b10000-e8f2-537e-4f6c-d104768a1214" UUID is found. Once discovered and connected,
  it will remotely control the Bluetooth® Low Energy peripheral's LED, when the button is pressed or released.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
  - Button with pull-up resistor connected to pin 2.

  You can use it with another board that is compatible with this library and the
  Peripherals -> LED example.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

// variables for button
const int buttonPin1 = 2;
int oldButtonState = LOW;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // configure the button pin as input
  pinMode(buttonPin1, INPUT_PULLUP);

  // initialize the Bluetooth® Low Energy hardware
  BLE.begin();

  Serial.println("Bluetooth® Low Energy Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "LED") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    // while the peripheral is connected

    // read the button pin
    int buttonState = digitalRead(buttonPin1);

    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

        // button is pressed, write 0x01 to turn the LED on
        ledCharacteristic.writeValue((byte)0x01);
      } else {
        Serial.println("button released");

        // button is released, write 0x00 to turn the LED off
        ledCharacteristic.writeValue((byte)0x00);
      }
    }
  }

  Serial.println("Peripheral disconnected");
}


Recover
/*
  LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin1 = 2; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin1, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin1, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin1, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

Do you have any experience with any of the button reading libraries available for Arduino? I like Bounce2.h, but ezButton.h and Button.h are also commonly used.

Write separate code to use a library to read the 8 buttons. I think you want to generate a byte value from reading the 8 buttons. Each of the 8 bits will represent a pressed button

When you have this worked out, the button code will go on the central and will replace the reading of one button. Then you will send the value which represents which buttons are pressed to the peripheral. The code is already written for a byte characterisitic.

The code on the peripheral will need to interpret the value it reads from the central to turn on multiple leds.

1 Like

As you can see from the code using BLE is a medium complex thing.
If you are a real beginner you should start with something simpler than that.

If you are just asking for code you would be in the situation of asking again and again and again and again always depending on the grace of somebody to modify your code.

You should learn the most crucial basics about programming
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Hello geirinn

Welcome to the worldbest forum.

Does the programme work for one LED as shown in your sketch?

Hi, i have been reading codes and material for days now and that is why i am asking, i am not asking because i opened on project and started asking. I have tried many ways and failed many ways that is why i ask.
The code works fine for the one button/pin it is constructed for, but i do not seam to find a way to make it so it works for the other buttons, i have tried giving them a new uuid´s, new characteristics and all the other things i have found.

So if you could be so kind to help me or point me in the right direction that would be very appreciated.

You seem to have not understood. You seem to still underestimate the knowledge you need to write code on your own. It is not done by reading through 30 lines of code.

As a short point into the right direction:
you have to configure each button as input_pullup for reliable button-operation
you have to debounce each button by using a button-library or writing the code from scratch which in case of debouncing is a pretty easy job
one approach to switch 8 LEDs is to put the state of each button as an ascii-coded "0" / "1" into a an array of char and send this array of char as a single characteristic to the receiver

And then check each array-element if it is a "0" or a "1" and according to its value switch on off the LED.

If you did not understand anything of what I wrote above this indicates that your programming-knowledge is at such a low level that anybody who wants to help you has to write down the ready to use code.

And as soon as you want to change any kind of detail you would totally rely again on other users grace to write this modification into your code.

This is the reason why I suggest that you take 5-8 hours of your time to work through
this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Did you read reply #5? You can still use the byte characteristic you have, but instead of a value 0 or 1 you will give it a value between 1 and 255 depending on which buttons are pressed.

An additional suggestion for your development is to focus on the peripheral code and use a phone app for the central. LightBlue is what I like to use, but there are others. The phone will send the byte value representing what you would construct from the buttons.

I'm not certain what you are trying to achieve, but you might find it easier to work with an 8 pole dip switch instead of individual buttons.
https://www.digikey.com/en/articles/the-basics-of-dip-switches

1 Like

Thank you for your answers, so if i am understanding things right, it is to complex writing one code for all 8.buttons in the same code?

Im a trying to control 8. Push buttons throught the nano 33 iot to with ble to a 8.channel relay control that is connected to another nano 33 iot. I can find alot of information, but all of them seem to include only one button and information that my auto-mechanic brain cant see or understand. Maby its to complex for me to try.

Is there maby some place i can pay for this code?

You are understanding it

totally wrong

For an experienced user It is a peace of cake to write the code
that 8 physical buttons are transmitted via buetooth to a second microcontroller and to switch 8 relays on the receiving microcontroller.

Depending on the details of the functionality

  • plain simple indeoendant toggle on/off
  • something more complex

it takes some hours or more

The arduino userforum has a subform jobs and paid consulting

best regards Stefan

Thank you for taking the time to write in this thread.
There is one thing i will never understand and that is why instead of giving people pointers on what they are asking about , you feel the urge to stomp on the fact that i they are new and they need to learn, when in fact they are trying to learn. by asking, well that does not put you on a higher horse if you know things other people dont know yet. So in the future dont post anything in my threads if you are not going to write something helpfull that makes sens, when in dead i was asking for help not someone to tell me the obvius fact that i am new at this, because it seems you make it you habit of doing so.

Best regards.

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