As it now seems to be definitly confirmed that under firmware 0.2.0 the ArduinoUno R4 Wifi's LED Matrix Library (Arduino_LED_Matrix.h) conflicts with the BLE Library (ArduinoBLE.h) causing the BLE connection to disconnect and re-connect continually. Because with firmware 0.1.0 there is no BLE compatiblity (despite it being adverised on the box the Uno R4 Wifi came in from the Arduino store) it is, at the moment, not possible to use the LED Matrix Library with the BLE Library thereby making the Uno R4 Wifi unusable for the project I bought it for.
I was wondering if it would be possible to use an Adafruit LED Matrix library on the Uno R4 Wifi? Or alternativey if simplified instructions could be given as to how to address the matrix without a library.
However, my wild guess is, that the issue is not that the matrix code is running from a library. But probably by something that the code is doing. I threw out a few darts on possibilities earlier.
For the fun of it, I am experimenting with the LED examples. I have a NANO 33 BLE programed as the LED device and I am running a modified version of the LED_Control on the WIFI. So far I changed from using button, to use Sparkfun QWIIC button, as it was convenient. The two are still talking. Next up will add the matrix library.
Note: I updated the analog.cpp to the one that fixed the heap corruption bug.
Also should mention, running with the updated wifi firmware that fixed the issue about not being able to program the WIFI reliably.
EDIT The modified example appears to work OK with the matrix.
It starts off that each time you press or release the button, it changes which LED in the matrix is lit... If you hold down the button for over 5 seconds (Appears to take longer than that), it does a matrix.begin(). And then each time you press and release it updates the display with the frames from the DisplaySingleFrame sketch. Here is the current version of the sketch. Not sure if it will help or not. Will try a Wi-Fi example next. BLE_LedControl-230812a.zip (2.0 KB)
Thanks for your time with this. I tried your sketch but I couldn't get it to work, I have the Uno R4 Wifi running your sketch and a Nano 33 BLE Sense runnin the example LED sketch. The monitor stops after these comments:
*** Button did not start ***Bluetooth® Low Energy Central - LED control
Found ba:8e:08:04:a3:4d 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
Pressing the button does nothing on either board. I noticed that in your sketch the line #39pinMode(buttonPin, INPUT); was commented out. I removed the // but the button still seems to have no effect.
Mainly because it was easy for me to do as the board has a qwiic connector on it. Should easily be able to convert it back to simple button. I hacked up the main page:
/*
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.
*/
//#define USE_SPARKFUN_QWIIC_BUTTON
#include <ArduinoBLE.h>
#ifdef USE_SPARKFUN_QWIIC_BUTTON
#include <SparkFun_Qwiic_Button.h>
QwiicButton button;
#else
const int buttonPin = 2;
#endif
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
#include "frames.h"
bool oldButtonState = false;
int matrix_led = -1;
bool matrix_begin_called = false;
uint32_t button_pressed_start_time = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
#ifdef USE_SPARKFUN_QWIIC_BUTTON
Wire1.begin();
if (!button.begin(0x6F, Wire1)) {
Serial.print("*** Button did not start ***");
}
#else
// configure the button pin as input
pinMode(buttonPin, INPUT);
#endif
// 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
#ifdef USE_SPARKFUN_QWIIC_BUTTON
bool buttonState = button.isPressed();
#else
bool buttonState = digitalRead(buttonPin) == 0; // using PU so 0 is pressed
#endif
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);
button_pressed_start_time = millis();
} else {
Serial.println("button released");
// button is released, write 0x00 to turn the LED off
ledCharacteristic.writeValue((byte)0x00);
}
if (!matrix_begin_called && ((millis() - button_pressed_start_time) > 5000)) {
// button pressed more than 5 seconds.
Serial.println("\n***Call matrix.begin() ***");
matrix.begin();
matrix_begin_called = true;
matrix_led = 0;
}
if (matrix_begin_called) {
matrix_led++;
if (matrix_led >= (int)(sizeof(frame_list)/ sizeof(frame_list[0]))) matrix_led = 0;
matrix.loadFrame(frame_list[matrix_led]);
} else {
matrix_led = (matrix_led == 95)? 0 : matrix_led + 1;
matrix.on(matrix_led);
}
}
}
Serial.println("Peripheral disconnected");
}
Still need the other tab with the frames...
It compiles but I have not tried it.
I have, it works! I don't know what this shows the experts but for me it shows that I can use BLE and the Matrix in the same sketch, which is important to my little project.
For what is worth, I also pushed up example, where instead of R4 WIFI running the LedControl sketch, it runs the LED sketch. And have version that does similar to the other test. I tested it then with Nano 33IOT running a very slightly modified version of the LedControl. Only changes I made was to change pinMode(buttonPin, INPUT_PULLUP);
and changed the check for state: int buttonState = (digitalRead(buttonPin) == 0);
As I said in a previous comment this sketch works on my Uno R4 WiFi and I haven't updated the analog.cpp to the one that fixed the heap corruption bug.
So looking at your sketch you are using the Arduino_LED_Matrix.h and ArduinoBLE.h which caused my original sketch above to keep disconnecting-connecting. What is it in your sketch that fixes this issue?
I am following your advice but before I can use your code for other applications I would like to alter it so that the button remains in a released state until it is pressed and remains in a pressed state until it is released.
At the moment the code causes the button to alternate between button pressed and button released continually until the button is actually pressed when the monitor reads button pressed and everything stops until the button is released again. How can I stop the runaway pressed/released mode in the released state.
I realise this should be simple but I have been working so hard with this BLE/Matrix issue my brain is beginning to melt
One other question if I may, would including the two libraries with "" rather than <> make any difference? ie
This may depend on your button, the wiring of the button and the code.
Short answer: You probably don't have a pull-up resistor, so the signal going into pin 2 is floating, and unpredictable.
Longer version:
If you look at their example LedControl - it says:
Button with pull-up resistor connected to pin 2.
And the code shows
// configure the button pin as input
pinMode(buttonPin, INPUT);
...
int buttonState = digitalRead(buttonPin);
if (oldButtonState != buttonState) {
// button changed
oldButtonState = buttonState;
if (buttonState) {
Serial.println("button pressed");
A couple of issues with this description and code.
My internal questions about the comment about the button, is:
The other oead to the button is connected to ground
Wondering if they are assuming the switch is NO(Normally open) or NC(Normally closed)? Most of my switches are NO - The connection between the two pins is only made when the switch is pressed. NC the connection is made unless the button is pressed.
Assuming you have a PU resistor and other side connected to GND on a NO switch, then the state test is wrong:
if (buttonState) {
Serial.println("button pressed");
As in the unpressed state, the PU will have the signal high, and when pressed it will be low...
Change I would make to he code:
Avoid the need for external Pull up resistor:
pinMode(buttonPin, INPUT_PULLUP);
(I forgot to check in that to my github test project) This causes the pin to be internall pulled high.
I would also change the state of the test for when the button is pressed.
Could be done: like change the test above to: if (!buttonState)
Or what I did on the reading of the button:
`
Short Answer:
When you use the
The compiler will look for the file name in the normal search path directories. Typically used.
for header files contained in the system area and libraries.
When you use the "filename"
Like above, but also looks in the directory of where the code is that included it. Like internal header files used in the core, or in a library. Likewise for header files that you have in your sketch folder.
removing the PU resistor and using pinMode(buttonPin, INPUT_PULLUP); fixed the problem.
A few comments ago you said you had also used the Nano as Central and the Uno R4 WiFi as the periheral with the Uno running the LED sketch. But to test the matrix the Uno must be running a modified LED sketch in order for the Matrix to be used.
Again many thanks! I have uploaded you modified LED sketch, UNOR4_WIFI_LED.ino, onto the Uno
R4 but as I couldn't find a modified LED_Control sketch fot the Nano so I used the original with all the matrix stuff removed and the two modifications you suggested. Unfortunately it keeps disconnecting. According to the monitor it disconnects because the Uno as peripheral device doesn't have a LED characteristic yet the LED charateristic on the Uno is set up with BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
and on the Nano with BLECharacteristic ledCharacteristic = peripheral.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
Using the example LedControl sketch with those mods on the Nano with the Nano connected to the monitor and the Uno R4 with the sketch UNOR4_WIFI_LED.ino fom your github project causes the Nano to report:
Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
Peripheral does not have LED characteristic!
Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
Peripheral does not have LED characteristic!
Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
I have tried using the example LedControl sketch on the Uno and in the nRF Connect app on my phone there is a service available. after loading your UNOR4_WIFI_LED.ino from your github project on the Uno and there is no service available.
It seems likely to me that we may have come against the original problem with the matrix again.
Hard to say, but I simply try to remove as many bad things as possible, just so that I don't end up trying to debug something, that was already fixed.
The analog.cpp fix was causing a heap issue at startup. How issues like that manifest themself can be really fickle. Also I updated the firmware of the ESP32 to fix the issue where you could not download new sketches...
Or maybe there is a race condition that is there that you have to just hit things exactly right to happen.