im building a "magical box" using an arduino UNO, a speaker, an SD Module and some LEDs.
For now the design and functioning is simple:
- A magnetic switch (SW1) is put in the box. When it opens it activates a first TIP41C transistor that will provide 8-9V to the arduino VIN pin and an 7805 5V voltage regulator.
- Once opened, the arduino is turned on and starts playing a song saved in an SD card. At the same time, 12 LEDs driven by 3 TIP41C are turned on. They have 3 patterns that can be selected with an external button (not shown in diagram, connected to pin 2.
Everything works fine when I use an external 5V source on a breadboard and power the NANO with USB. However, when trying to use and external 9V connected in a jack, it works normally if the LEDs doesnt have to change pattern and stay always on (default pattern). When a different sequence is selected, everytime they have to "blink" the music acts weird, and sounds like if you had a radio with bad signal trying to tune in. I dont know why this happens and only seems to happen with external power supply.
Any idea why this happens or how i can improve it?
Also, not 100% sure of the design itself arround the TIP41 and 7805, really could use some tips there too.
Code and schematic (done in proteus, used terminals for the SD module and NANO pins) shown below.
Thanks!!!!
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //connect pin 4 of arduino to cs pin of sd card
#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h> // need to include the SPI library
TMRpcm tmrpcm; // create an object for use in this sketch
int trans1 = 3;
int trans2 = 5;
int trans3 = 6;
int sensorCAP = 2;
volatile int ledstates = 0;
void setup() {
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
pinMode(trans1, OUTPUT);
pinMode(trans2, OUTPUT);
pinMode(trans3, OUTPUT);
pinMode(sensorCAP, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorCAP), nextLed, RISING);
Serial.begin(9600);
Serial.println("Initializing....");
delay(1000);
SD.begin(SD_ChipSelectPin);
File root;
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
tmrpcm.setVolume(5); //
char archive = "song.wav";
tmrpcm.play("song.wav"); //the sound file "song" will play each time the arduino powers up, or is reset
}
void loop() {
if (!tmrpcm.isPlaying()) {
tmrpcm.disable();
}
Serial.println(ledstates);
switch (ledstates) {
case 1:
// statements
patron1();
break;
case 2:
patron2();
break;
case 3:
patron3();
break;
default:
digitalWrite(trans1, HIGH);
digitalWrite(trans2, HIGH);
digitalWrite(trans3, HIGH);
break;
}
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void patron1() {
if (ledstates != 1) { return; }
digitalWrite(trans1, HIGH);
digitalWrite(trans2, HIGH);
digitalWrite(trans3, HIGH);
if (ledstates != 1) { return; }
delay(2000);
if (ledstates != 1) { return; }
digitalWrite(trans1, LOW);
digitalWrite(trans2, LOW);
digitalWrite(trans3, LOW);
if (ledstates != 1) { return; }
delay(2000);
}
void patron2() {
if (ledstates != 2) { return; }
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 255; i++) {
analogWrite(trans1, i);
analogWrite(trans2, i);
analogWrite(trans3, i);
if (ledstates != 2) { return; }
delay(5);
}
for (int i = 255; i > 0; i--) {
analogWrite(trans1, i);
analogWrite(trans2, i);
analogWrite(trans3, i);
if (ledstates != 2) { return; }
delay(5);
}
if (ledstates != 2) { return; }
delay(2000);
}
}
void patron3() {
int linea = 1;
int espacio = 2;
int punto = 3;
morse(linea);
morse(espacio);
morse(punto);
morse(punto);
morse(linea);
morse(espacio);
morse(linea);
morse(punto);
morse(linea);
morse(punto);
morse(espacio);
morse(linea);
morse(linea);
morse(linea);
morse(espacio);
morse(punto);
morse(linea);
morse(punto);
morse(punto);
morse(espacio);
morse(punto);
morse(linea);
if (ledstates != 3) { return; }
delay(2000);
}
void morse(int lineaespaciopunto) {
switch (lineaespaciopunto) {
case 1:
// linea
digitalWrite(trans1, HIGH);
digitalWrite(trans2, HIGH);
digitalWrite(trans3, HIGH);
if (ledstates != 3) { return; }
delay(1500);
digitalWrite(trans1, LOW);
digitalWrite(trans2, LOW);
digitalWrite(trans3, LOW);
if (ledstates != 3) { return; }
delay(1500);
break;
case 2:
// espacio
digitalWrite(trans1, LOW);
digitalWrite(trans2, LOW);
digitalWrite(trans3, LOW);
if (ledstates != 3) { return; }
delay(500);
break;
case 3:
// punto
digitalWrite(trans1, HIGH);
digitalWrite(trans2, HIGH);
digitalWrite(trans3, HIGH);
if (ledstates != 3) { return; }
delay(750);
digitalWrite(trans1, LOW);
digitalWrite(trans2, LOW);
digitalWrite(trans3, LOW);
if (ledstates != 3) { return; }
delay(750);
break;
default:
// statements
break;
}
}
void nextLed() {
delayMicroseconds(5000);
if (ledstates > 2) {
ledstates = 0;
} else {
ledstates = ledstates + 1;
}
}