Skip to content

Instantly share code, notes, and snippets.

@FdelMazo
Last active July 5, 2020 14:46
Show Gist options
  • Save FdelMazo/be753d49a4897286c92aa471f10f1402 to your computer and use it in GitHub Desktop.
Save FdelMazo/be753d49a4897286c92aa471f10f1402 to your computer and use it in GitHub Desktop.
BeatCube! The magical 4x4x4 LED Cube that detects music! ---> https://youtu.be/gIms3RQuJqA
/* https://youtu.be/gIms3RQuJqA */
/* Cuby! The magical 4x4x4 LED Cube that detects music!
* This project was done with an arduino 1, which contains 14 digital pins, and 6 analog ones
* Cuby is a 4x4x4 LED Cube. That means that there are 4 planes of 16 LEDs each
* One of those planes, however, won't be controllable, it will be directly connected to the ground exit of the arduino
* So each time a LED is turned on, wherever it is placed, the corresponding LED of that plane will also switch on
* This is because of a lack of pins. One analog pin must be used by the sound detector, leaving only 3 controllable planes
* The division of pins will be done like this:
* - D0 to D13 are the first 14 positive side of the leds
* - A0 and A1 are the other 2 positive leds
* - A2, A3 and A4 will be the grounds. One for each controllable plane
* - A5 will be used by the sound detector
*/
int LED[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int LEDCount = 16;
int PlanePin[] = {A2, A3, A4, A5};
int PlanePinCount = 4;
int SoundSensorPin = A7;
int SoundThreshold = -1; // The first value read by the microphone will be the ambient sound (have a quiet room right before connecting the arduino!)
int SoundDivision = 10;
void setup() {
Serial.begin(9600);
for (int pin=0; pin<LEDCount; pin++) {
pinMode( LED[pin], OUTPUT );
}
for (int pin=0; pin<PlanePinCount; pin++) {
pinMode( PlanePin[pin], OUTPUT );
digitalWrite( PlanePin[pin], HIGH);
}
pinMode(SoundSensorPin, INPUT);
SoundThreshold = analogRead(SoundSensorPin)+1;
}
void turnOnPlane(int plane) {
if (digitalRead(PlanePin[plane]) == HIGH) {
digitalWrite(PlanePin[plane], LOW);
}
}
void turnOffPlane(int plane) {
if (digitalRead(PlanePin[plane]) == LOW) {
digitalWrite(PlanePin[plane], HIGH);
}
}
void turnOff(int led){
digitalWrite(led, LOW);
}
void turnOn(int led){
digitalWrite(led, HIGH);
}
void fadeOut(int level){
for (int plane=PlanePinCount-1; plane > level; plane--) {
turnOffPlane(plane);
}
}
void fadeIn(int level) {
for (int pin=0; pin<LEDCount; pin++) {
turnOn(LED[pin]);
}
for (int plane=0; plane<=level; plane++) {
turnOnPlane(plane);
}
}
void turnOffAll() {
for (int pin=0; pin<LEDCount; pin++) {
turnOff(LED[pin]);
}
}
void turnOnLevel(int level) {
fadeOut(level);
if (level == -1) {
turnOffAll();
}
else fadeIn(level);
}
void loop() {
int sensorData = analogRead(SoundSensorPin);
int deltaSound = sensorData - SoundThreshold;
int level = map(deltaSound, -SoundDivision, 4*SoundDivision, -1, PlanePinCount-1);
Serial.println((String) sensorData + " - " + SoundThreshold + ": " + level);
turnOnLevel(level);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment