I am trying to compile my sketch and I am caught in an loop with the following error:
conversion from 'unsigned char' to 'arduino::string' is ambiguous
Can you help me please as my attempts to fix it has brought me back to where I started
#include <ArduinoBLE.h>
// Motor control pins
const int ENA = 5;
const int IN1 = 6;
const int IN2 = 7;
BLEService motorService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Define the motor control service
BLEByteCharacteristic motorControlCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // Define the motor control characteristic
int speed = 0; // Motor speed
void setup() {
Serial.begin(9600);
// Configure motor control pins
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
// Initialize BLE
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
// Set advertised local name and service UUID
BLE.setLocalName("ModelRailway");
BLE.setAdvertisedService(motorService);
// Add characteristics to the service
motorService.addCharacteristic(motorControlCharacteristic);
// Add service
BLE.addService(motorService);
// Start advertising
BLE.advertise();
Serial.println("BLE Motor control peripheral");
}
void loop() {
// Listen for BLE connections
BLEDevice central = BLE.central();
// If a central is connected to peripheral
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
// While the central is still connected
while (central.connected()) {
// Check if characteristic value is available
if (motorControlCharacteristic.written()) {
// Allocate a byte array to store the characteristic value
byte data[1];
// Read the characteristic value (up to 1 byte) - Corrected argument
if (motorControlCharacteristic.read(data, sizeof(data))) {
// Process the received data (assuming 1 byte)
unsigned char cmd = data[0];
// Process the command based on the character
if (cmd == 'F') {
forward();
} else if (cmd == 'B') {
backward();
} else if (cmd == 'S') {
stop();
} else if (cmd >= '0' && cmd <= '9') { // Check for digits (0-9)
speed = map(cmd - '0', 0, 10, 0, 255); // Corrected mapping for digits
analogWrite(ENA, speed);
}
} else {
Serial.println("Failed to read characteristic value");
}
}
}
// When the central disconnects, print it out.
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
void stop() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
Arduino: 1.8.13 (Linux), Board: "Arduino NANO 33 IoT"
/home/pi/Arduino/sketch_apr18a/sketch_apr18a.ino: In function 'void loop()':
sketch_apr18a:57:59: error: conversion from 'unsigned char' to 'arduino::String' is ambiguous
String command = motorControlCharacteristic.value();
^
In file included from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/IPAddress.h:24:0,
from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/ArduinoAPI.h:30,
from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/Arduino.h:23,
from sketch/sketch_apr18a.ino.cpp:1:
/home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/String.h:74:2: note: candidate: arduino::String::String(const arduino::__FlashStringHelper*) <near match>
String(const __FlashStringHelper *str);
^~~~~~
/home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/String.h:74:2: note: conversion of argument 1 would be ill-formed:
sketch_apr18a:57:58: error: invalid conversion from 'unsigned char' to 'const arduino::__FlashStringHelper*' [-fpermissive]
String command = motorControlCharacteristic.value();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
In file included from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/IPAddress.h:24:0,
from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/ArduinoAPI.h:30,
from /home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/Arduino.h:23,
from sketch/sketch_apr18a.ino.cpp:1:
/home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/String.h:70:2: note: candidate: arduino::String::String(const char*) <near match>
String(const char *cstr = "");
^~~~~~
/home/pi/.arduino15/packages/arduino/hardware/samd/1.8.14/cores/arduino/api/String.h:70:2: note: conversion of argument 1 would be ill-formed:
sketch_apr18a:57:58: error: invalid conversion from 'unsigned char' to 'const char*' [-fpermissive]
String command = motorControlCharacteristic.value();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
sketch_apr18a:65:13: error: 'forward' was not declared in this scope
forward();
^~~~~~~
/home/pi/Arduino/sketch_apr18a/sketch_apr18a.ino:65:13: note: suggested alternative: 'fread'
forward();
^~~~~~~
fread
sketch_apr18a:67:13: error: 'backward' was not declared in this scope
backward();
^~~~~~~~
sketch_apr18a:69:13: error: 'stop' was not declared in this scope
stop();
^~~~
/home/pi/Arduino/sketch_apr18a/sketch_apr18a.ino:69:13: note: suggested alternative: 'ltoa'
stop();
^~~~
ltoa
exit status 1
conversion from 'unsigned char' to 'arduino::String' is ambiguous
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.