0

I have a DotStar LED strip that I want to make it blink (e.g., 1 second on, 5 second off in an endless loop).

I tried doing so via the script below but it does not work. Can someone please explain me how to do so?

Script:

#include <Adafruit_DotStar.h>
#include <SPI.h>          

#define NUMPIXELS 10

#define DATAPIN    4
#define CLOCKPIN   5
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);  

void setup() 
{
  strip.begin();  
  strip.show();   
}

void loop() 
{
  strip.show(); 
  strip.setBrightness(155);  
  for (int i=0; i <= 10; i=i+1) {
    strip.setPixelColor(i, 255, 255, 255); 
  }
  delay(1000); 

  strip.setBrightness(0);  
  delay(5000); 
}
  • There's absolutely nothing in that sketch that shows an attempt at achieving what you want. Are you sure you posted the right code? – Majenko Dec 21 '15 at 14:58
  • This is the starting code and I hoped that the delay will cause at least a blink but it did not work. – user2788464 Dec 21 '15 at 15:18
  • You will only get a blink if you turn the LEDs on, then delay, then turn them off, then delay again. At the moment you are just turning them on and never off. – Majenko Dec 21 '15 at 15:19
3

The authors of the library are not fans of the setBrightness() method and advise handling it yourself.

The following code should do what you want.

#include <Adafruit_DotStar.h>
#include <SPI.h>          

#define NUMPIXELS 60 // This should be the total number of LEDs in your strip 

#define DATAPIN    4
#define CLOCKPIN   5
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);  

void setup() 
{
  strip.begin();  
  strip.show();   
}

void loop() 
{

  // loop through each pixel in the control array setting it to the colour mix you want.  
  for (int i=0; i <= NUMPIXELS; i=i+1) {
    strip.setPixelColor(i, 25, 25, 25); // Even 25 on my strip gives reasonably bright flash
  }

  strip.show(); // Then push the control array to the strip 
  delay(1000); // and leave it set for a while

// Then do it again for the 2nd brightness in your scheme

  // loop through each pixel in the control array setting it to off.  
  for (int i=0; i <= NUMPIXELS; i=i+1) {
    strip.setPixelColor(i, 0, 0, 0); 
  }

  strip.show(); // Then push the control array to the strip 
  delay(1000); // and leave it set for a while

}
  • Both here and in the OP: If you have 10 pixels, and you go from 0 to 10, you have tried to address 11 pixels. The middle condition check on the for loop ought to be a plain “<“. – Hack Saw Apr 9 '18 at 16:44
2

The basic idea of the program you're trying to write is:

loop() {
  strip.show(); // Turn LED strip on.
  delay(1000);  // One second, argument is milliseconds.
  strip.setBrightness(0); //Turn LED strip off.
  delay(5000);
  strip.setBrightness(155); //Turn LED strip on.
}

I haven't used the Adafruit DotStar library, so there may be details that I'm missing (like do you need to do a strip.show() after setting the brightness). But maybe this is enough to get you thinking about how to construct the program.

The real joy in hacking with something like this is the experimenting and the learning. Take the time to try to understand how you project works and to predict what will change as you modify the code and how you can tell if it is working the way you expect – that will get you a long way towards being a strong programmer.

When you're troubleshooting something new and/or unfamiliar, one thing that really helps is to break down the problem into simple steps. For example with this project a good first step would be to get the DotStart strip to turn on. Then see if you can get it to turn off. Build it up piece by piece.

  • Thanks, I updated the script in the question based on your suggestions but it still does not work, any idea why? – user2788464 Dec 21 '15 at 16:10
  • Can you describe what it does do, and what you're expecting it to do? "Does not work" is very hard to troubleshoot – especially remotely when you can't see the behavior. – dlu Dec 21 '15 at 16:20
  • OK, Only LED number 10 in the strip is on and no flashing. – user2788464 Dec 21 '15 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.