1

I'm trying to use the Arduino Uno to make a strip of LEDs blink as fast as they can while still blinking at different rates. I am able to get them blinking at different rates but the blinking is very slow:

#include <Adafruit_DotStar.h>
#include <SPI.h>         // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET

#define NUMPIXELS 144 // Number of LEDs in strip

// Here's how to control the LEDs from any two pins:
#define DATAPIN    11
#define CLOCKPIN   13
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN);

void setup() {
strip.begin(); // Initialize pins for output
strip.show();  // Turn all LEDs off ASAP
}

  int brightness = 1; //Setting brightness of all LEDs

   void loop() {

  for(int i=1; i<6; i++) 
//This loop is here so the number i can be used below to get different blink     //rates for the 3 LEDs blink at the same time
  {  
    if(i%2==0){

    strip.setPixelColor(0, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(0, 0);
    }
    //Sets blink rate of first LED, will be the fastest

    if(i%3==0){

    strip.setPixelColor(1, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(1, 0);
    }
     //Sets blink rate of second LED

    if(i%4==0){

    strip.setPixelColor(2, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(2, 0);
    }
     //Sets blink rate of third LED

   strip.show();
  }
  }

I thought I would be able to make them blink as fast as possible by using the micro() function as my counter instead of the i in the for loop, but this led to strange behavior:

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

#define NUMPIXELS 144 // Number of LEDs in strip

// Here's how to control the LEDs from any two pins:
#define DATAPIN    11
#define CLOCKPIN   13
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN);

void setup() {

  strip.begin(); // Initialize pins for output
  strip.show();  // Turn all LEDs off ASAP

  Serial.begin(9600);
}

int brightness = 1;

void loop() {

    int time = micros();

    if(time%2==0){

    strip.setPixelColor(0, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(0, 0);
    }

  if(time%3==0){
    strip.setPixelColor(1, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(1, 0);
    }

   if(time%4==0){
    strip.setPixelColor(1, brightness, brightness, brightness);
    }
    else
    {strip.setPixelColor(2, 0);
    }

   strip.show();
}

This led to the first LED appearing on (good, presumably I wouldn't be able to see it blinking) and the second blinking at a rate I could see (I shouldn't be able to see this) and the third LED is not on at all.

Can anyone see the mistake I'm making? Am I using the micro() function right? Thank you!

  • 1
    1 microsecond is only 16 clock-cycles. Your loop function will take a lot more that 1 microsecond to complete. Instead just declare int time=0; outside the loop and increment this variable at the end of the loop function (time=time+1). That way you don't skip any values for time. – Gerben May 7 '15 at 19:37
  • Thank you. So basically I can't make my fastest blinking LED blink at half the frequency of the clock in the Arduino? I'm limited by how fast the loop can execute. Thanks a lot :) – tablemountain May 8 '15 at 19:29
  • 1
    Blinking an led at 8Mhz is useless (human eye can see anything faster than 150hz), and probably impossible (due to gate capacitance of the output pins) – Gerben May 8 '15 at 19:45

Your Answer

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

Browse other questions tagged or ask your own question.