Not sure how to do this

Pat13

Senior Member
Hi all, I am having trouble figuring this out. I want an mp3 to loop until a PIR is detected upon which it switches to another mp3. I tried this but it would only switch once mp3 1 was finished.

Code:
Main0:
           serout TENDA, BAUD, ($E8)
            Do
                serout TENDA, Baud, ($F2,01)     `play Track 1, Folder 2
		pause 1000       
		readadc PIR,debounce 
		if debounce>50 then Open_Chant 'if triggered go to opening chant
		gosub Test_Busy_Line          'check busy line before doing anything else
            loop while debounce < 50        'if not triggered sit in this loop
	
Test_Busy_Line:
            Do
              if Wait_If_Busy = 1 then exit
            Loop
            return
I tried this but the mp3 would only play for 5 seconds and loop back. The track is 28 seconds long. I would think "pause 28000" would stop program flow for 28 seconds.
Code:
Main0:
           serout TENDA, BAUD, ($E8) 'set Tenda volume
            Do
                serout TENDA, Baud, ($F2,01)     `play Track 1, Folder 2
		pause 5000       
		readadc PIR,debounce 
		if debounce>50 then Open_Chant 'if triggered go to opening chant
		loop while debounce < 50        'if not triggered sit in this loop
I read the if/elseif section of manual 2. I am sure there is a simple way to do this, I'm just not seeing it.
 

Goeytex

Senior Member
Your code is incomplete....symbols missing, etc .

Can you post complete code and tell us what Picaxe Chip and what frequency ?
 

Pat13

Senior Member
sorry about that.
Here is the complete code
Code:
#Picaxe 18M2
'Witch Prop

symbol PIR= B.7      `PIR sensor
symbol TENDA=B.3 `Tenda RXD  `
symbol Debounce=b18
symbol BAUD=T4800
symbol Wait_If_Busy=pinB.1 `Tenda Busy Line
symbol Magic_Hand=C.0
symbol Elbow=C.6
symbol Shoulder=C.7

Start0:


	let w0 = time
	random w1
	pause w2

Init0:                'Staring point to setup all inputs, outputs and set variables
 
            serout TENDA,BAUD, ($EF);       'STOP MP3 module 
            pause 10000
            serout TENDA,BAUD, ($E0)        `set Tenda Volume off
            pause 60000  'one minute pause to let PIR settle down
            
                                ;
Main0:
           serout TENDA, BAUD, ($E8) 'set Tenda volume
            Do
                serout TENDA, Baud, ($F2,01)     `play Track 1, Folder 2
		pause 5000       
		readadc PIR,debounce 
		if debounce>50 then Open_Chant 'if triggered go to opening chant
		loop while debounce < 50        'if not triggered sit in this loop
	
		
Open_chant:
            Gosub Clear_Debounce		'clear Trigger
            Pause 1000
            serout TENDA,BAUD,($F2,02)		'play cackle,Track 2, folder 2
            do
               if Wait_If_Busy=1 then gosub Spellcast 'wait unti track ends  
            loop
		 
Spellcast:										
            serout TENDA,BAUD,($00) 'random selection of verses
            pause 1000
            do
                if Wait_If_Busy=1 then gosub Cauldron'watch for spellcast to end and return to start0
            loop

Cauldron:
		random w1                  'stir the pot
		serout TENDA,Baud,($F2,10) 'play cauldron track, Folder 2,file 10
		pause 1000            
		w2=w1//30000+60000 'generate random pause between 30 and 60 seconds before retrigger 
		pause w2
		goto Main0
Clear_Debounce:
            Debounce=0
            return
	                              
		
Start1:


            do
                High Magic_Hand
                random w3
                w4=w3//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w4
                Low Magic_Hand
                random w3
                w4=w3//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w4
		loop
Start2:
	       do
                High Elbow
                random w5
                w6=w5//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w6
                Low Elbow
                random w5
                w6=w5//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w6
		loop
Start3:
		       do
                High Shoulder
                random w7
                w8=w7//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w8
                Low Shoulder
                random w7
                w8=w7//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w8
		loop
 

Jamster

Senior Member
Also, can you link to the datasheet of the mp3 module. I reckon that there will be an option to stop the mp3 module from waiting to the end of the track before changing in there.
 

Pat13

Senior Member
Here is a link to the module on MDfly, thru which i purchased mine.
http://www.mdfly.com/index.php?main_page=product_info&cPath=9_53&products_id=284
and here is a data sheet for the Tenda.
http://www.thaieasyelec.net/archives/Manual/TDB380 datasheet V2[1].0 .pdf
I contacted Tenda when i was having some issue and they assured me that the MDFly version is not made by them. However, the codes seem to work.
Most of the info for this module I got from this thread. Have re read the thread several times and have used Drivesort to sort the files on a FAT32 formatted card.
http://www.picaxeforum.co.uk/showthread.php?16353-mp3-playback-module&highlight=tenda
 

Haku

Senior Member
I can't say if this works as I haven't setup one of my Tenda boards, but it should work.

The main part of what I modified was the loops that waited for the mp3 to stop playing, then play it again in the case of the track 1 folder 2, or go on to the next section in the case of the other tracks when the PIR is triggered.

Code:
#Picaxe 18M2
'Witch Prop

symbol PIR= B.7      `PIR sensor
symbol TENDA=B.3 `Tenda RXD  `
symbol Debounce=b18
symbol BAUD=T4800
symbol Wait_If_Busy=pinB.1 `Tenda Busy Line
symbol Magic_Hand=C.0
symbol Elbow=C.6
symbol Shoulder=C.7

Start0:


	let w0 = time
	random w1
	pause w2

Init0:                'Staring point to setup all inputs, outputs and set variables
 
            serout TENDA,BAUD, ($EF);       'STOP MP3 module 
            pause 10000
            serout TENDA,BAUD, ($E0)        `set Tenda Volume off
            pause 60000  'one minute pause to let PIR settle down
            

Main0:
		serout TENDA, BAUD, ($E8)           'set Tenda volume
		debounce=0
		Do
			if Wait_If_Busy=1 then              '0=playing, 1=stopped
				serout TENDA, Baud, ($F2,01)  `play Track 1, Folder 2
				pause 1000                    'give Tenda time to start playing track
			endif
			readadc PIR,debounce 
		Loop While debounce<50              ' exit loop if PIR triggered
		
Open_chant:
		Pause 1000
		serout TENDA,BAUD,($F2,02)        'play cackle,Track 2, folder 2
		do while Wait_If_Busy=0           'wait until track ends  
		loop
		 
Spellcast:										
		serout TENDA,BAUD,($00)           'random selection of verses
		pause 1000
		do while Wait_If_Busy=0           'wait until track ends  
		loop

Cauldron:
		random w1                  'stir the pot
		serout TENDA,Baud,($F2,10) 'play cauldron track, Folder 2,file 10
		pause 1000            
		w2=w1//30000+60000 'generate random pause between 30 and 60 seconds before retrigger 
		pause w2
		serout TENDA,BAUD, ($EF);       'STOP MP3 module 
		goto Main0
                            
		
Start1:


            do
                High Magic_Hand
                random w3
                w4=w3//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w4
                Low Magic_Hand
                random w3
                w4=w3//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w4
		loop
Start2:
	       do
                High Elbow
                random w5
                w6=w5//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w6
                Low Elbow
                random w5
                w6=w5//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w6
		loop
Start3:
		       do
                High Shoulder
                random w7
                w8=w7//1000 + 5000 'random ON time between 1 and 5 seconds
                pause w8
                Low Shoulder
                random w7
                w8=w7//1000 + 5000 'random OFF time between 1 and 5 seconds
		    pause w8
		loop
If you really get stuck I could breadboard one of my Tenda modules with some test mp3s to test the code on real hardware instead of just checking it compiles ok.
 
Last edited:

Pat13

Senior Member
Yes, that works. Still getting weird responses from the Tenda though. Sometimes the first mp3 ($01) will repeat after 7 or 8 seconds instead of the playing full 25 seconds (if not triggered). Also, in the cauldron section,the mp3 ($F2,10) isn't playing. But I just realized something as I was writing this. There are only 3 mp3s in F2 (A001,A002,A010),so instead of calling F2,10 I should be calling F2,03.
 
Top