0% found this document useful (0 votes)
2 views4 pages

Arduino FastLED Con Micro

This document is an Arduino sketch for controlling a LED strip based on audio input from a microphone. It features different lighting modes, including a 'CLOUD' mode that triggers various lightning effects when sound is detected above a certain threshold. The code includes functions for different types of lightning effects such as 'thunderburst', 'rolling', and 'crack', along with a reset function to turn off the LEDs.

Uploaded by

josemrvr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Arduino FastLED Con Micro

This document is an Arduino sketch for controlling a LED strip based on audio input from a microphone. It features different lighting modes, including a 'CLOUD' mode that triggers various lightning effects when sound is detected above a certain threshold. The code includes functions for different types of lightning effects such as 'thunderburst', 'rolling', and 'crack', along with a reset function to turn off the LEDs.

Uploaded by

josemrvr
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Wire.

h>
#include "FastLED.h"

// How many leds in your strip?


#define NUM_LEDS 210
#define DATA_PIN 2

long randomNumber;

// Mode enumeration - if you want to add additional party or colour modes, add them
here; you'll need to map some IR codes to them later;
// and add the modes into the main switch loop
enum Mode { CLOUD,OFF};
Mode mode = CLOUD;
Mode lastMode = CLOUD;

// Mic settings, shouldn't need to adjust these.


#define MIC_PIN A0 // Microphone is attached to this analog pin
#define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
#define NOISE 10 // Noise/hum/interference in mic signal
#define SAMPLES 10 // Length of buffer for dynamic level adjustment
byte
volCount = 0; // Frame counter for storing past volume data
int
vol[SAMPLES]; // Collection of prior volume samples
int n, total = 30;
float average = 0;

// used to make basic mood lamp colour fading feature


int fade_h;
int fade_direction = 1;

// Define the array of leds


CRGB leds[NUM_LEDS];

void setup() {
// this line sets the LED strip type - refer fastLED documeantion for more
details [Link]
[Link]<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
// starts the audio samples array at volume 15.
memset(vol, 15, sizeof(vol));
[Link](115200);
randomSeed(analogRead(A1));
}

void loop() {

randomNumber = random(0, 255);


// Maps mode names to code functions.
switch(mode){
case CLOUD: detect_thunder();reset();break;
case OFF:reset();break;
default: detect_thunder(); reset();break;
}

}
void detect_thunder() {

n = analogRead(MIC_PIN); // Raw reading from mic


n = abs(n - 512 - DC_OFFSET); // Center on zero
n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
vol[volCount] = n; // Save sample for dynamic leveling
if(++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter

total = 0;
for(int i=0; i<SAMPLES; i++) {
total += vol[i];
}

average = (total/SAMPLES)+2;
if(n>average){
[Link]("TRIGGERED");
reset();

//I've programmed 3 types of lightning. Each cycle, we pick a random one.


switch(random(1,3)){
//switch(3){

case 1:
thunderburst();
delay(random(10,500));
[Link]("Thunderburst");
break;

case 2:
rolling();
[Link]("Rolling");
break;

case 3:
crack();
delay(random(50,250));
[Link]("Crack");
break;

}
}
}

// utility function to turn all the lights off.


void reset(){
for (int i=0;i<NUM_LEDS;i++){
leds[i] = CHSV( 0, 0, 0);
}
[Link]();

}
void rolling(){
// a simple method where we go through every LED with 1/10 chance
// of being turned on, up to 10 times, with a random delay wbetween each time
for(int r=0;r<random(2,10);r++){
//iterate through every LED
for(int i=0;i<NUM_LEDS;i++){
if(random(0,100)>90){
// leds[i] = CHSV(randomNumber, randomNumber, randomNumber);

}
else{
//dont need reset as we're blacking out other LEDs her
leds[i] = CHSV( 0, 0, 0);
}
}
[Link]();
delay(random(5,100));
reset();

}
}

void crack(){
//turn everything white briefly
for(int i=0;i<NUM_LEDS;i++) {
// leds[i] = CHSV(randomNumber, randomNumber, randomNumber);
leds[i] = CHSV(0, 0, 255);
}
[Link]();
delay(random(10,100));
reset();
}

void thunderburst(){

// this thunder works by lighting two random lengths


// of the strand from 10-20 pixels.
int rs1 = random(0,NUM_LEDS/2);
int rl1 = random(10,20);
int rs2 = random(rs1+rl1,NUM_LEDS);
int rl2 = random(10,20);

//repeat this chosen strands a few times, adds a bit of realism


for(int r = 0;r<random(3,6);r++){

for(int i=0;i< rl1; i++){


// leds[i+rs1] = CHSV(randomNumber, randomNumber, randomNumber);
leds[i] = CHSV(0, 0, 255);
}

if(rs2+rl2 < NUM_LEDS){


for(int i=0;i< rl2; i++){
// leds[i+rs2] = CHSV(randomNumber, randomNumber, randomNumber);
leds[i] = CHSV(0, 0, 255);
}
}

[Link]();
//stay illuminated for a set time
delay(random(10,50));

reset();
delay(random(10,50));
}

You might also like