HELP Transfer sound data between 2 arduinos

Hi,
i'm trying to transfer data frome one arduino to another arduino. I'm using a SD card to read the file, and later send it by the tx and receiving on the another arduino rx. Finally, the transmitted file will be reproduced by a speaker. I'm using a .wav file.
Principal idea is to use laser to to this transmission, but by the way i'm using 2 wires.
The recieving arduino code, reproduces audio when the file is directly sent of the same arduino.
But when i try to send it after the SD read from one arduino to another, it doesn't sound anything. The problem is that RX doesn't work, because the RX light is off.
This guy does a similar project:

The code:
Sending arduino:

/* Cablejat:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

void setup() {

Serial.begin(115200);
while (!Serial) {
;
}

Serial.print("Iniciant targeta SD...");
pinMode(10, OUTPUT);

// Combrobar que es pot iniciar la targeta.
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("Targeta iniciada amb éxit.");

File dataFile = SD.open("fademono.wav");

if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}

else {
Serial.println("error opening fademono.wav");
}
}

void loop() {
}

Receiving arduino:


#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#define SAMPLE_RATE 8000


// #include "sounddata.h" /Això ja no ha de ser necessari.

int ledPin = 13;
int speakerPin = 11; // Can be either 3 or 11, two PWM outputs connected to Timer 2
volatile uint16_t sample;
byte lastSample;

byte theByte = 0;


void stopPlayback()
{
    // Disable playback per-sample interrupt.
    TIMSK1 &= ~_BV(OCIE1A);

    // Disable the per-sample timer completely.
    TCCR1B &= ~_BV(CS10);

    // Disable the PWM timer.
    TCCR2B &= ~_BV(CS10);

    digitalWrite(speakerPin, LOW);
}

// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
  
        if(speakerPin==11){
            OCR2A = theByte;
        } else {
// Això no s'executa mai.
//            OCR2B = pgm_read_byte(&sounddata_data[sample]);            
        }
    

    ++sample;
}

void startPlayback()
{
    pinMode(speakerPin, OUTPUT);

    // Set up Timer 2 to do pulse width modulation on the speaker
    // pin.

    // Use internal clock (datasheet p.160)
    ASSR &= ~(_BV(EXCLK) | _BV(AS2));

    // Set fast PWM mode  (p.157)
    TCCR2A |= _BV(WGM21) | _BV(WGM20);
    TCCR2B &= ~_BV(WGM22);

    if(speakerPin==11){
        // Do non-inverting PWM on pin OC2A (p.155)
        // On the Arduino this is pin 11.
        TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
        TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
        // No prescaler (p.158)
        TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);

        // Set initial pulse width to the first sample.
// AIXÒ POT SER NECESSARI I IMPEDIR EL BON FUNCIONAMENT.
//        OCR2A = pgm_read_byte(&sounddata_data[0]);
// POSSIBLE ALTERNATIVA
       OCR2A = 128;
    } else {
        // Do non-inverting PWM on pin OC2B (p.155)
        // On the Arduino this is pin 3.
        TCCR2A = (TCCR2A | _BV(COM2B1)) & ~_BV(COM2B0);
        TCCR2A &= ~(_BV(COM2A1) | _BV(COM2A0));
        // No prescaler (p.158)
        TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);

        // Set initial pulse width to the first sample.
// AIXÒ NO S'EXECUTA MAI
//        OCR2B = pgm_read_byte(&sounddata_data[0]);
    }


    // Set up Timer 1 to send a sample every interrupt.

    cli();

    // Set CTC mode (Clear Timer on Compare Match) (p.133)
    // Have to set OCR1A *after*, otherwise it gets reset to 0!
    TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
    TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));

    // No prescaler (p.134)
    TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);

    // Set the compare register (OCR1A).
    // OCR1A is a 16-bit register, so we have to do this with
    // interrupts disabled to be safe.
    OCR1A = F_CPU / SAMPLE_RATE;    // 16e6 / 8000 = 2000

    // Enable interrupt when TCNT1 == OCR1A (p.136)
    TIMSK1 |= _BV(OCIE1A);

//    lastSample = pgm_read_byte(&sounddata_data[sounddata_length-1]);
    sample = 0;
    sei();
}


void setup()
{   Serial.begin(115200);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    startPlayback();
}

void loop()
{
    while (Serial.available()); {
      theByte = Serial.read();}
}

I need help ASAP. Thank you for reading.

Then you need to show us a schematic so we can see your project.

The semicolon after while (Serial.available()) is not correct.

Hi ,
The conexion is just a TX-RX and ground to ground between the two arduinos.

What version of Arduino boards are you using?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.