Arduino – basic LED blink

Yet another Arduino blinking LED example. This one is using BasicLed class to make the LED control more comfortable. The code is on Github, here.

Parts Required

  • arduino board (i.e. Arduino/Genuino UNO)
  • basic LED (i.e. on-board LED)

Software

This code is written in C++ and can be compiled using an Arduino IDE. All information about how to compile this project is here.

/**
 * Copyright (c) 2020, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
 * Arduino/005
 * Blinking LED with BasicLED class.
 */

#include <stdint.h>

/**
 * The instance of this class can control single LED.
 * - on, off, toggle (any digital pin)
 * - brightness (only PWM pins; i.e. for Arduino Uno: 3, 5, 6, 9, 10, 11)
 */
class BasicLed {
public:
  BasicLed(uint8_t pin): pin_(pin) {
    pinMode(pin, OUTPUT); 
  }
  /**
   * Light the LED up
   */
  void on(void) {
    digitalWrite(pin_, HIGH);
  }
  /**
   * Turn the LED off
   */
  void off(void) {
    digitalWrite(pin_, LOW);
  }
  /**
   * Toogle LED on/off
   */
  void toggle(void) {
    digitalWrite(pin_, !digitalRead(pin_));
  }
  /**
   * Set LED brightness from range <0, 255>
   */
  void setBrightness(uint8_t value) {
    analogWrite(pin_, value);
  }
private:
  uint8_t pin_;
}; /* End of class BasicLed */

BasicLed led(13);

void setup() {
  // do nothing
}

void loop() {
  while (1) {
    led.toggle();
    delay(500); // wait 0.5s
  }
}

Leave a Comment