Skip to content

Instantly share code, notes, and snippets.

@creotiv
Created January 24, 2016 11:06
Show Gist options
  • Select an option

  • Save creotiv/afdeb402ea58bab7d3a1 to your computer and use it in GitHub Desktop.

Select an option

Save creotiv/afdeb402ea58bab7d3a1 to your computer and use it in GitHub Desktop.
Arduin MusicLights example
#define OCTAVE 1 // // Group buckets into octaves
#define OCT_NORM 0 // Don't normalise octave intensities by number of bins
#define FHT_N 256 // set to 256 point fht
#include <FHT.h> // include the library
int noise[] = {90, 125, 147, 143, 128, 123, 104, 89}; //just test output without in silence, and use values from ocatve bins
//int noise[] = {90, 85, 147, 143, 128, 123, 104, 89}; //just test output without in silence, and use values from ocatve bins
// leds
#define R1 PB1
#define R2 PB2
#define R3 PB3
void setup() {
PORTB = 0; // 1 for hight
DDRB = (1<<R1)|(1<<R2)|(1<<R3); //1 for output
Serial.begin(115200); // use the serial port
TIMSK0 = 0; // turn off timer0 for lower jitter
//ADCSRA = 0xC5;//0xe5; // set the adc to free running mode
DIDR0 = 0x39; // turn off the digital input for adc0,3,4,5
}
void loop() {
// Start of Fourier Transform code; takes input on ADC from mic
//ADMUX = 0x40; // use adc0
while (1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
/*while (!(ADCSRA & 0x10)); // wait for adc to be ready
ADCSRA = 0xD5; //0xf5; // restart adc
byte m = ADCL; // fetch adc data
byte j = ADCH;
int k = (j << 8) | m; // form into an int*/
int k = analogRead(0);
k -= 0x0200; // form into a signed int
k <<= 6; // form into a 16b signed int
fht_input[i] = k; // put real data into bins
}
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fht
fht_run(); // process the data in the fht
fht_mag_octave();
sei();
// End of Fourier Transform code - output is stored in fht_oct_out[i].
int fht_noise_adjusted[8];
int result_octaves[8];
for (int i = 0; i < 8; i++) { // For each of the 6 useful octave bins
fht_noise_adjusted[i] = abs(fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
fht_noise_adjusted[i] = constrain(fht_noise_adjusted[i], 37, 125); // 37 lowpass for noise; 125 high pass doesn't go much higher than this [found by trial and error]
result_octaves[i] = map(fht_noise_adjusted[i], 37, 125, 0, 255); // map to values 0 - 160, i.e. blue to red on colour spectrum - larger range gives more colour variability [found by trial and error]
Serial.print(i);
Serial.print("\t");
Serial.println(fht_oct_out[i]);
/*Serial.print("\t");
Serial.println(fht_oct_out[i]);*/
lights(fht_oct_out[i], i);
}
}
}
void lights(int con, int octave)
{
int k1 = analogRead(3);
k1 = map(k1, 0, 1023, 0, 255);
int k2 = analogRead(4);
k2 = map(k2, 0, 1023, 0, 255);
int k3 = analogRead(5);
k3 = map(k3, 0, 1023, 0, 255);
Serial.print("Settings: ");
Serial.print(k1);
Serial.print(" ");
Serial.print(k2);
Serial.print(" ");
Serial.println(k3);
if(octave == 1 && con >= k1) {
PORTB |= (1<<R3);
} else if(octave == 1 && con < k1) {
PORTB &= (0<<R3);
}
if(octave == 3 && con >= k2) {
PORTB |= (1<<R2);
} else if(octave == 3 && con < k2) {
PORTB &= (0<<R2);
}
if(octave == 7 && con >= k3) {
PORTB |= (1<<R1);
} else if(octave == 7 && con < k3) {
PORTB &= (0<<R1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment