Digital I/O
Digital Output
LEDs are a perfect visual indicator for the operation of a digital output.
LED - Blink
Below, is one of the simplest built-in examples of the Arduino IDE. Users can find this sketch in the File > Examples > 01.Basics > Blink drop-down menu. The example toggles the digital output of the GPIO pin connected to the blue, STAT
(status) LED.
Tip
Users do not have to worry about modifying the sketch for the GPIO pin that is connected to the STAT
LED. The pin is already defined in the Arduino core with the LED_BUILTIN
variable. Therefore, no adjustments are necessary to utilize this example code.
Blink.ino
LED - PWM
LEDs are also great visual indicators for PWM outputs.
On the RA6M5 Thing Plus, the GPIO pin connected to the STAT
LED is also capable of generating a PWM output. There are two built-in examples in the Arduino IDE for operating a PWM output on an LED. The sketches operate by oscillating the duty cycle of the PWM signal from the GPIO pin so that the LED appears to be dimming or fading.
Tip
Users will need to modify the sketches below, for the GPIO pin that is connected to the STAT
LED. As mentioned in the example above, they can use the predefined LED_BUILTIN
variable. Users will need to replace the highlighted line with the modification using the LED_BUILTIN
variable to define the GPIO pin connected to the STAT
LED.
Fade.ino
int led = 9; // the PWM pin the LED is attached to
int led = LED_BUILTIN; // the PWM pin the LED is attached to
Fading.ino
int ledPin = 9; // LED connected to digital pin 9
int ledPin = LED_BUILTIN; // LED connected to digital pin 14
Users can find this sketch in the File > Examples > 01.Basics > Fade drop-down menu.
Fade.ino
Users can find this sketch in the File > Examples > 03.Analog > Fading drop-down menu.
PWM ≠ Analog
Although this example is listed under the 03.Analog menu, users should keep in mind that a PWM output is not a true analog signal. PWM signals can only emulate an analog output, based on the average voltage output of their square waveform.
Fading.ino
LED - WS2812
Warning
Support for the new Renesas Arduino boards has not been officially released for the FastLED Arduino library. Users should follow the installation steps documented in the Software Overview.
The WS2812 is an addressable RGB LED that operates with a digital signal that stacks frames of 24-bit data, per LED in a chain. We recommend that the FastLED Arduino library be utilized in the Arduino IDE, to control the RGB LED on the RA6M5 Thing Plus. Once the library is installed in the Arduino IDE, users will find several example sketches listed in the File > Examples > FastLED > drop-down menu. We recommend the following examples for users to get started:
Blink.ino
ColorTemperature.ino
ColorPalette.ino
Tip
Below, are a few tips for users to start working with the FastLED Arduino library. However, for more details, users should refer to the documentation for the FastLED Arduino library.
Define Parameters
When utilizing the examples in the FastLED Arduino library, users may need to define and/or modify a few parameters:
// GPIO pins connected to the LED
#define LED_PIN LED_RGB // (1)!
// #define CLOCK_PIN 13 (2)
// Information about the LED (strip)
#define NUM_LEDS 1 // (3)!
#define CHIPSET WS2812 // (4)!
#define COLOR_ORDER GRB // (5)!
// Define the array of leds
CRGB leds[NUM_LEDS];
//
#define BRIGHTNESS 40 // (6)!
- For the RA6M5 Thing Plus, users can either refer to the predefined variable
LED_RGB
or define the pin as13
(D13
) for the RGB LED - A clock pin is not required for the WS2812 LED on the RA6M5 Thing Plus; comment out any mentions of it
- On the RA6M5 Thing Plus, there is only one RGB LED included on the board
- The RGB LED on the RA6M5 Thing Plus is a WS2812 LED
- The format of the color order in the WS2812's data frames is Green, Red, Blue (GRB)
-
Some of the examples may define an LED brightness; this should be an 8-bit value (
0
-255
). Below, is a table of reference values for users to get started with:Environment Description Value Dark Room Start to see primary colors 5
-12
Direct Sunlight Start to see primary colors > 60
Direct Sunlight Start to see color blending 100
-180
Direct Sunlight Decent color blending > 230
Instantiate LED Controller
Most of the examples in the FastLED library predefine parameters for the LED (strip), as shown above. Later in the sketches, an instance of the LED controller is created; therefore, if users have already modified all the parameters above, no other modifications should be necessary:
Otherwise, users will need to locate where the instance of the LED controller is created and provide the required values. Users will also need to configure the LED brightness later in the sketch, if that predefined variable isn't provided either:
FastLED.addLeds<WS2812, 13, GRB>(leds, 1);
FastLED.setBrightness(<value>);
The example code should still enumerate the order of the RGB color channels, required for the data frames of the LED chipset. However, users may need to modify the enumerator to GRB
:
Users can find this sketch in the File > Examples > FastLED > Blink drop-down menu.
Blink.ino
/// @file Blink.ino
/// @brief Blink the first LED of an LED strip
/// @example Blink.ino
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 1
// For led chips like WS2812, 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
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#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.
// ## Clockless types ##
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // GRB ordering is assumed
// FastLED.addLeds<SM16703, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1829, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1812, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1803, 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<UCS1904, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS2903, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<WS2852, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<GS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<APA106, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<PL9823, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2813, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GE8822, 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<LPD1886, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD1886_8BIT, DATA_PIN, RGB>(leds, NUM_LEDS);
// ## Clocked (SPI) types ##
// FastLED.addLeds<LPD6803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // BGR ordering is typical
// FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // BGR ordering is typical
// FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // BGR ordering is typical
// FastLED.addLeds<SK9822, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); // BGR ordering is typical
}
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);
}
Users can find this sketch in the File > Examples > FastLED > ColorTemperature drop-down menu.
ColorTemperature.ino
/// @file ColorTemperature.ino
/// @brief Demonstrates how to use @ref ColorTemperature based color correction
/// @example ColorTemperature.ino
#include <FastLED.h>
#define LED_PIN 3
// Information about the LED strip itself
#define NUM_LEDS 60
#define CHIPSET WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 128
// FastLED provides two color-management controls:
// (1) color correction settings for each LED strip, and
// (2) master control of the overall output 'color temperature'
//
// THIS EXAMPLE demonstrates the second, "color temperature" control.
// It shows a simple rainbow animation first with one temperature profile,
// and a few seconds later, with a different temperature profile.
//
// The first pixel of the strip will show the color temperature.
//
// HELPFUL HINTS for "seeing" the effect in this demo:
// * Don't look directly at the LED pixels. Shine the LEDs aganst
// a white wall, table, or piece of paper, and look at the reflected light.
//
// * If you watch it for a bit, and then walk away, and then come back
// to it, you'll probably be able to "see" whether it's currently using
// the 'redder' or the 'bluer' temperature profile, even not counting
// the lowest 'indicator' pixel.
//
//
// FastLED provides these pre-conigured incandescent color profiles:
// Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc,
// HighNoonSun, DirectSunlight, OvercastSky, ClearBlueSky,
// FastLED provides these pre-configured gaseous-light color profiles:
// WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent,
// FullSpectrumFluorescent, GrowLightFluorescent, BlackLightFluorescent,
// MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium,
// FastLED also provides an "Uncorrected temperature" profile
// UncorrectedTemperature;
#define TEMPERATURE_1 Tungsten100W
#define TEMPERATURE_2 OvercastSky
// How many seconds to show each temperature before switching
#define DISPLAYTIME 20
// How many seconds to show black between switches
#define BLACKTIME 3
void loop()
{
// draw a generic, no-name rainbow
static uint8_t starthue = 0;
fill_rainbow( leds + 5, NUM_LEDS - 5, --starthue, 20);
// Choose which 'color temperature' profile to enable.
uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2);
if( secs < DISPLAYTIME) {
FastLED.setTemperature( TEMPERATURE_1 ); // first temperature
leds[0] = TEMPERATURE_1; // show indicator pixel
} else {
FastLED.setTemperature( TEMPERATURE_2 ); // second temperature
leds[0] = TEMPERATURE_2; // show indicator pixel
}
// Black out the LEDs for a few secnds between color changes
// to let the eyes and brains adjust
if( (secs % DISPLAYTIME) < BLACKTIME) {
memset8( leds, 0, NUM_LEDS * sizeof(CRGB));
}
FastLED.show();
FastLED.delay(8);
}
void setup() {
delay( 3000 ); // power-up safety delay
// It's important to set the color correction for your LED strip here,
// so that colors can be more accurately rendered through the 'temperature' profiles
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 );
FastLED.setBrightness( BRIGHTNESS );
}
Users can find this sketch in the File > Examples > FastLED > ColorPalette drop-down menu.
ColorPalette.ino
/// @file ColorPalette.ino
/// @brief Demonstrates how to use @ref ColorPalettes
/// @example ColorPalette.ino
#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 50
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code. Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop()
{
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; ++i) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly. All are shown here.
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
}
}
// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
for( int i = 0; i < 16; ++i) {
currentPalette[i] = CHSV( random8(), 255, random8());
}
}
// This function sets up a palette of black and white stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
// 'black out' all 16 palette entries...
fill_solid( currentPalette, 16, CRGB::Black);
// and set every fourth one to white.
currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White;
}
// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
CRGB purple = CHSV( HUE_PURPLE, 255, 255);
CRGB green = CHSV( HUE_GREEN, 255, 255);
CRGB black = CRGB::Black;
currentPalette = CRGBPalette16(
green, green, black, black,
purple, purple, black, black,
green, green, black, black,
purple, purple, black, black );
}
// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM. A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
CRGB::Red,
CRGB::Gray, // 'white' is too bright compared to red and blue
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Gray,
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Red,
CRGB::Gray,
CRGB::Gray,
CRGB::Blue,
CRGB::Blue,
CRGB::Black,
CRGB::Black
};
// Additional notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color. You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative. FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.
This sketch allows users to test the brightness of the WS2812 RGB LED on the RA6M5 Thing Plus in direct sunlight. The sketch implements an interrupt on the USRBTN button to increase the brightness of the LED in increments. The brightness value is then displayed on the Qwiic OLED Display, which is connected by a Qwiic cable.
Optional Hardware
The example code can be found in the GitHub repository. However, users can simply click the button to download the code or expand the box (below) to copy the code.
RGB_sunlight.ino
/*
RGB_sunlight.ino
ColorPalette.ino (Original)
This example shows several ways to set up and use 'palettes' of colors
with FastLED.
These compact palettes provide an easy way to re-colorize your
animation on the fly, quickly, easily, and with low overhead.
USING palettes is MUCH simpler in practice than in theory, so first just
run this sketch, and watch the pretty lights as you then read through
the code. Although this sketch has eight (or more) different color schemes,
the entire sketch compiles down to about 6.5K on AVR.
FastLED provides a few pre-configured color palettes, and makes it
extremely easy to make up your own color schemes with palettes.
Some notes on the more abstract 'theory and practice' of
FastLED compact palettes are at the bottom of this file.
Created: June 19, 2014 (Based on GitHub commit)
Author: kriegsman
===========================================================================
Used to test the WS2812 RGB LED on the RA6M5 Thing Plus in direct sunlight.
The sketch implements an interrupt on the user button to increment the
brightness of the LED. The brightness value is displayed on a Qwiic OLED
Display (https://www.sparkfun.com/products/24606) connected by a Qwiic
cable.
Modified: April 11, 2024
By SparkFun Electronics
Author: Wes F
*/
#include <FastLED.h> // http://librarymanager/All#FastLED
#include <SparkFun_Qwiic_OLED.h> // http://librarymanager/All#SparkFun_Qwiic_OLED
QwiicNarrowOLED myOLED; //
#define LED_PIN 13
#define NUM_LEDS 1
// #define BRIGHTNESS 60
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
int BRIGHTNESS = 75;
void setup() {
delay(500); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
pinMode(31, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(31), ChangeBrightness, HIGH);
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
Wire.begin();
// Initalize the OLED device and related graphics system
if (myOLED.begin() == false) {
while (true)
;
}
}
void loop() {
FastLED.setBrightness(BRIGHTNESS);
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors(startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
myOLED.erase();
myOLED.text(0, 0, "Brightness: " + String(BRIGHTNESS), 1);
myOLED.display();
}
void ChangeBrightness() {
BRIGHTNESS = BRIGHTNESS + 20;
if (BRIGHTNESS > 255) {
BRIGHTNESS = 75;
}
}
void FillLEDsFromPaletteColors(uint8_t colorIndex) {
uint8_t brightness = 255;
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = ColorFromPalette(currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly. All are shown here.
void ChangePalettePeriodically() {
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if (lastSecond != secondHand) {
lastSecond = secondHand;
if (secondHand == 0) {
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
if (secondHand == 10) {
currentPalette = RainbowStripeColors_p;
currentBlending = NOBLEND;
}
if (secondHand == 15) {
currentPalette = RainbowStripeColors_p;
currentBlending = LINEARBLEND;
}
if (secondHand == 20) {
SetupPurpleAndGreenPalette();
currentBlending = LINEARBLEND;
}
if (secondHand == 25) {
SetupTotallyRandomPalette();
currentBlending = LINEARBLEND;
}
if (secondHand == 30) {
SetupBlackAndWhiteStripedPalette();
currentBlending = NOBLEND;
}
if (secondHand == 35) {
SetupBlackAndWhiteStripedPalette();
currentBlending = LINEARBLEND;
}
if (secondHand == 40) {
currentPalette = CloudColors_p;
currentBlending = LINEARBLEND;
}
if (secondHand == 45) {
currentPalette = PartyColors_p;
currentBlending = LINEARBLEND;
}
if (secondHand == 50) {
currentPalette = myRedWhiteBluePalette_p;
currentBlending = NOBLEND;
}
if (secondHand == 55) {
currentPalette = myRedWhiteBluePalette_p;
currentBlending = LINEARBLEND;
}
}
}
// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette() {
for (int i = 0; i < 16; ++i) {
currentPalette[i] = CHSV(random8(), 255, random8());
}
}
// This function sets up a palette of black and white stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette() {
// 'black out' all 16 palette entries...
fill_solid(currentPalette, 16, CRGB::Black);
// and set every fourth one to white.
currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White;
}
// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette() {
CRGB purple = CHSV(HUE_PURPLE, 255, 255);
CRGB green = CHSV(HUE_GREEN, 255, 255);
CRGB black = CRGB::Black;
currentPalette = CRGBPalette16(
green, green, black, black,
purple, purple, black, black,
green, green, black, black,
purple, purple, black, black);
}
// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM. A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM = {
CRGB::Red,
CRGB::Gray, // 'white' is too bright compared to red and blue
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Gray,
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Red,
CRGB::Gray,
CRGB::Gray,
CRGB::Blue,
CRGB::Blue,
CRGB::Black,
CRGB::Black
};
// Additional notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color. You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative. FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.
For more information, please refer to the WS2812 datasheet and FastLED Arduino library.
Digital Input
Buttons are great for learning about digital inputs; and are often paired with LEDs, as a visual indicator for their operation.
Button - USRBTN
On the RA6M5 Thing Plus, the USRBTN button is the perfect component for operating a digital input. There are several built-in examples in the Arduino IDE for the basic use of a button as an input device. These examples utilize a digital intput to toggle an LED and/or generate an output text on the USB serial port. Users can find these sketches in the File > Examples > 01.Basics > DigitalReadSerial and File > Examples > 02.Digital drop-down menus.
DigitalReadSerial.ino
Button.ino
Debounce.ino
InputPullupSerial.ino
Tip
Users will need to modify the sketches below, for the GPIO pin that is connected to the STAT
LED and USRBTN. As previously, they can use the predefined LED_BUILTIN
variable for the GPIO pin that is connected to the STAT
LED. For the GPIO pin that is connected to the USRBTN, users till need to modify the pin to D31
.
For more details, expand the boxes below to see the modifications that required to replace the highlighted values, in order to define the GPIO pins connected to the STAT
LEDand USRBTN
USRBTN
DigitalReadSerial.ino
int pushButton = 2;
int pushButton = 31;
Button.ino
and Debounce.ino
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin = 31; // the number of the pushbutton pin
InputPullupSerial.ino
pinMode(2, INPUT_PULLUP);
pinMode(31, INPUT_PULLUP);
int sensorVal = digitalRead(2);
int sensorVal = digitalRead(31);
STAT
LED
Button.ino
and Debounce.ino
const int ledPin = 13; // the number of the LED pin
const int ledPin = LED_BUILTIN; // the number of the LED pin
InputPullupSerial.ino
pinMode(13, OUTPUT);
digitalWrite(13, value);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, value);
Users can find this sketch in the File > Examples > 01.Basics > DigitalReadSerial drop-down menu.
DigitalReadSerial.ino
Users can find this sketch in the File > Examples > 02.Digital > Button drop-down menu.
Button.ino
Users can find this sketch in the File > Examples > 02.Digital > Debounce drop-down menu.
Debounce.ino
Users can find this sketch in the File > Examples > 02.Digital > InputPullupSerial drop-down menu.
InputPullupSerial.ino
Interrupts
The RA6M5 provides IRQ support on several of its digital input pins. This feature is great for creating an instantaneous response to a digital input, without having to wait for the MCU to complete its current task.
Code
const byte ledPin = LED_BUILTIN;
const byte interruptPin = 31;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Arduino
In the Arduino IDE, interrupt requests are configured using the attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
function, where:
pin
- The GPIO pinISR
- The interrupt service routine to call/execute when the interrupt occursmode
- Defines how the interrupt should be triggered:LOW
- When the pin is LOWCHANGE
- Whenever the pin changes valueRISING
- When the pin changes from LOW to HIGHFALLING
- When the pin changes from HIGH to LOW
Warning
When utilizing an interrupt on a digital GPIO, the attached interrupt service routine should be able to execute within the shortest, possible time frame. Otherwise, the suspended task could trigger various faults or errors.