I need help with a project

Store Forums Audio Hacker Discussion and Project Ideas I need help with a project

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #711
    danespcha
    Member

    Hello,

    I’m doing a project for my university, I need audio shield for recording steps, but I need to do it without the ISR, can i do it?

    Now, the code I have done is this:

    void loop() {
    if(aux==1){
    grabar();
    }
    Serial.println("HOLA");
    delay(5000);
    }

    void grabar(){
    byte signal;

    AudioHacker.writeSRAM(addressChipNumber, address, signal);

    address=address+1;
    if (address > 131071) {
    if (addressChipNumber == 0) {
    // proceed to the second SRAM chip
    address = 0;
    addressChipNumber = 1;
    }
    else {
    // end of memory, stop recording
    endAddress = address;
    endAddressChipNumber = 1;
    address = 0; // loop around to beginning of memory
    addressChipNumber = 0;
    aux=0;
    }
    }
    }

    but the delays in the loop doesn’t make effect and the output in the serial doesn’t have delays.

    Please help me!

    #2007
    Michael
    Keymaster

    I really cannot tell what you are trying to accomplish or why you cannot use an ISR in your solution. Are you trying to record audio? Your variable

    signal

    is unassigned, so your program seems to just write garbage to memory. Why?

    #2008
    danespcha
    Member

    Ok, I’m trying to do this with the ISR.

    I want to record 10s of audio aprox. (so I chose 18000hz sampleRate), and then use that audio in an artificial neural network.

    #include 

    unsigned int sampleRate;
    unsigned int timer1Start;
    unsigned int address=0;
    unsigned int chipAddress=0;
    int aux=0;
    void setup(){
    Serial.begin(9600);
    sampleRate=18000;
    timer1Start = UINT16_MAX-(F_CPU/sampleRate);
    AudioHacker.begin();
    }

    void loop(){
    if(aux==1){
    Serial.println("HOLA");
    delay(3000);
    }

    }

    ISR(TIMER1_OVF_vect){
    TCNT1=timer1Start;
    unsigned int signal;
    signal=AudioHacker.readADC();
    AudioHacker.writeSRAM(chipAddress,address,signal);
    address++;
    if (address > 131071) {
    if (chipAddress == 0) {
    // proceed to the second SRAM chip
    address = 0;
    chipAddress = 1;
    }
    else {
    aux=1;
    }
    }
    }

    this is the code I have now, but it doesn’t work, it never shows ‘HOLA’ in the serial monitor.

    Can you help me?

    #2009
    Michael
    Keymaster

    If you are going to change the value of aux in the ISR, you need to declare it to be volatile.

    volatile int aux = 0;

    You have some other problems with your code. You are reading a 12-bit value from the ADC, but only incrementing the address by 1. You need to increment by 2 if you are writing 2 bytes to SRAM.

    If you want more efficient storage (required to get 10s at 18KHz) you will need to pack two 16-bit samples into 3 bytes using the function [tt:h3thdvvl]writeSRAMPacked[/tt:h3thdvvl]. See the example [tt:h3thdvvl]Sampler_12bit[/tt:h3thdvvl] for how to do this.

    If you want 8-bit sampling, use [tt:h3thdvvl]readADC_8bit()[/tt:h3thdvvl] instead, and you can increment the memory address by one as you are now.

    #2010
    danespcha
    Member

    I have another question, It seems like the ISR is not executed, because the monitor doesn’t show the address, how is the ISR executed?

      #include 

    unsigned int sampleRate;
    unsigned int timer1Start;
    unsigned int address=0;
    unsigned int chipAddress=0;
    volatile int aux = 0;
    void setup(){
    Serial.begin(9600);
    sampleRate=18000;
    timer1Start = UINT16_MAX-(F_CPU/sampleRate);
    AudioHacker.begin();
    }

    void loop(){
    if(aux==1){
    Serial.println("HOLA");
    delay(3000);
    }

    }

    ISR(TIMER1_OVF_vect){
    TCNT1=timer1Start;
    unsigned int signal;
    signal=AudioHacker.readADC_8bit();
    AudioHacker.writeSRAM(chipAddress,address,signal);
    address++;
    Serial.println(address);
    if (address > 131071) {
    if (chipAddress == 0) {
    // proceed to the second SRAM chip
    address = 0;
    chipAddress = 1;
    }
    else {
    aux=1;
    }
    }
    }

    pd: sorry for clumsiness, but I’m new to arduino and need to do this as fast as I can.

    #2011
    Michael
    Keymaster

    You cannot do Serial output in an interrupt service routine. Take that out.
    The timer setup is done in AudioHacker.begin(). When timer1 overflows, the ISR is executed.

    You are going about this all wrong. Start with a working example program and adapt it to your needs. You are starting from scratch and are not declaring things right. Your variable address needs to be declared volatile because you are changing it in the ISR. Any variable changed within the context of an interrupt must be declared as volatile.

    #2013
    danespcha
    Member

    Thank you michael, I have it working now.

    One last question, I have a code in C that transforms a wav file in an array with 18000 elements, because of sample rate, there is any way to get that 18000 elements with the audio hack shield?

    #2014
    Michael
    Keymaster

    You could write a program on the Arduino that listens to the serial port and writes the data to SRAM. And a program on a computer that writes the data to the serial port.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.