Help with project combining code for LEDS & Ultrasonic Sensor

Hello all,
I am relatively new to using Arduino, and am trying to create an LED display using an ultrasonic sensor. I have made one before, that simply changed the colors from rainbow to white on my strip lights as someone moved closer to the sensor. I am now trying to make one that utilizes the sensor and changes the strips lights from showing rainbow, to twinkling rainbow, then showing a solid color randomly from the options: green, blue, yellow, and red. I successfully got the code to work, minus the twinkle aspect, which is what I am struggling with. I will post the initial project without my mess trying to incorporate the twinkling lights. I did try to incorporate some from the example Twinkle Fox, so if you would like for me to post that set of code so you can see what I was trying, I am happy to do that as well. If anyone is able to help me figure out the twinkle aspect, even if they just twinkle solid white instead of rainbow, it would be appreciated! Again, my goal is for the lights to show rainbow until triggered by the ultrasonic sensor, then twinkle, then show a solid color from the options at random.

#include <Arduino.h>
#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 100
#define COLOR_ORDER GRB
#define DATA_PIN 6

Ultrasonic ultrasonic1(9, 10);
CRGB leds[NUM_LEDS];
int Pot = A0;
int PotVal = 0;
int OutVal = 0;

uint8_t hue = 0;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  randomSeed(analogRead(0));
}

void loop() {
  PotVal = analogRead(Pot);
  OutVal = map(PotVal, 0, 1023, 0, 255);

  if (ultrasonic1.read() >= 30) {
    for (int i = 0; i < NUM_LEDS; i++)
      //leds[i] = CHSV(hue, 255, 255);
      leds[i] = CHSV(hue + (i * 10), 255, 255);
    ;
    analogWrite(NUM_LEDS, OutVal);
    FastLED.setBrightness(OutVal);
    EVERY_N_MILLISECONDS(15)
    hue++;
    FastLED.show();
    delay(1000);
  }

  else {
    long rand;
    int pause;
    rand = random(1, 8);  //choose random value 1-4
    switch (rand) {
      case 1:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Yellow);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 2:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Blue);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 3:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Green);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 4:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 5:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Yellow);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 6:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Blue);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 7:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Green);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 8:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;
    }
  }
}

This makes no sense at all. There is no pin 100!

Variables of type long can hold numbers from about -2,000 million to +2,000 million. You need it to hold a number in the range 1 to 8. Why did you choose long?

Sorry if I seem to be nit-picking, I'm reading through your code, and some things kind of stick out like a sore thumb!

That's fine, I appreciate you pointing out my mistakes. As I said, I am relatively new, so I was using several different guides from blogs and other questions throughout this forum over the years to try to assemble my code. I didn't realize I had written something wrong, especially since it worked how I needed it to! If there are any other clear mistakes, please let me know! I am eager to learn more and hopefully make less mistakes in the future.

As a first step, I've shortened your code by removing all the duplication and the unnecessary switch-case, replacing it with an array:

#include <Arduino.h>
#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 100
#define COLOR_ORDER GRB
#define DATA_PIN 6

Ultrasonic ultrasonic1(9, 10);
CRGB leds[NUM_LEDS];
int Pot = A0;
int PotVal = 0;
int OutVal = 0;

uint8_t hue = 0;
CRGB myColours[8] = {
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red,
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red
};

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  randomSeed(analogRead(0));
}

void loop() {
  PotVal = analogRead(Pot);
  OutVal = map(PotVal, 0, 1023, 0, 255);

  if (ultrasonic1.read() >= 30) {
    for (int i = 0; i < NUM_LEDS; i++)
      //leds[i] = CHSV(hue, 255, 255);
      leds[i] = CHSV(hue + (i * 10), 255, 255);
    FastLED.setBrightness(OutVal);
    EVERY_N_MILLISECONDS(15)
    hue++;
    FastLED.show();
    delay(1000);
  }

  else {
    byte rand = random(0, 7);  //choose random value 0-7
    delay(2000);
    fill_solid(leds, NUM_LEDS, myColours[rand]);
    FastLED.setBrightness(OutVal);
    FastLED.show();
    delay(10000);
  }
}
1 Like

So any distance greater than 30cm is considered not to be triggered?

By "then" do you mean later in time or closer in distance?

Yes. I am creating a lights display. So when it is plugged in and set up, I would like the rainbow lights to show. I will have a chair set up in front, which is why the distance is so small right now. I want it to stay rainbow until something is basically directly in front of the sensor, and then that would trigger the random color. The twinkle effect is just something I would like to add to create "suspense" before the random color is revealed. So later in time, but with the same distance trigger. Let me know if that makes sense!

Ok, so the twinkle effect can just go in the else part, before the code for the solid colour.

So how would you describe "twinkling rainbow" in more detail?

Okay, so this is where I really am over my head and get confused. I am sorry I am such a newbie! There is a Fast LED library example called Twinkle Fox, and the color palette RainbowColors_p is what I would like to use. The twinkle effect requires coding for the cycles, brightness, etc. The original example cycles through a list of palettes, so I just chopped it up so that it only shows the one that I want. With all of this extra code, I was getting extremely confused on how to mesh the two together without messing up my code for the sensor and random color pick. I went down the route of trying to have it in a separate tab so the code doesn't look so intimidating together, but still was struggling to mesh them. Since it is pulled from an example, I don't understand each piece enough to know how to simplify it better. I will include that code below:

#include "FastLED.h"

#define NUM_LEDS 50
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define DATA_PIN 3
//#define CLK_PIN       4
#define VOLTS 12
#define MAX_MA 4000

CRGBArray<NUM_LEDS> leds;
#define TWINKLE_SPEED 5
#define TWINKLE_DENSITY 6
#define SECONDS_PER_PALETTE 30
#define AUTO_SELECT_BACKGROUND_COLOR 1
#define COOL_LIKE_INCANDESCENT 1

CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;

void setup() {
  delay(3000);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_MA);
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
    .setCorrection(TypicalLEDStrip);

  chooseNextColorPalette(gTargetPalette);
}


void loop() {
  EVERY_N_SECONDS(SECONDS_PER_PALETTE) {
    chooseNextColorPalette(gTargetPalette);
  }

  EVERY_N_MILLISECONDS(10) {
    nblendPaletteTowardPalette(gCurrentPalette, gTargetPalette, 12);
  }

  drawTwinkles(leds);

  FastLED.show();
}

void drawTwinkles(CRGBSet& L) {
  uint16_t PRNG16 = 11337;

  uint32_t clock32 = millis();
  CRGB bg;
  if ((AUTO_SELECT_BACKGROUND_COLOR == 1) && (gCurrentPalette[0] == gCurrentPalette[1])) {
    bg = gCurrentPalette[0];
    uint8_t bglight = bg.getAverageLight();
    if (bglight > 64) {
      bg.nscale8_video(16);  // very bright, so scale to 1/16th
    } else if (bglight > 16) {
      bg.nscale8_video(64);  // not that bright, so scale to 1/4th
    } else {
      bg.nscale8_video(86);  // dim, scale to 1/3rd.
    }
  }

  uint8_t backgroundBrightness = bg.getAverageLight();

  for (CRGB& pixel : L) {
    PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;  // next 'random' number
    uint16_t myclockoffset16 = PRNG16;          // use that number as clock offset
    PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;  // next 'random' number
    // use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
    uint8_t myspeedmultiplierQ5_3 = ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
    uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
    uint8_t myunique8 = PRNG16 >> 8;  // get 'salt' value for this pixel

    CRGB c = computeOneTwinkle(myclock30, myunique8);

    uint8_t cbright = c.getAverageLight();
    int16_t deltabright = cbright - backgroundBrightness;
    if (deltabright >= 32 || (!bg)) {
      // If the new pixel is significantly brighter than the background color,
      // use the new color.
      pixel = c;
    } else if (deltabright > 0) {
      // If the new pixel is just slightly brighter than the background color,
      // mix a blend of the new color and the background color
      pixel = blend(bg, c, deltabright * 8);
    } else {
      // if the new pixel is not at all brighter than the background color,
      // just use the background color.
      pixel = bg;
    }
  }
}

CRGB computeOneTwinkle(uint32_t ms, uint8_t salt) {
  uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
  uint8_t fastcycle8 = ticks;
  uint16_t slowcycle16 = (ticks >> 8) + salt;
  slowcycle16 += sin8(slowcycle16);
  slowcycle16 = (slowcycle16 * 2053) + 1384;
  uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);

  uint8_t bright = 0;
  if (((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
    bright = attackDecayWave8(fastcycle8);
  }

  uint8_t hue = slowcycle8 - salt;
  CRGB c;
  if (bright > 0) {
    c = ColorFromPalette(gCurrentPalette, hue, bright, NOBLEND);
    if (COOL_LIKE_INCANDESCENT == 1) {
      coolLikeIncandescent(c, fastcycle8);
    }
  } else {
    c = CRGB::Black;
  }
  return c;
}

uint8_t attackDecayWave8(uint8_t i) {
  if (i < 86) {
    return i * 3;
  } else {
    i -= 86;
    return 255 - (i + (i / 2));
  }
}

void coolLikeIncandescent(CRGB& c, uint8_t phase) {
  if (phase < 128) return;

  uint8_t cooling = (phase - 128) >> 4;
  c.g = qsub8(c.g, cooling);
  c.b = qsub8(c.b, cooling * 2);
}


const TProgmemRGBPalette16* ActivePaletteList[] = {

  &RainbowColors_p,
  &PartyColors_p,
};

void chooseNextColorPalette(CRGBPalette16& pal) {
  const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
  static uint8_t whichPalette = -1;
  whichPalette = addmod8(whichPalette, 1, numberOfPalettes);

  pal = *(ActivePaletteList[whichPalette]);
}

This was one of my (probably embarrassing) attempts to Frankenstein the two together. I also tried to put the twinkle under each case at one point so it would come before the color in each one.

It does verify without errors, but does not function how it should.

#include <Arduino.h>
#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 100
#define COLOR_ORDER GRB
#define DATA_PIN 6

Ultrasonic ultrasonic1(9, 10);
CRGB leds[NUM_LEDS];
int Pot = A0;
int PotVal = 0;
int OutVal = 0;

uint8_t hue = 0;

// Twinkle Rainbow parameters
#define TWINKLE_SPEED 5
#define TWINKLE_DENSITY 6
#define SECONDS_PER_PALETTE 30
#define AUTO_SELECT_BACKGROUND_COLOR 1
#define COOL_LIKE_INCANDESCENT 1

CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  randomSeed(analogRead(0));
  FastLED.setMaxPowerInVoltsAndMilliamps(12, 4000);
  chooseNextColorPalette(gTargetPalette);
}

void loop() {
  PotVal = analogRead(Pot);
  OutVal = map(PotVal, 0, 1023, 0, 255);

  if (ultrasonic1.read() >= 30) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue + (i * 10), 255, 255);
    }
    analogWrite(NUM_LEDS, OutVal);
    FastLED.setBrightness(OutVal);
    EVERY_N_MILLISECONDS(15)
    hue++;
    FastLED.show();
    delay(1000);
  } else {
    long rand;
    int pause;
    rand = random(1, 9);

    twinkleRainbow(3000);

    switch (rand) {
      case 1:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Yellow);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 2:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Blue);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 3:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Green);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 4:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 5:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Yellow);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 6:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Blue);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 7:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Green);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;

      case 8:
        delay(2000);
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        analogWrite(NUM_LEDS, OutVal);
        FastLED.setBrightness(OutVal);
        FastLED.show();
        delay(10000);
        break;
    }
  }
}
void showColor(CRGB color, int duration) {
  fill_solid(leds, NUM_LEDS, color);
  analogWrite(NUM_LEDS, OutVal);
  FastLED.setBrightness(OutVal);
  FastLED.show();
  delay(duration);
}

void twinkleRainbow(int duration) {
  unsigned long start = millis();
  while (millis() - start < duration) {
    EVERY_N_SECONDS(SECONDS_PER_PALETTE) {
      chooseNextColorPalette(gTargetPalette);
    }
    EVERY_N_MILLISECONDS(10) {
      nblendPaletteTowardPalette(gCurrentPalette, gTargetPalette, 12);
    }
  }
}

void drawTwinkles(CRGBSet& L) {
  uint16_t PRNG16 = 11337;
  uint32_t clock32 = millis();
  CRGB bg;
  if ((AUTO_SELECT_BACKGROUND_COLOR == 1) && (gCurrentPalette[0] == gCurrentPalette[1])) {
    bg = gCurrentPalette[0];
    uint8_t bglight = bg.getAverageLight();
    if (bglight > 64) {
      bg.nscale8_video(16);
    } else if (bglight > 16) {
      bg.nscale8_video(64);
    } else {
      bg.nscale8_video(86);
    }
  }

  uint8_t backgroundBrightness = bg.getAverageLight();

  for (CRGB& pixel : L) {
    PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
    uint16_t myclockoffset16 = PRNG16;
    PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
    uint8_t myspeedmultiplierQ5_3 = ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
    uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
    uint8_t myunique8 = PRNG16 >> 8;

    CRGB c = computeOneTwinkle(myclock30, myunique8);

    uint8_t cbright = c.getAverageLight();
    int16_t deltabright = cbright - backgroundBrightness;
    if (deltabright >= 32 || (!bg)) {
      pixel = c;
    } else if (deltabright > 0) {
      pixel = blend(bg, c, deltabright * 8);
    } else {
      pixel = bg;
    }
  }
}

CRGB computeOneTwinkle(uint32_t ms, uint8_t salt) {
  uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
  uint8_t fastcycle8 = ticks;
  uint16_t slowcycle16 = (ticks >> 8) + salt;
  slowcycle16 += sin8(slowcycle16);
  slowcycle16 = (slowcycle16 * 2053) + 1384;
  uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);

  uint8_t bright = 0;
  if (((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
    bright = attackDecayWave8(fastcycle8);
  }

  uint8_t hue = slowcycle8 - salt;
  CRGB c;
  if (bright > 0) {
    c = ColorFromPalette(gCurrentPalette, hue, bright, NOBLEND);
    if (COOL_LIKE_INCANDESCENT == 1) {
      coolLikeIncandescent(c, fastcycle8);
    }
  } else {
    c = CRGB::Black;
  }
  return c;
}

uint8_t attackDecayWave8(uint8_t i) {
  if (i < 86) {
    return i * 3;
  } else {
    i -= 86;
    return 255 - (i + (i / 2));
  }
}

void coolLikeIncandescent(CRGB& c, uint8_t phase) {
  if (phase < 128) return;

  uint8_t cooling = (phase - 128) >> 4;
  c.g = qsub8(c.g, cooling);
  c.b = qsub8(c.b, cooling * 2);
}

const TProgmemRGBPalette16* ActivePaletteList[] = {
  &RainbowColors_p,
  &PartyColors_p,
};

void chooseNextColorPalette(CRGBPalette16& pal) {
  const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
  static uint8_t whichPalette = -1;
  whichPalette = addmod8(whichPalette, 1, numberOfPalettes);
  pal = *(ActivePaletteList[whichPalette]);
}

Just spotted this error.

The idea of this line of code is to make the random numbers generated by the rand() function less predictable, by giving it a genuinely random number to use as a starting point. Reading a value from an analog pin, which is not connected to anything, provides that because the unconnected pin will pick up random signals from the environment.

Problem is, analogRead(0) is the same as analogRead(A0), which is the pin your pot is connected to. So it won't be very random at all.

Thank you for explaining that! I found that it was repeating the same sequence of colors pretty often, and wanted it to be more random. I found that bit from another question in here but didn't realize that is how it functions. So if I wanted it to work, would I just change it to randomSeed (analogRead(1))?

1 Like

Not a bad effort! But you missed something.

I can see that you took the contents of loop() from the original Twinkle Fox code and made a new function called twinkleRainbow(). You gave that function a parameter to tell it how long to run, which it does. And you call twinkleRainbow() at the correct point in loop(). Great so far.

But if you compare the code in loop() from the Twinkle Fox example with the code in twinkleRainbow() in your code, your code is missing a couple of very important lines.

I think you're referring to drawTwinkles( leds); and FastLED.show(); ?
The twinkles line shows an error of invalid initialization. It seemed like it was something from my initial defining of leds at the top. I didn't see any repeat language so I wasn't sure how I needed to rewrite it.

Yes

Please post the complete set of error messages, using code tags.

Here are the error messages:
Let me know if you need any other information

C:\Users\Library\AppData\Local\Temp\.arduinoIDE-unsaved2024416-14216-1eza9zw.1gow\sketch_may16a\sketch_may16a.ino: In function 'void twinkleRainbow(int)':
C:\Users\Library\AppData\Local\Temp\.arduinoIDE-unsaved2024416-14216-1eza9zw.1gow\sketch_may16a\sketch_may16a.ino:147:21: error: invalid initialization of reference of type 'CRGBSet& {aka CPixelView<CRGB>&}' from expression of type 'CRGB [100]'
   drawTwinkles( leds);
                     ^
C:\Users\Library\AppData\Local\Temp\.arduinoIDE-unsaved2024416-14216-1eza9zw.1gow\sketch_may16a\sketch_may16a.ino:153:6: note: in passing argument 1 of 'void drawTwinkles(CRGBSet&)'
 void drawTwinkles(CRGBSet& L) {
      ^~~~~~~~~~~~

exit status 1

Compilation error: invalid initialization of reference of type 'CRGBSet& {aka CPixelView<CRGB>&}' from expression of type 'CRGB [100]'

I think it may be because of the different way that leds is defined in the Twinkle Fox code compared to your code. Try that other way. If you get more errors, post those.

I tried combining the code with your simplified edit earlier in this thread. There are a ton of errors coming through about the function definitions from the twinkle fox code. I am not sure where I need to move each section so that it functions how it should. I will post the code and the error logs.

#include <Arduino.h>
#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 100
#define COLOR_ORDER GRB
#define DATA_PIN 6

// Twinkle Rainbow parameters
#define TWINKLE_SPEED 5
#define TWINKLE_DENSITY 6
#define SECONDS_PER_PALETTE 30
#define AUTO_SELECT_BACKGROUND_COLOR 1
#define COOL_LIKE_INCANDESCENT 1

CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;

Ultrasonic ultrasonic1(9, 10);
CRGB leds[NUM_LEDS];
int Pot = A0;
int PotVal = 0;
int OutVal = 0;

uint8_t hue = 0;
CRGB myColours[8] = {
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red,
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red
};

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  randomSeed(analogRead(1));
  FastLED.setMaxPowerInVoltsAndMilliamps(12, 4000);
}

void loop() {
  PotVal = analogRead(Pot);
  OutVal = map(PotVal, 0, 1023, 0, 255);

  if (ultrasonic1.read() >= 30) {
    for (int i = 0; i < NUM_LEDS; i++)
      //leds[i] = CHSV(hue, 255, 255);
      leds[i] = CHSV(hue + (i * 10), 255, 255);
    FastLED.setBrightness(OutVal);
    EVERY_N_MILLISECONDS(15)
    hue++;
    FastLED.show();
    delay(1000);
  }


  else {
    byte rand = random(0, 7);  //choose random value 0-7
    delay(2000);
    twinkleRainbow(3000);
    fill_solid(leds, NUM_LEDS, myColours[rand]);
    FastLED.setBrightness(OutVal);
    FastLED.show();
    delay(10000);
  }
}
void showColor(CRGB color, int duration) {
  fill_solid(leds, NUM_LEDS, color);
  analogWrite(NUM_LEDS, OutVal);
  FastLED.setBrightness(OutVal);
  FastLED.show();
  delay(duration);
}

void twinkleRainbow(int duration) {
  unsigned long start = millis();
  while (millis() - start < duration) {
    EVERY_N_SECONDS(SECONDS_PER_PALETTE) {
      chooseNextColorPalette(gTargetPalette);
    }
    EVERY_N_MILLISECONDS(10) {
      nblendPaletteTowardPalette(gCurrentPalette, gTargetPalette, 12);
    }
    drawTwinkles();

    FastLED.show();
  }
  void drawTwinkles(CRGBSet & L) {
    uint16_t PRNG16 = 11337;
    uint32_t clock32 = millis();
    CRGB bg;
    if ((AUTO_SELECT_BACKGROUND_COLOR == 1) && (gCurrentPalette[0] == gCurrentPalette[1])) {
      bg = gCurrentPalette[0];
      uint8_t bglight = bg.getAverageLight();
      if (bglight > 64) {
        bg.nscale8_video(16);
      } else if (bglight > 16) {
        bg.nscale8_video(64);
      } else {
        bg.nscale8_video(86);
      }
    }
    uint8_t backgroundBrightness = bg.getAverageLight();
    for (CRGB& pixel : L) {
      PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
      uint16_t myclockoffset16 = PRNG16;
      PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
      uint8_t myspeedmultiplierQ5_3 = ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
      uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
      uint8_t myunique8 = PRNG16 >> 8;

      CRGB c = computeOneTwinkle(myclock30, myunique8);

      uint8_t cbright = c.getAverageLight();
      int16_t deltabright = cbright - backgroundBrightness;
      if (deltabright >= 32 || (!bg)) {
        pixel = c;
      } else if (deltabright > 0) {
        pixel = blend(bg, c, deltabright * 8);
      } else {
        pixel = bg;
      }
    }
  }


  CRGB computeOneTwinkle(uint32_t ms, uint8_t salt) {
    uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
    uint8_t fastcycle8 = ticks;
    uint16_t slowcycle16 = (ticks >> 8) + salt;
    slowcycle16 += sin8(slowcycle16);
    slowcycle16 = (slowcycle16 * 2053) + 1384;
    uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);

    uint8_t bright = 0;
    if (((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
      bright = attackDecayWave8(fastcycle8);
    }

    uint8_t hue = slowcycle8 - salt;
    CRGB c;
    if (bright > 0) {
      c = ColorFromPalette(gCurrentPalette, hue, bright, NOBLEND);
      if (COOL_LIKE_INCANDESCENT == 1) {
        coolLikeIncandescent(c, fastcycle8);
      }
    } else {
      c = CRGB::Black;
    }
    return c;
  }
  uint8_t attackDecayWave8(uint8_t i) {
    if (i < 86) {
      return i * 3;
    } else {
      i -= 86;
      return 255 - (i + (i / 2));
    }
  }

void coolLikeIncandescent(CRGB & c, uint8_t phase) {
    if (phase < 128) return;

    uint8_t cooling = (phase - 128) >> 4;
    c.g = qsub8(c.g, cooling);
    c.b = qsub8(c.b, cooling * 2);
}

const TProgmemRGBPalette16* ActivePaletteList[] = {
    &RainbowColors_p,
    &PartyColors_p,
  };

void chooseNextColorPalette(CRGBPalette16 & pal) {
    const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
    static uint8_t whichPalette = -1;
    whichPalette = addmod8(whichPalette, 1, numberOfPalettes);
    pal = *(ActivePaletteList[whichPalette]);
  }
}
C:\Users\Library\Desktop\HPEdit\HPEdit.ino: In function 'void twinkleRainbow(int)':
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:81:7: error: 'chooseNextColorPalette' was not declared in this scope
       chooseNextColorPalette(gTargetPalette);
       ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:86:5: error: 'drawTwinkles' was not declared in this scope
     drawTwinkles();
     ^~~~~~~~~~~~
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:90:34: error: a function-definition is not allowed here before '{' token
   void drawTwinkles(CRGBSet & L) {
                                  ^
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:129:53: error: a function-definition is not allowed here before '{' token
   CRGB computeOneTwinkle(uint32_t ms, uint8_t salt) {
                                                     ^
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:154:39: error: a function-definition is not allowed here before '{' token
   uint8_t attackDecayWave8(uint8_t i) {
                                       ^
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:163:52: error: a function-definition is not allowed here before '{' token
 void coolLikeIncandescent(CRGB & c, uint8_t phase) {
                                                    ^
C:\Users\Library\Desktop\HPEdit\HPEdit.ino:176:50: error: a function-definition is not allowed here before '{' token
 void chooseNextColorPalette(CRGBPalette16 & pal) {
                                                  ^

exit status 1

Compilation error: 'chooseNextColorPalette' was not declared in this scope

I assume you are using Tools->Auto Format?

The above is a clue as to what's wrong.

That void should be in column 1, but it's indented by 2 spaces ... Auto Format will have done that, because it's going off the placement of { and }. So if you ever see functions that are indented and not in column 1, you know you have missed a } somewhere.

Other clues are 2 } in vertical alignment in column 1, or the last } in the sketch not being in column 1.

In the IDE, if you put the cursor next to a { or }, it will highlight where it thinks the corresponding } or { is, which is helpful in checking for errors.

Okay, I had read in the forum that we are supposed to Auto Format before posting. Now I know to check that. I think I fixed the formatting and have kept tinkering with the twinkle fox code that is mixed in to try to get rid of some of the error messages. I am not sure how to fix it. I found another post in the forum where several other people had issues with the Twinkle Fox example and those same things (drawTwinkles) were causing errors. They didn't say how they were able to fix it in the comments.

#include <Arduino.h>
#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 100
#define COLOR_ORDER GRB
#define DATA_PIN 6
#define LED_TYPE   WS2811
#define TWINKLE_SPEED 5
#define TWINKLE_DENSITY 6
#define AUTO_SELECT_BACKGROUND_COLOR 1
#define COOL_LIKE_INCANDESCENT 1

CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;

Ultrasonic ultrasonic1(9, 10);
CRGB leds[NUM_LEDS];
int Pot = A0;
int PotVal = 0;
int OutVal = 0;

uint8_t hue = 0;
CRGB myColours[8] = {
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red,
  CRGB::Yellow,
  CRGB::Blue,
  CRGB::Green,
  CRGB::Red
};

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS);
   randomSeed(analogRead(1));
  FastLED.setMaxPowerInVoltsAndMilliamps(12, 4000);
}

void loop() {
  PotVal = analogRead(Pot);
  OutVal = map(PotVal, 0, 1023, 0, 255);

  if (ultrasonic1.read() >= 30) {
    for (int i = 0; i < NUM_LEDS; i++)
      //leds[i] = CHSV(hue, 255, 255);
      leds[i] = CHSV(hue + (i * 10), 255, 255);
    FastLED.setBrightness(OutVal);
    EVERY_N_MILLISECONDS(15)
    hue++;
    FastLED.show();
    delay(1000);
  }


  else {
    byte rand = random(0, 7);  //choose random value 0-7
    delay(2000);
    twinkleRainbow(3000);
    fill_solid(leds, NUM_LEDS, myColours[rand]);
    FastLED.setBrightness(OutVal);
    FastLED.show();
    delay(10000);
  }
}

void showColor(CRGB color, int duration) {
  fill_solid(leds, NUM_LEDS, color);
  analogWrite(NUM_LEDS, OutVal);
  FastLED.setBrightness(OutVal);
  FastLED.show();
  delay(duration);
}

void twinkleRainbow(int duration) {
unsigned long start = millis();
drawTwinkles(leds);
FastLED.show();
}

const TProgmemRGBPalette16* ActivePaletteList[] = {
  &RainbowColors_p,
  &PartyColors_p,
};

void drawTwinkles(leds) {
    uint16_t PRNG16 = 11337;
    uint32_t clock32 = millis();
    CRGB bg;
    if ((AUTO_SELECT_BACKGROUND_COLOR == 1) && 
    (gCurrentPalette[0] == gCurrentPalette[1])) {
    bg = gCurrentPalette[0];
    uint8_t bglight = bg.getAverageLight();
    if( bglight > 64) {
      bg.nscale8_video( 16); 
    } else if( bglight > 16) {
      bg.nscale8_video( 64);
    } else {
      bg.nscale8_video( 86); 
    }
 }

  uint8_t backgroundBrightness = bg.getAverageLight();
  for(leds) {
      PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
      uint16_t myclockoffset16 = PRNG16;
      PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384;
      uint8_t myspeedmultiplierQ5_3 = ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
      uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
      uint8_t myunique8 = PRNG16 >> 8;

      CRGB c = computeOneTwinkle(myclock30, myunique8);

      uint8_t cbright = c.getAverageLight();
      int16_t deltabright = cbright - backgroundBrightness;
      if (deltabright >= 32 || (!bg)) {
        leds = c;
      } else if (deltabright > 0) {
        leds = blend(bg, c, deltabright * 8);
      } else {
        leds = bg;
      }
    }
}

CRGB computeOneTwinkle(uint32_t ms, uint8_t salt) {
    uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
    uint8_t fastcycle8 = ticks;
    uint16_t slowcycle16 = (ticks >> 8) + salt;
    slowcycle16 += sin8(slowcycle16);
    slowcycle16 = (slowcycle16 * 2053) + 1384;
    uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);

    uint8_t bright = 0;
    if (((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
      bright = attackDecayWave8(fastcycle8);
    }

    uint8_t hue = slowcycle8 - salt;
    CRGB c;
    if (bright > 0) {
      c = ColorFromPalette(gCurrentPalette, hue, bright, NOBLEND);
      if (COOL_LIKE_INCANDESCENT == 1) {
        coolLikeIncandescent(c, fastcycle8);
      }
    } else {
      c = CRGB::Black;
    }
    return c;
}


uint8_t attackDecayWave8(uint8_t i) {
    if (i < 86) {
      return i * 3;
    } else {
      i -= 86;
      return 255 - (i + (i / 2));
    }
}

void coolLikeIncandescent(CRGB & c, uint8_t phase) {
    if (phase < 128) return;

    uint8_t cooling = (phase - 128) >> 4;
    c.g = qsub8(c.g, cooling);
    c.b = qsub8(c.b, cooling * 2);
}

In function 'void twinkleRainbow(int)':

error: variable or field 'drawTwinkles' declared void