Grove LED Bar / Teensy 4.1

JackChot

New member
I’m trying to add a Grove LED Bar to a Teensy 4.1 project.
https://wiki.seeedstudio.com/Grove-LED_Bar/#tech-support--product-discussion

The problem is that the bar graph doesn’t respond immediately, or even at all.
The same test sketch works fine with a UNO.
Is the Teensy 4.1 too fast?
Do I need to use specific pins for the CLK and Data signals?
Thanks

C++:
#include <Grove_LED_Bar.h>
#define Bouton 4

Grove_LED_Bar bar(24, 25, 1, LED_BAR_10); // Clock pin, Data pin, Orientation

void setup() {
    // nothing to initialize
    bar.begin();
    pinMode(Bouton, INPUT_PULLUP);
    pinMode(13, OUTPUT);
}

void loop() {

    if (digitalRead(Bouton) == LOW) {
        digitalWrite(13, HIGH);
        bar.setLevel(10);
    //for (int i = 1; i <= 10; i++) {
    //    bar.setLed(i, i * 0.1);
    }
    else {
        digitalWrite(13, LOW);
        bar.setLevel(1);
    //for (int i = 1; i <= 10; i++) {
    //    bar.setLed(i, 1-(i * 0.1));
    }
}
 
Not sure if this is the cause of the problem: Debouncing the button is useful in any case. You can look at the "bounce" example script. Or store the time manually when the button state has changed and set a limit of e.g. 20ms or so to change the state.

I don't know if it makes a difference for the LED_Bar library when the setLevel command is used so often and repeated so quickly in the loop. You could check that by inserting a delay command as a test. A more clean programming style would be to just send the command when the state of the button is changed once.

Possible that there are other reasons that this library is not fully compatible with the Teensy.
 
Thank you both.

Inserting a delay or running an LED_Bar command on a rising edge doesn’t solve the problem.
My UNO R3 clone seems to be compatible with the library, unlike the Teensy.

It’s a real shame because this gadget is exactly what I need.
I don’t have the programming knowledge to hope to fix this. What would you advise?
 
The example at the link looks like below - only 10 updates per second.
The Teensy 4.1 will pass loop() at one or many MILLIONS of times persecond.
As noted this will catch button bounces - then once the button is settled it will continue to write the same value in the p#1 at a rate many times faster than an UNO.
There may be issues with the library, it may have working examples with usage detail.

There are better ways like elapsedMillis - but for a quick test putting a delay(1); in the loop code to see if that helps.


1777669902811.png
 
Before you upload to Teensy, try setting Tools > CPU Speed to 24 MHz.

Looking quickly at the library code, the send() function at line 62 and the latch() function at line 115 appear to be written for Arduino Uno at 16 MHz. There's no delay in send() and latch() lacks delays between driving the clock. Teensy at 600 MHz will run this no-delay code far too fast.

The hardware is rated for 10 MHz maximum clock speed. Uno running at 16 MHz can't possibly create a clock that fast. Teensy at 600 MHz definitely can!

At 24 MHz Teensy will still run faster than Uno, but you should at least get into the right range for this code to create a clock slow enough to work.


sc.png
 
Thank you very much Paul, I wouldn’t have thought of that solution.

24 MHz and even 150 MHz work, but not 396 MHz.
This reduction in speed does not seem to affect the rest of the project, which involves MIDI communication via the USB and USB Host ports.

I’m very grateful that you took the time to sort out my little problem.
I’d also like to take this opportunity to thank you for this forum, which has been a great help to me with this first project.

Best regards
 
If you want to run faster, edit those 2 functions inside the library. Try adding delayNanoseconds(50) before every digitalWrite to the clock pin. The latch() function has 2 digitWrite on some lines. Be careful to get a delay before every one of them.
 
Back
Top