Go Down

Topic: wav player with led's controling by ethernet (Read 705 times) previous topic - next topic

mcmarek

Jan 21, 2013, 04:59 pm Last Edit: Jan 21, 2013, 05:07 pm by mcmarek Reason: 1
Hello guys. I've got problem with interrupts (I guess?). First of all I describe the scheme. I using the arduino UNO + ethernet shield. There is a speaker connected to GND and PWM~9, and led's conected to 6,5 pins. I've got a 3 wav files on sd card. Everything works separately, but all at once won't. I can control the led's via ethernet, also I can control the sound using console. The problem comes when I tried to change led sequence during the song is playing. I using the TMRpcm library and standard Ethernet. Here is the code:

Code: [Select]

#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>                      
#define SD_ChipSelectPin 4  //using digital pin 4
#include <TMRpcm.h>

TMRpcm tmrpcm;  

unsigned long time = 0;

boolean reading = false;

short unsigned int choice=3;

short int Speaker=9;
short int LedOne=5;
short int LedTwo=6;

int LedOneStat=0;
int LedTwoStat=0;

int x=0;

IPAddress ip(192,168,1, 177);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80); //port 80

void flash_all()
{
   LedOneStat=1-LedOneStat;
   LedTwoStat=1-LedTwoStat;
   digitalWrite(LedOne,LedOneStat);
   digitalWrite(LedOne,LOW);
   digitalWrite(LedTwo,LedTwoStat);
   
}

void flash_even()
{
 LedOneStat=1-LedOneStat;
 LedTwoStat=1-LedOneStat;
 
 digitalWrite(LedOne,LedOneStat);
 digitalWrite(LedTwo,LedTwoStat);
 
 delay(250);
}

void flash_1()
{
  //...
}

void flash_2()
{
  //...
}

void setup()
{
   Serial.begin(9600);
   
   pinMode(Speaker,OUTPUT);
   pinMode(LedOne,OUTPUT);  
   pinMode(LedTwo,OUTPUT);
   
   
   if (!SD.begin(SD_ChipSelectPin))
   {  
       Serial.println("SD fail");  
       return;
   }
   
   else
   {  
       Serial.println("SD ok");  
   }
   
   tmrpcm.play("lc1.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
   
   Ethernet.begin(mac,ip);
 
   server.begin();

   Serial.println(Ethernet.localIP());
     
   //digitalWrite(LedOne,HIGH);
   //digitalWrite(LedTwo,HIGH);  
}

void loop()
{
   checkForClient();
   
   if(choice==2) flash_all();
   if(choice==3) flash_even();
   if(choice==4) flash_1();
   if(choice==5) flash_2();
   
   if(Serial.available())
   {    
     switch(Serial.read())
     {
       case 'a':
           tmrpcm.play("lc1.wav");
           break;
       case 'b':
           tmrpcm.play("b1.wav");
           break;
       case 'c':
           tmrpcm.play("c1.wav");
           break;
       case 'p':
           tmrpcm.pause();
           break;
       case '?':
           if(tmrpcm.isPlaying()) Serial.println("A wav file is being played");
           else Serial.println("A wav file isn't playing");
           break;
       case 'S':
           tmrpcm.stopPlayback();
           break;
       default:
           break;
     }
   }

}

void checkForClient()
{

 EthernetClient client = server.available();

 if (client) {

   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   boolean sentHeader = false;
   tmrpcm.pause();
   while (client.connected()) {
     if (client.available()) {

       if(!sentHeader){
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         sentHeader = true;
       }

       char c = client.read();

       if(reading && c == ' ') reading = false;
       if(c == '?') reading = true; //found the ?, begin reading the info

       if(reading){
         Serial.print(c);

          switch (c) {
           case '2':
             choice=2;
             break;
           case '3':
             choice=3;
             break;
           case '4':
             choice=4;
             break;
           case '5':
             choice=5;
             break;
           
         }

       }

       if (c == '\n' && currentLineIsBlank)  break;

       if (c == '\n') {
         currentLineIsBlank = true;
       }else if (c != '\r') {
         currentLineIsBlank = false;
       }

     }
  }

   delay(1); // give the web browser time to receive the data
   tmrpcm.pause();
   client.stop(); // close the connection:

 }

}


I think the interrupts are overlap. But my knowledge is not very impressive. Can anyone help me figure it out?
Thanks for all replays.

PS. Sorry for my limited english. It's my secound language.

/********************edit********************/
I check everything once again and I find out that problem is flash_all(). i can set all others sequences of led's and it's work's. I think the problem could be that the led's have a diffrent specification, and if I turn on the 'ONE', i can't turn on the 'TWO'(not exacly, i can turn them on but still only one is flashing). So I have to quicky change the pwm's voltage. Any ideas?

Go Up