Final Project

Electronic circuit

I found FFT library here.
As Neil adviced me, I could get the information about frequency of the sound without using electronic component such as spectrum analyzer.

Using this library, I wrote a program for my project.

	#define LIN_OUT 1 
#define FFT_N 256 // set to 256 point fft
#include  // include the library
int limit = 200; 
int ANODEPIN[4]   ={8,7,6,13} ;
int CATHODEPIN[4] = {  9, 10, 11, 12 };
int i;
int matrix[4][4] ;
//Define spectrum variables
int freq_amp;
int Frequencies_One[4];

void setup() {
  TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0
  for( int ano = 0; ano < 4; ano++ ){
	pinMode( ANODEPIN[ano], OUTPUT );
		digitalWrite( ANODEPIN[ano], LOW );
	}

	
	for( int cat = 0; cat < 4; cat++ )
	{
		pinMode( CATHODEPIN[cat], OUTPUT );
		digitalWrite( CATHODEPIN[cat], HIGH );
	}
    Serial.begin(9600); // 9600bps
 
}

void loop() {
  while(1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples
      while(!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fft_input[i] = k; // put real data into even bins
      fft_input[i+1] = 0; // set odd bins to 0
    }
    fft_window(); // window the data for better frequency response
    fft_reorder(); // reorder the data before doing the fft
    fft_run(); // process the data in the fft
    fft_mag_lin(); // take the output of the fft リニアで値をとる。
    sei(); 
    Frequencies_One[0] =fft_lin_out[1] ;
    Frequencies_One[1] =fft_lin_out[5] ;
     Frequencies_One[2] =fft_lin_out[7] ;
    Frequencies_One[3] =fft_lin_out[10] ;
    Read_Frequencies();

   matrixled();
    
  }
}



void Read_Frequencies(){
  
 if  (Frequencies_One[0] < 50){
                          matrix[0][0] = 1;
                          matrix[1][0]= 1;
                          matrix[2][0] = 1;
                          matrix[3][0] = 1;
                        
                         }else if(Frequencies_One[0]<100){
                           matrix[0][0] = 1;
                          matrix[1][0] = 1;
                          matrix[2][0] = 1;
                          matrix[3][0] = 0;
                           
                         }else if(Frequencies_One[0]<150){
                           matrix[0][0] = 1;
                          matrix[1][0] = 1;
                          matrix[2][0] = 0;
                          matrix[3][0] = 0;
                         }else if(Frequencies_One[0]<200){
                          matrix[0][0] = 1;
                          matrix[1][0] = 0;
                          matrix[2][0] = 0;
                          matrix[3][0] = 0;
                         }
                         else {
                          matrix[0][0] = 0;
                          matrix[1][0] = 0;
                          matrix[2][0] = 0;
                          matrix[3][0] = 0;
                          
                         }
if  (Frequencies_One[1] < 50){
                          matrix[0][1] = 1;
                          matrix[1][1]= 1;
                          matrix[2][1] = 1;
                          matrix[3][1] = 1;
                        
                         }else if(Frequencies_One[1]<100){
                           matrix[0][1] = 1;
                          matrix[1][1] = 1;
                          matrix[2][1] = 1;
                          matrix[3][1] = 0;
                           
                         }else if(Frequencies_One[1]<150){
                           matrix[0][1] = 1;
                          matrix[1][1] = 1;
                          matrix[2][1] = 0;
                          matrix[3][1] = 0;
                         }else if(Frequencies_One[1]<200){
                          matrix[0][1] = 1;
                          matrix[1][1] = 0;
                          matrix[2][1] = 0;
                          matrix[3][1] = 0;
                         }
                         else {
                          matrix[0][1] = 0;
                          matrix[1][1] = 0;
                          matrix[2][1] = 0;
                          matrix[3][1] = 0;
                          
                         }
if  (Frequencies_One[2] < 50){
                          matrix[0][2] = 1;
                          matrix[1][2]= 1;
                          matrix[2][2] = 1;
                          matrix[3][2] = 1;
                        
                         }else if(Frequencies_One[2]<100){
                           matrix[0][2] = 1;
                          matrix[1][2] = 1;
                          matrix[2][2] = 1;
                          matrix[3][2] = 0;
                           
                         }else if(Frequencies_One[2]<150){
                           matrix[0][2] = 1;
                          matrix[1][2] = 1;
                          matrix[2][2] = 0;
                          matrix[3][2] = 0;
                         }else if(Frequencies_One[2]<200){
                          matrix[0][2] = 1;
                          matrix[1][2] = 0;
                          matrix[2][2] = 0;
                          matrix[3][2] = 0;
                         }
                         else {
                          matrix[0][2] = 0;
                          matrix[1][2] = 0;
                          matrix[2][2] = 0;
                          matrix[3][2] = 0;
                          
                         }

if  (Frequencies_One[3] < 50){
                          matrix[0][3] = 1;
                          matrix[1][3]= 1;
                          matrix[2][3] = 1;
                          matrix[3][3] = 1;
                        
                         }else if(Frequencies_One[3]<100){
                           matrix[0][3] = 1;
                          matrix[1][3] = 1;
                          matrix[2][3] = 1;
                          matrix[3][3] = 0;
                           
                         }else if(Frequencies_One[3]<150){
                           matrix[0][3] = 1;
                          matrix[1][3] = 1;
                          matrix[2][3] = 0;
                          matrix[3][3] = 0;
                         }else if(Frequencies_One[3]<200){
                          matrix[0][3] = 1;
                          matrix[1][3] = 0;
                          matrix[2][3] = 0;
                          matrix[3][3] = 0;
                         }
                         else {
                          matrix[0][3] = 0;
                          matrix[1][3] = 0;
                          matrix[2][3] = 0;
                          matrix[3][3] = 0;
                          
                         }
   
  
}
void Graph_Frequencies(){
   for( i= 0; i<3; i++)
   {
    
        Serial.println(Frequencies_One[i]);
        
     }
       Serial.println("next");
     delay(100);
   }

void matrixled()
{
  
  
	// cathode loop

	for( int ano = 0; ano < 4; ano++ ){
		digitalWrite( ANODEPIN[ano], HIGH );	
		// Anode loop
		for( int cat = 0; cat < 4; cat++ )
		{
			digitalWrite( CATHODEPIN[cat], matrix[cat][ano] );	// HIGH or LOW
		}
		delayMicroseconds(100);	//
		for( int cat = 0; cat < 4; cat++ )
		{
			digitalWrite( CATHODEPIN[cat], HIGH );
		}
		digitalWrite( ANODEPIN[ano], LOW );	// 
}
	
}
	
you can watch the prototype of LED matrix here.




Speaker Box


After modeling my product using fusion 360, I changed my mind and I wanted to combine woods and LED illumination like this picture.




And I made a hall in the wood and tested many times.
You can watch how milling machine works from here.

I was looking for a best width of wood for combining LED, and I found the best one is 0.1mm.




Inside the box, leds and the speaker are attatched to the board.
For attatching the leds, I used glue gan.






Here is inside the box.




Fabkit


In this class, I needed to make arduino by myself.
Reffering this website, I made fabkit which almost functions as arduino uno.

Amplifier circuit


Here is the board design of my amplifier circuit.


I used illustrator to make it and made the png data.
The circuit pattern is opposite so that I can solder it on the opposite side.


And here is the circuit using these data. But something was wrong with the png data at that time and I needed to solder to correct it.


Serial Communication

As an extra credit, I wanted to try working on using serial communication.
I thought it would be ideal if I could send a sound data to the speaker remotely.
And I went through some experiment using my original programming code. That is because I wanted to watch how fast Arduino can work and accuracy of programming.

I wrote this program and changed it a little and used it again and again. The charactor '//' is so useful!!
void setup() {
  // PIN#3/#11: Timer/Counter2
 // pinMode(3, OUTPUT);

  // http://www.atmel.com/Images/doc8161.pdf
  // Non-inverting fast PWM mode on Pin 3.
  // COM2B1:0 ==  10: Non-inverting mode
  // WGM22:0  == 011: Fast PWM mode, 256 cycle (16MHz / 256 == 62.5kHz)
  // CS2:0    == 001: No prescaler (runs at maximum rate, 62.5kHz)
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS20);


Serial.begin(9600);
  
}


void loop() {




 
    int input=analogRead(0);
 
//  Serial.print(input);
  //Serial.print(", ");

//  analogWrite(9,input);

    Serial.write(input);
// OCR2B = input;
// delayMicroseconds(125);

    
  
}
    
    
  
After making many mistakes, I found that the command "delay" is necessary and the length of the delay need to be decided carefully.
After calculating, the quality of the sound is getting better like here




And after that I studied about the sampling rate and so on and I found that the best number for the command delayMicroseconds is 20.
The quality of the sound changed dramatically.




After that I used serial communicaton. Though I can hear the sound clearly, there was too much noise in the sound.

So the problem is not the speed of the microcontroller but the noise during the singal sending.