led arduino code example

Example 1: code for led blink in arduino

void setup() {
  LED_BUILTIN = 3; //or 13 
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT); 
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Example 2: digital led arduino

// include library
#include<FastLED.h>

//define number of LED and pin
#define NUM_LEDS 8
#define DATA_PIN 3

// create the ld object array
CRGB leds[NUM_LEDS];

// define 3 byte for the random color
byte  r, g, b;

void setup() {
  // init the LED object
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  // set random seed
  randomSeed(analogRead(0));
}


void loop() {
  // loop over the NUM_LEDS
  for (int cur = 0; cur < NUM_LEDS; cur++) {
    // chose random value for the r/g/b
    r = random(0, 255);
    g = random(0, 255);
    b = random(0, 255);

    //set the value to the led
    leds[cur] = CRGB (r, g, b);

    // set the colors set into the phisical LED
    FastLED.show();

    // delay 50 millis
    FastLED.delay(200);
  }


}

Example 3: blink led arduino

void setup()
{
  pinMode(ledPin, OUTPUT);
}

Example 4: digital led arduino

#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 8
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 3
#define CLOCK_PIN 13
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() { 
     // Uncomment/edit one of the following lines for your leds arrangement.
     // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
 	  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
     // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
     // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
}
void loop() { 
 // Turn the LED on, then pause
 leds[0] = CRGB::Red;
 FastLED.show();
 delay(500);
 // Now turn the LED off, then pause
 leds[0] = CRGB::Black;
 FastLED.show();
 delay(500);
}

Tags:

C Example