Skip to content

Instantly share code, notes, and snippets.

@koalahamlet
Created January 11, 2022 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koalahamlet/91cc7c27f706e507b4e03fcb13ada079 to your computer and use it in GitHub Desktop.
Save koalahamlet/91cc7c27f706e507b4e03fcb13ada079 to your computer and use it in GitHub Desktop.
Sound reactive comet
#include <FastLED.h>
#define NUM_LEDS 30
#define DATA_PIN 3
const uint8_t g_Brightness = 16;
CRGB g_LEDs[NUM_LEDS];
int sensorAnalogPin = A0; // Select the Arduino input pin to accept the Sound Sensor's analog output
int sensorDigitalPin = 6; // Select the Arduino input pin to accept the Sound Sensor's digital output
int analogValue = 0; // Define variable to store the analog value coming from the Sound Sensor
int digitalValue; // Define variable to store the digital value coming from the Sound Sensor
int Led13 = 13; // Define LED port; this is the LED built in to the Arduino (labled L)
// When D0 from the Sound Sensor (connnected to pin 7 on the
// Arduino) sends High (voltage present), L will light. In practice, you
// should see LED13 on the Arduino blink when LED2 on the Sensor is 100% lit.
void setup() {
Serial.begin(9600); // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
pinMode(sensorDigitalPin,INPUT); // Define pin 7 as an input port, to accept digital input
pinMode(Led13,OUTPUT); // Define LED13 as an output port, to indicate digital trigger reached
FastLED.addLeds<NEOPIXEL, DATA_PIN>(g_LEDs, NUM_LEDS); // intialize LED's with FASTLED
FastLED.setBrightness(g_Brightness);
FastLED.clear(true); // still no idea why I do this
}
void loop () {
analogValue = analogRead(sensorAnalogPin); // Read the value of the analog interface A0 assigned to digitalValue
digitalValue=digitalRead(sensorDigitalPin); // Read the value of the digital interface 7 assigned to digitalValue
Serial.println(analogValue); // Send the analog value to the serial transmit interface
if(digitalValue==HIGH) // When the Sound Sensor sends signla, via voltage present, light LED13 (L)'
{
digitalWrite(Led13,HIGH);
for (int j = 0; j < 45; j++) {
drawComet();
FastLED.show();
}
}
else
{
digitalWrite(Led13,LOW);
}
delay(30); // Slight pause so that we don't overwhelm the serial interface
}
void drawComet() {
const byte fadeAmount = 196;
const byte cometSize = 2;
const byte deltaHue = 4;
static byte hue = HUE_RED;
static int iDirection = 1;
static int iPos = 0;
FastLED.clear(false);
// switch direction and change color if at end
iPos += iDirection;
if (iPos == (NUM_LEDS - cometSize) || iPos == 0) {
hue += 30;
iDirection *= -1;
}
for (int i = 0; i < cometSize; i++)
g_LEDs[iPos + i].setHue(hue);
// Randomly fade the LEDs
for (int j = 0; j < NUM_LEDS; j++)
if (random(10) > 5)
g_LEDs[j] = g_LEDs[j].fadeToBlackBy(fadeAmount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment