Created
June 6, 2021 19:14
-
-
Save SebastiaanDeJonge/52b1206786badcaf1952c867e94b9b87 to your computer and use it in GitHub Desktop.
arduino-led-party.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This sketch will let three LED strips blink based on the intensity of sound input. My example | |
* used 12V LED strips which required an external power source and some transistors to hook up | |
* if you have something to hook up to the Arduino directly then you can use that instead of | |
* course and omit/change the transistors/resistors. | |
* | |
* Requirements: | |
* - Arduino Nano/Uno (or equivalent) | |
* - Power source for the Arduino (i.e. 9V battery or USB) | |
* - Digital microphone module (i.e. LM393) | |
* - Power source for the LEDs (i.e. 12V battery) | |
* - 3x 12V LED strip | |
* - 3x TIP120 transistor | |
* - 3x 2k Ohm resistor | |
* - Slide switch (to control sensitivity) | |
* | |
* Sorry, I don't have a schematic for this setup anymore :) | |
*/ | |
int firstLed = 10; | |
int secondLed = 11; | |
int thirdLed = 12; | |
int microphone = 9; | |
int input = LOW; | |
int inputs[10] = {0, 0, 0, 0, 00}; | |
int inputIndex = 0; | |
int inputSize = 5; | |
int brightness; | |
void setup() { | |
pinMode(microphone, INPUT); | |
pinMode(firstLed, OUTPUT); | |
pinMode(secondLed, OUTPUT); | |
pinMode(thirdLed, OUTPUT); | |
} | |
void loop() { | |
input = digitalRead(microphone); | |
inputs[inputIndex] = input; | |
inputIndex++; | |
if (inputIndex == inputSize) { | |
inputIndex = 0; | |
} | |
int inputSum = sum(); | |
brightness = inputSum > 0 ? round(inputSum * (255 / inputSize)) : 0; | |
analogWrite(firstLed, brightness); | |
analogWrite(secondLed, brightness); | |
analogWrite(thirdLed, brightness); | |
} | |
int sum() { | |
int sum = 0; | |
for (int index = 0; index < inputSize; index++) { | |
sum += inputs[index]; | |
} | |
return sum < 0 ? 0 : sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment