In this sketch, which is based on the FastLED "100-lines-of-code" demo reel, there is a typedef between the setup() and loop() functions.
If I move this code block to before the setup, I get this error:
'rainbow' was not declared in this scope
But when the block is between the setup and loop functions, it compiles and runs just fine.
Why??
All of the functions not in scope are defined below the loop function So, what difference does it make to the precompiler where the typedef appears?
#define SKETCH "XmasTree2_OTA.ino"
#include <FastLED.h>
#include "ESP8266WiFi.h"
#include "D:\River Documents\Arduino\libraries\Kaywinnet.h"
#include <ArduinoOTA.h>
FASTLED_USING_NAMESPACE
// Inspired by
// FastLED "100-lines-of-code" demo reel, showing just a few
// of the kinds of animation patterns you can quickly and easily
// compose using FastLED.
//
// This example also shows one easy way to define multiple
// animations patterns and have them automatically rotate.
//
// -Mark Kriegsman, December 2014
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
/*
This is the sketch finally used on the Christmas Tree with a topper LED.
The topper LED is on pin D1.
*/
#define TOP_PIN D1
#define DATA_PIN D2
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
#define NUM_LEDS 50
CRGB leds[NUM_LEDS];
CRGB top[1]; // Tree topper, one LED
#define BRIGHTNESS 50
#define FRAMES_PER_SECOND 120
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
// --------------------------- setup ---------------------------
void setup() {
delay(1000); // No one knows why.
setup_wifi();
start_OTA();
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, TOP_PIN, COLOR_ORDER>(top, 1).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
// Init the topper
top[0] = CRGB::Yellow;
FastLED.show();
}
// ------------- List of patterns -------------
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
// Removed confetti and sinelon from the original sample.
SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, juggle, bpm };
// --------------------------- loop ---------------------------
void loop() {
ArduinoOTA.handle();
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
FastLED.show();
FastLED.delay(1000 / FRAMES_PER_SECOND);
EVERY_N_MILLISECONDS( 20 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
EVERY_N_SECONDS( 10 ) {
nextPattern(); // change patterns periodically
}
}
// ------------- nextPattern -------------
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
// ------------- Rainbow -------------
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
//------------- Rainbow with glitter -------------
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
//------------- addGlitter -------------
void addGlitter( fract8 chanceOfGlitter)
{
if ( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
//------------- confetti -------------
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
}
//------------- sinelon -------------
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
leds[pos] += CHSV( gHue, 255, 192);
}
//------------- bpm -------------
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for ( int i = 0; i < NUM_LEDS; i++) { //9948
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
}
}
//------------- juggle -------------
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for ( int i = 0; i < 8; i++) {
leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}