Unable to connect via Bluetooth

I have the following sketches for an Arduino Giga and an Arduino Wifi R4. At first, the boards were connecting but not anymore. The boards recognize each other but fail to connect. Any guidance would be greatly appreciated.

Giga:

#include "Arduino_GigaDisplay_GFX.h"
#include "Arduino_GigaDisplayTouch.h"
#include <ArduinoBLE.h>

GigaDisplay_GFX display;
Arduino_GigaDisplayTouch touchDetector;

#define WHITE 0xffffff
#define BLACK 0x0000
#define RED 0xF920
#define BLUE 0x0000ff
#define GREEN 0x07C0

int x, y, r, rowCoord;
int r1 = 50;
int r2 = 50;
int r3 = 50;

int red, blue, green;

int button = 2;
int buttonState;
int screen = 0;

char buffer[2];

bool justTurnedOn = true;

void setup() {
  delay(3000);
  Serial.begin(115200);

  while (!Serial);
  Serial.println("Test");

  pinMode(button, INPUT);

  display.begin();

  display.setRotation(1);
  display.fillScreen(BLACK);
  
  drawLines();
  printValues();

  display.fillCircle(r1, 80, 30, RED);
  display.fillCircle(r2, 230, 30, BLUE);
  display.fillCircle(r3, 380, 30, GREEN);

  touchDetector.begin();

  BLE.begin();
}

void loop() {
  buttonState = digitalRead(button);

  if (buttonState) {
    display.fillScreen(BLACK);

    if (screen < 2)
      screen++;
    else screen = 0;
  }

  if (screen == 0) {
    BLE.scanForUuid("19B10000-E8F2-537E-4F6C-D104768A1214");

    BLEDevice peripheral = BLE.available();

    Serial.println("Test 1");

    if (peripheral) {
      Serial.print("Found ");
      Serial.print(peripheral.address());
      Serial.print(" '");
      Serial.print(peripheral.localName());
      Serial.print("' ");
      Serial.print(peripheral.advertisedServiceUuid());
      Serial.println();

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

      BLE.stopScan();

      sendRGB(peripheral);
    

      drawLines();
      printValues();

      display.fillCircle(r1, 80, 30, RED);
      display.fillCircle(r2, 230, 30, BLUE);
      display.fillCircle(r3, 380, 30, GREEN);
    }
  }
    
  delay(250);
  

  uint8_t contacts;
  GDTpoint_t points[5];
  
  contacts = touchDetector.getTouchPoints(points);
  if (contacts > 0) { //Check if at least one touch occurs on the screen
      //Print the coordinates of all simultaneous contacts detected
    for (uint8_t i = 0; i < contacts; i++) {
        //Serial.print(points[i].x);
        //Serial.print(" ");
        // Serial.println(points[i].y);

        x = points[i].y;
        y = points[i].x;
    }
  }

  if (screen == 0) {
    if (x >= 50 && x <= 560) {
      if (y > 35 && y < 115) r = 2;
      else if (y > 215 && y < 225) r = 1;
      else if (y > 365 && y < 385) r = 0;

      moveCircle();
      calculateRGB();
      printValues();
      
    }
  } else if (screen == 1) {
    display.setCursor(50, 50);
    display.print("Screen 2");
  } else if (screen == 2) {
    display.setCursor(50, 50);
    display.print("Screen 3");
  }
}

void drawLines() {
  display.fillRect(50, 75, 510, 10, WHITE);
  display.fillRect(50, 225, 510, 10, WHITE);
  display.fillRect(50, 375, 510, 10, WHITE);
}

void moveCircle() {
  display.fillScreen(BLACK);

  drawLines();

  buffer[0] = r;
  buffer[1] = x;

  if (r == 0) {
    r1 = x;
    
    display.fillCircle(x, 80, 30, RED);
    display.fillCircle(r2, 230, 30, BLUE);
    display.fillCircle(r3, 380, 30, GREEN);
  } else if (r == 1) {
    r2 = x;

    display.fillCircle(r1, 80, 30, RED);
    display.fillCircle(x, 230, 30, BLUE);
    display.fillCircle(r3, 380, 30, GREEN);
  } else {
    r3 = x;

    display.fillCircle(r1, 80, 30, RED);
    display.fillCircle(r2, 230, 30, BLUE);
    display.fillCircle(x, 380, 30, GREEN);
  }

  delay(2);
}

void calculateRGB() {
  red = ((r1 - 50) / 2);
  blue = ((r2 - 50) / 2);
  green = ((r3 - 50) / 2);
}

void printValues() {
  display.setTextSize(7); 

  display.setCursor(625, 60);
  display.print(red);

  display.setCursor(625, 210);
  display.print(blue);

  display.setCursor(625, 360);
  display.print(green);
}

void sendRGB(BLEDevice peripheral) {
  Serial.println("Connecting ...");

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

  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()) {
    if (Serial.available() > 0) {
      ledCharacteristic.writeValue(buffer, 2, true);
    }
  }
}

R4:

#include <FastLED.h>
#include <ArduinoBLE.h>

#define NUM_LEDS 1
#define DATA_PIN 3
#define CLOCK_PIN 13

CRGB leds[NUM_LEDS];

BLEService service("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

void setup() { 
  FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);  

  Serial.begin(9600);
  while (!Serial);

  Serial.println("Test");

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

    while (1);
  }

  BLE.setLocalName("R4");
  BLE.setAdvertisedService(service);

  service.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(service);

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

  // start advertising
  BLE.advertise();

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

void loop() { 
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected()) {
      if (switchCharacteristic.written()) {
        Serial.println("test");
        Serial.println(switchCharacteristic.value());
      }
    }
  }

  leds[0] = CRGB(0, 0, 255);
}

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