Hi, I am fairly new to coding, and this is my first ever post, so I hope I am using the forum correctly. I am working on a project where two nano 33 BLE peripheral devices act as remote controls and Bluetooth connect too one central GIGA R1. The GIGA will be taking the inputs from the peripheral device to control the movements of a robotic arm.
I am hoping that the GIGA will be able to be connected to the nano controllers simultaneously allowing both controllers to move the axis’s of the arm. So far, I have managed to get one controller to work but incorporating the second one is proving difficult. If anyone has any input on the best way to go about this or whether this is even possible with BLE that would be great.
Here is my code for one controller connecting to the central GIGA.
Central:
/*
Created on 1.03.2024~ intended for use on a Giga R1 acting as a central BLE devices. When central receives the signal from the peripheral
it provides digital outputs to control the manipulator arm motor drivers.
*/
#include <ArduinoBLE.h>
#define TOG_RIGHT_OUT_PIN 3
#define TOG_LEFT_OUT_PIN 4
#define TOG_UP_OUT_PIN 6
#define TOG_DOWN_OUT_PIN 5
#define TWO_WAY_UP_OUT_PIN 1
#define TWO_WAY_DWN_OUT_PIN 2
const int CONNECTION_LED = 8; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(CONNECTION_LED, OUTPUT);
Serial.begin(9600);
while (!Serial)
;
// initialize the BLE hardware
BLE.begin();
Serial.println("BLE Central - Manipultor control");
// start scanning for Manipulator controller BLE peripherals
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // this is the Universal Unique Identifier Number
}
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().indexOf("Manipulator controller") < 0) {
Serial.println("No 'Manipulator controller' in name");
return; // If the name doesn't have "Manipulator controller" in it then ignore it
}
// 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 Controller_Input_Direction characteristic
BLECharacteristic Controller_Input_Direction = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214"); // 0001 peripheral characteristic
if (!Controller_Input_Direction) {
Serial.println("Peripheral does not have Controller Input Direction characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
// while the peripheral is connected
if (Controller_Input_Direction.canRead()) {
byte value = Controller_Input_Direction.read();
Controller_Input_Direction.readValue(value);
//Serial.println(Controller_Input_Direction .readValue(value));
if (value == 0x01) {
digitalWrite(TOG_RIGHT_OUT_PIN, HIGH); // When the toggle swtich is pressed right, output right signal(led)
} else {
digitalWrite(TOG_RIGHT_OUT_PIN, LOW);
}
if (value == 0x02) {
digitalWrite(TOG_LEFT_OUT_PIN, HIGH);
} else {
digitalWrite(TOG_LEFT_OUT_PIN, LOW);
}
if (value == 0x03) {
digitalWrite(TOG_UP_OUT_PIN, HIGH);
} else {
digitalWrite(TOG_UP_OUT_PIN, LOW); // This part of the code turns on the correct output depending on the
} // value it receives from the Peripheral (controller)
if (value == 0x04) {
digitalWrite(TOG_DOWN_OUT_PIN, HIGH);
} else {
digitalWrite(TOG_DOWN_OUT_PIN, LOW);
}
if (value == 0x05) {
digitalWrite(TWO_WAY_UP_OUT_PIN, HIGH);
} else {
digitalWrite(TWO_WAY_UP_OUT_PIN, LOW);
}
if (value == 0x06) {
digitalWrite(TWO_WAY_DWN_OUT_PIN, HIGH);
} else {
digitalWrite(TWO_WAY_DWN_OUT_PIN, LOW);
}
// when peripheral is connnected LED will flash- done without using deley's as they effect the rest of program
if (peripheral.connected()) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(CONNECTION_LED, ledState);
}
} else {
digitalWrite(CONNECTION_LED, HIGH);
}
}
}
Serial.println("Peripheral disconnected");
}
Peripheral:
/*
Created 1.03.2024~ intended for use on a Nano 33 BLE acting as a peripheral device. When it receives a digital input from
the controller, a set byte value depending on what the input was is sent via Bluetooth low energy to the central device
which control the manipulator motor drivers in relation to what button was pressed.
*/
#include <ArduinoBLE.h>
#define LED_TEST 10
//referance which switch contact connect to which Digital Input
int TOG_RIGHT_PIN = 4;
int TOG_LEFT_PIN = 5;
int TOG_UP_PIN = 6;
int TOG_DOWN_PIN = 7;
int TWO_WAY_UP_PIN = 2;
int TWO_WAY_DWN_PIN = 3;
BLEService Manip_Controller_Service("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service - Universally Unique IDentifier same as on central
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic Controller_Input_Direction("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify | BLEWrite); //0001 defines the peripheral characteristic
void setup() {
Serial.begin(9600);
pinMode(TOG_RIGHT_PIN, INPUT_PULLUP);
pinMode(TOG_LEFT_PIN, INPUT_PULLUP);
pinMode(TOG_UP_PIN, INPUT_PULLUP); //input pull up - means that a pull down resistor does not have to be used.
pinMode(TOG_DOWN_PIN, INPUT_PULLUP);
pinMode(TWO_WAY_UP_PIN, INPUT_PULLUP);
pinMode(TWO_WAY_DWN_PIN, INPUT_PULLUP);
pinMode(LED_TEST, OUTPUT);
// begin initialization the BLE connection
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy failed!");
}
// set advertised local name and service UUID:
BLE.setLocalName("Manipulator controller");
BLE.setAdvertisedService(Manip_Controller_Service);
// add the characteristic to the service
Manip_Controller_Service.addCharacteristic(Controller_Input_Direction);
// add service
BLE.addService(Manip_Controller_Service);
// start advertising
BLE.advertise();
Serial.println("BLE Manipulator controller Peripheral, waiting for connections....");
}
void loop() {
// listen for BLE 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 (digitalRead(TOG_RIGHT_PIN) == LOW) {
Serial.println("RIGHT"); //when the right button is pressed assign characteristic value to 0x01
Controller_Input_Direction.writeValue((byte)0x01);
digitalWrite(LED_TEST, HIGH);
}
else if (digitalRead(TOG_LEFT_PIN) == LOW) {
Controller_Input_Direction.writeValue((byte)0x02);
Serial.println("LEFT");
}
else if (digitalRead(TOG_UP_PIN) == LOW) {
Controller_Input_Direction.writeValue((byte)0x03);
Serial.println("UP");
} else if (digitalRead(TOG_DOWN_PIN) == LOW) {
Controller_Input_Direction.writeValue((byte)0x04);
Serial.println("DOWN");
}
else if (digitalRead(TWO_WAY_UP_PIN) == LOW) {
Controller_Input_Direction.writeValue((byte)0x05);
Serial.println("TWO WAY UP");
}
else if (digitalRead(TWO_WAY_DWN_PIN) == LOW) {
Controller_Input_Direction.writeValue((byte)0x06);
Serial.println("TWO WAY DOWN");
}
else {
Controller_Input_Direction.writeValue((byte)0x00); // when there are no buttons pressed, characteristic value = 0x00
Serial.println("OFF");
digitalWrite(LED_TEST, LOW);
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}