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;
}
}
}