Arduino Tone Generator SD Log Then Playback
by powerman666 in Circuits > Arduino
2932 Views, 2 Favorites, 0 Comments
Arduino Tone Generator SD Log Then Playback
This is how you us radio waves, cosmic rays, electromag-netic interference from cell phones and fluorescent lights, and so on to generate random tones on your Arduino then store them to and SD card and then play them back.
What You Need.
Connections
The code for the SD Card Shield uses pins (10,11,12, and 13).
My code uses pin 8 for the speaker.
So first attach you SD Card Shield, then connect your speaker to pin 8.
My code uses pin 8 for the speaker.
So first attach you SD Card Shield, then connect your speaker to pin 8.
Enter the Code
#include <SdFat.h>
// filename for this example
char name[] = "NEWSONG.LOG";
char toneline[5];
int n, holder;
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
int playtone;
//int dlay;
void setup()
{
pinMode(8, OUTPUT);
//pinMode(A5, INPUT);
// Serial.begin(9600);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
startupsong();
}
void loop()
{
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
ofstream sdout(name, ios::out | ios::app);
if (!sdout) error("open failed");
delay(200);
randomSeed(analogRead(5));
/*If you have an unconnected analog pin,
it will pick up random noise from the
surrounding environment (radio waves,
cosmic rays, electromag-netic interference
from cell phones and fluorescent lights,
and so on).*/
long dlayer = random(180000)+180000;
/* orginally this code was for a random annoyer with at least a 3 minute delay. So you can change the dlayer to a shorter time if you want. */
//int dlayer = 180000;
// while (dlayer < 0){dlayer = random(180000)+180000;}
// Serial.end();
// Serial.begin(9600);
// Serial.print("Delay 1 is ");
// Serial.print(dlayer/1000, DEC);
// Serial.print(" seconds, aprox ");
// Serial.print(dlayer/1000/60, DEC);
// Serial.println( " minutes");
delay(dlayer);
playtone = random(2000);
// Serial.print("Tone 1 is ");
// Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
// sdout.close();
playtone = random(7000);
// Serial.print("Tone 2 is ");
//Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
playtone = random(5000);
//Serial.print("Tone 3 is ");
//Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
sdout.close();
readsong();
}
void startupsong(){
// Serial.println("Playing startup song.");
tone(8, 2500);
delay(50);
noTone(8);
tone(8, 2500);
delay(50);
noTone(8);
delay(50);
tone(8, random(7000));
delay(random(250));
noTone(8);
delay(50);
tone(8, random(5000));
delay(50);
noTone(8);
delay(50);
}
void readsong(){
SdFile rdfile("SONG.LOG", O_READ);
while ((n = rdfile.fgets(toneline, sizeof(toneline))) > 0) {
/*
if (toneline[n - 1] == '\n') {
cout << '>' << toneline;
} else {
cout << '#' << toneline << endl;
}
*/
holder = atoi(toneline);
tone(8, holder);
delay(50);
noTone(8);
delay(50);
// Serial.print("+");
// Serial.println(holder);
//cout << holder << endl;
}
//char c;
// ifstream file("SOUNDS.LOG");
//if (!file.is_open()) sd.errorHalt("open failed");
// copy the file to Serial
// while ((c = file.get()) >= 0) cout << c;
// cout << "Done" << endl;
}
// filename for this example
char name[] = "NEWSONG.LOG";
char toneline[5];
int n, holder;
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
int playtone;
//int dlay;
void setup()
{
pinMode(8, OUTPUT);
//pinMode(A5, INPUT);
// Serial.begin(9600);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
startupsong();
}
void loop()
{
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
ofstream sdout(name, ios::out | ios::app);
if (!sdout) error("open failed");
delay(200);
randomSeed(analogRead(5));
/*If you have an unconnected analog pin,
it will pick up random noise from the
surrounding environment (radio waves,
cosmic rays, electromag-netic interference
from cell phones and fluorescent lights,
and so on).*/
long dlayer = random(180000)+180000;
/* orginally this code was for a random annoyer with at least a 3 minute delay. So you can change the dlayer to a shorter time if you want. */
//int dlayer = 180000;
// while (dlayer < 0){dlayer = random(180000)+180000;}
// Serial.end();
// Serial.begin(9600);
// Serial.print("Delay 1 is ");
// Serial.print(dlayer/1000, DEC);
// Serial.print(" seconds, aprox ");
// Serial.print(dlayer/1000/60, DEC);
// Serial.println( " minutes");
delay(dlayer);
playtone = random(2000);
// Serial.print("Tone 1 is ");
// Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
// sdout.close();
playtone = random(7000);
// Serial.print("Tone 2 is ");
//Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
playtone = random(5000);
//Serial.print("Tone 3 is ");
//Serial.println(playtone);
tone(8, playtone);
delay(50);
noTone(8);
delay(50);
sdout << playtone << endl;
sdout.close();
readsong();
}
void startupsong(){
// Serial.println("Playing startup song.");
tone(8, 2500);
delay(50);
noTone(8);
tone(8, 2500);
delay(50);
noTone(8);
delay(50);
tone(8, random(7000));
delay(random(250));
noTone(8);
delay(50);
tone(8, random(5000));
delay(50);
noTone(8);
delay(50);
}
void readsong(){
SdFile rdfile("SONG.LOG", O_READ);
while ((n = rdfile.fgets(toneline, sizeof(toneline))) > 0) {
/*
if (toneline[n - 1] == '\n') {
cout << '>' << toneline;
} else {
cout << '#' << toneline << endl;
}
*/
holder = atoi(toneline);
tone(8, holder);
delay(50);
noTone(8);
delay(50);
// Serial.print("+");
// Serial.println(holder);
//cout << holder << endl;
}
//char c;
// ifstream file("SOUNDS.LOG");
//if (!file.is_open()) sd.errorHalt("open failed");
// copy the file to Serial
// while ((c = file.get()) >= 0) cout << c;
// cout << "Done" << endl;
}