VEEROBOT Docs
StoreBlogWiki
  • 🌐Introduction
  • ROS Based Robots
    • 🐺Wolf Robot
      • ROS Introduction
      • Getting Started with ROS 2
      • Introduction to Wolf Robot
      • ROS 2 Setup
      • ROS Concepts
      • Using Wolf Examples
      • Creating a URDF File
      • Visualising in Gazebo
      • Controlling motors from ROS
      • LiDAR Basics & Control
      • Using a Camera with ROS2
      • ROS 2 Cleanup
      • ROS 2 Controller
      • Teleoperation
      • Slam Navigation
      • Wolf : Conclusion
  • Arduino Based Robots
    • 🚜Micro:Xbot
      • Preparations before First Use
      • Programming the Board
      • Powering the Board
      • Pin Mapping
      • Basic Programs
        • Blinking LED
        • Buzzer Program
        • Fading LED
        • RGB LED
        • Light Detection
        • Battery Voltage
        • Line Follower Module
        • Ultrasonic Sensor
        • Infrared Remote Control
        • Motor Control
        • Bluetooth Control
        • Display Test
        • Test Me
      • Robot Assembly
      • Advanced Programs
        • Bluetooth Controlled Robot
        • Obstacle Avoidance Robot
        • Line Follower Robot
  • ROS2 Repo
    • Introduction
    • Page 1
      • Sub Page 1
    • Page 2
Powered by GitBook
On this page
  1. Arduino Based Robots
  2. Micro:Xbot
  3. Basic Programs

Fading LED

Slowly turn on and turn off an LED using analogWrite()

In the Blinking LED and Buzzer example, we saw how to control the pins digitally by turning it on and off. This example demonstrates the use of the analogWrite() function to fade an LED off and on. AnalogWrite uses pulse width modulation (PWM), turning a digital pin on and off very quickly with different ratio between on and off, to create a fading effect.

/*
* Fading LED: The program below fades an LED from On to Off to On continuously. 
* LED is connected to pin 10
*
* At VEEROBOT, we invest time and resources providing this open source code,
* Please support VEEROBOT and open-source hardware by purchasing products
* from us @ http://veerobot.com
* -----------------------------------------------------------------------------
* You are free to redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as  published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This Code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* See <http://www.gnu.org/licenses/>
*/

#include "config.h"

int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 10 to be an output:
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // set the brightness of pin 10:
  analogWrite(LED_BUILTIN, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
PreviousBuzzer ProgramNextRGB LED

Last updated 2 years ago

🚜