Go Down

Topic: Sending ADC and Processing it on RPi to a WAV file (Read 120 times) previous topic - next topic

hatsuyuki

Hello everyone,

I used the code from this site: http://www.instructables.com/id/Arduino-Audio-Input/ to convert analog reading to a ADC value.


Now, my question is: how do I use these values to generate WAV files on the serial port-connected Raspberry Pi? Am I missing something on the Arduino?


Here is my code (on the Arduino):

Code: [Select]

void setup()
{
  cli();//disable interrupts
 
  //set up continuous sampling of analog pin 0
 
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
 
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
 
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
 
  sei();//enable interrupts
   Serial.begin(9600);
}

ISR(ADC_vect) {//when new ADC value ready
  sample = ADCH;//update the variable sample with new value from A0 (between 0 and 255)
}
void loop()
{
  Serial.println(sample);
  }

Grumpy_Mike

Many things you are missing.

The 9600 baud rate will slow down considerably the speed you can get it into the Pi.

Using serial print will result in a very inefficient transfer as will sending a line feed at the end of each print. You should be sending the sample as two bytes using serial write.

You are not synchronising the sending of the sample with the acquisition of the sample, this will result in missing samples, or sending samples more than once.

You can not generate a WAV file on the fly, you need to have the whole sample before you can fill in the headers in the file. You will have to do some work at the Piend for this. Also a WAV file needs to know the sample rate and you have no way of knowing how fast that code is sampling.

In short instrutables are crap, mainly written by people who think they know stuff but don't.

Go Up