Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* I2C SD Card Audio Player
*
* This project is part of Bob The Robot project and
* used for playing audio via the I2C bus
* https://github.com/aviosipov/bob-the-robot
*
* by Avi Osipov
*/
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
String data ;
void setup(){
Serial.begin(57600);
Serial.println("ready");
Wire.begin(9); /// our device id
Wire.onReceive(receiveEvent);
if (!SD.begin(4)) {
return;
}
tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
}
void receiveEvent(int bytes) {
char c ;
data = "" ;
while ( Wire.available() > 0 ) {
c = Wire.read();
data += c ;
}
/// play the file requested
char fileName[16] ;
data.toCharArray(fileName,16) ;
tmrpcm.play(fileName);
Serial.println(data) ;
}
void loop() {
}
#include <Wire.h>
void playAudio(String data) {
char tmp[12]; /// xxxxxxxx.yyy
data.toCharArray(tmp, 12);
Wire.beginTransmission(9);
Wire.write(tmp);
Wire.endTransmission();
}
void setup() {
Wire.begin();
playAudio("hello.wav");
delay(2000);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment