DRPlayer with 08M2 chip

1968neil

Senior Member
From what i remember there was an indexing issue with the FAT which meant you'd have to use Drive Sort or the card read wouldn't always work correctly.
I have always used the MP3 and 0001.mp3 format without a problem. Im sure this was a work around at the time.
 

hippy

Ex-Staff (retired)
From what i remember there was an indexing issue with the FAT which meant you'd have to use Drive Sort or the card read wouldn't always work correctly.
Yes, I believe it uses '1st', '2nd', '3rd' etc for finding folders and files and the numeric naming of things matters not one jot if the folders and files aren't added to the file system in the right order, if Drive Sort hasn't reordered them to be in numeric order.

I recently battled that very issue with a portable MP3 player which I needed to hold one album which it would choose by default and play all tracks in correct order. That played folders and tracks in the order found on disk so involved deleting all folders with MP3 files in, creating a single album folder and adding tracks one at a time.

It was easier and quicker to do that at the time than find a version of Drive Sort for Windows 10.

I have always used the MP3 and 0001.mp3 format without a problem. Im sure this was a work around at the time.
Thanks for the confirmation.
 

regpye

New Member
Totally do-able as 1968neil says. I would say it falls into the category of "what a PICAXE is perfect for".

You can have a 5 x 4 button matrix or larger which is easy enough with the right PICAXE and you could even hack a PS/2 keyboard. Then you just need to scan for a button push or wait for a key press, play the track and ignore any new pushes until it has finished.

If it's a button matrix I believe that can also be used to light a LED in the button or next to it so it's more obvious which the track is for. It's a slightly more complicated matrix but might well be worth the effort.
I like that idea, but I don't know how to implement that at this stage, would love to learn more, the idea of a LED to show which is active at the time I think is rather important.
 

regpye

New Member
I am still waiting for the DFPlayers to arrive, it takes so long to get from overseas and I haven't been able to find them locally. (South Australia)
While waiting, I have thought of another project to try and I think I can work it out, but to make sure I thought I should pose the questions here.

This is a simple project using just a 08M2, a DFPlayer, a lithium 3.6v battery, an LDR, and a solar cell and a cheap balance charger TP4056 for charging the battery.
The idea is to have the battery charged by the solar cell to power everything. The LDR to determine night or day because I don't want this working at night. The DFPlayer to play a single file of birds calling.
What I am not sure about is the length of an MP3 file that can be played and also how to get the file to repeat after playing.
The idea behind this project is to have some bird calls playing in the garden but only during the daytime.
The length of the file I have is about 1 hour in length.
 

papaof2

Senior Member
DriveSort may need mfc140u.dll, which can be found with a Google (or other) search. Put it in the folder with DriveSort. My Win 10 PCs have a folder in Program Files named "other". It's specifically for programs that don't have an associated install wrapper.
If you're using Win 10 (maybe earlier versions), uacpas32 is a useful tool to ensure that programs start with the proper "baggage" of who and what. It's also free.
 

1968neil

Senior Member
I am still waiting for the DFPlayers to arrive, it takes so long to get from overseas and I haven't been able to find them locally. (South Australia)
While waiting, I have thought of another project to try and I think I can work it out, but to make sure I thought I should pose the questions here.

This is a simple project using just a 08M2, a DFPlayer, a lithium 3.6v battery, an LDR, and a solar cell and a cheap balance charger TP4056 for charging the battery.
The idea is to have the battery charged by the solar cell to power everything. The LDR to determine night or day because I don't want this working at night. The DFPlayer to play a single file of birds calling.
What I am not sure about is the length of an MP3 file that can be played and also how to get the file to repeat after playing.
The idea behind this project is to have some bird calls playing in the garden but only during the daytime.
The length of the file I have is about 1 hour in length.
The TP4056 input voltage is 4-8v which may need some careful consideration when choosing a solar panel as the voltages vary wildly.
The typical battery to charge from the TP4056 is 3.7v cell eg: 18650, they would work well in this application.
I would recommend looking at the Sleep command for the picaxe as you would then be able to draw minimum current over a long period and lower you discharge of the battery
The DF player can handle very large track sizes, i have used them in background music systems in shopping centres with no issues.
1hr long files will be fine.
The only issue i see might be from the "Green" perspective in that by playing bird calls during the daytime you may attract a lot of cats and that may have its own problems :)
 

regpye

New Member
Thanks Neil,
I have ordered some 5.5volt solar panel, under 1 watt, so I think that there should be no over charging issues.
Good to know about the file size, that had me a bit worried although if needed I could have cut the file up into smaller sizes.
I will look at the sleep command, not used that before, good idea I think and will expand my knowledge.
After looking at the sleep command it looks like I could send a command out to the DFPlayer then put the 08M2 to sleep for about an hour by using sleep 1500, is that correct?
 
Last edited:

regpye

New Member
I am not sure if I have done this right as I can't check it for real, still waiting for hardware.

I have tried to use the sleep command and I found that it really does make the picaxe go to sleep as when remmed out it will allow the next command to execute.
Anyway this is what I have come up with, probably a few mistakes and better ways of doing things.
Code:
#no_data           ; Optional to speed downloading program modifications
setfreq m8        ; increase frequecy 
#terminal 9600
hsersetup B9600_16,%10
pause 2000         ; Recommended to give the terminal time to start up

Symbol Busy_Pin = PinC.1
Symbol Busy_Delay = 200
Symbol TX = B.4
Symbol BAUD_FREQ = M8
Symbol BAUD = T9600_8
Symbol LDR = C.2
Symbol cmd = b0
Symbol arg = w1 ; b3:b2
Symbol arg.lsb = b2
Symbol arg.msb = b3
;XL = $08 : YW = $01  ; repeat play FAT file number 01 (valid 01...255)

start:
readadc LDR,b10 ; read LDR for night or day into variable b0
if b10 > 105 then Day
if b10 < 95 then nighttime
if b10 > 96 and b0 < 104 then waiting
goto done

Day:
SerTxd("DAYTIME birds are singing", CR, LF )
High TX ; set TX pin high for idle high serial
Pause 500
SerTxd("Play MP3 folder song 0001.mp3", CR, LF )
cmd = $08 : arg = 0001 : Gosub Send    ;$12 play tracks from the default \MP3 folder - 0001 = track 1 $08 repeat track
Pause 500
sleep 1500        ; put 08M2 in sleep mode for about 1 hour
goto start

nighttime:
SerTxd("NIGHTTIME birds are sleeping", CR, LF )
check:
readadc LDR,b10 ; read LDR for night or day into variable b0
if b10 > 105 then Day
;sleep 3000
goto check

Send:
 SetFreq BAUD_FREQ
 Pause 10
 SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
 gosub Check_Busy
 SetFreq MDEFAULT
 Return
 
Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0
Return

waiting:
wait 6

done:
goto start
 

hippy

Ex-Staff (retired)
then put the 08M2 to sleep for about an hour by using sleep 1500, is that correct?
The SLEEP and NAP commands are not guaranteed to be particularly accurate, have -50%/+100% tolerance and that can vary with temperature.. The nominal SLEEP unit is 2.3 seconds so SLEEP 1500 should be about an hour.

You may need to calibrate that to get it roughly right and if you can live with some variation that may be good enough to use. There are some other techniques such as using SETFREQ to drop the operating frequency, extend PAUSE times, while reducing power consumption which may be good enough alternatives.

I would suggest not worrying about minimising current consumption and just use PAUSE until you have things prototyped and can assess if current saving is actually needed. If you need reasonably accurate timing it would be wise to keep the I2C pins free so you can add an external battery backed-up RTC later if needed.

I'm actually in the unenviable position of wanting to get rid of real life birdsong since this year has attracted some critter who emits a fine "badda-bing-badda-boom' at four in the morning which is impossible to ignore once you have latched on to it after being woken by it. Then the rest of the crowd start echoing variants of that. It completely breaks the pleasant randomness of birdsong I am used to which is easy to fall asleep to. I'm sure it would be fascinating to someone interested in birdsong.
 

regpye

New Member
I'm actually in the unenviable position of wanting to get rid of real life birdsong since this year has attracted some critter who emits a fine "badda-bing-badda-boom' at four in the morning which is impossible to ignore once you have latched on to it after being woken by it.
I am very lucky in that way, I have a cochlear implant and I remove the processor at night and don't hear anything, it is good too if the wife starts to nag too much.

Thanks for the tips Hippy, as you can see from my code I worked out the length of time for about one hour. What I am not sure about is if I have got the repeat code right. Maybe I should split the MP3 file up into smaller pieces and play them as separate files and that way I can check the day or night better so as not to have an extended play into the evening by using a long playing file that might start just before dusk?
 

hippy

Ex-Staff (retired)
What I am not sure about is if I have got the repeat code right.
Using '$08' does seem to be correct looking at SPE035.PDF.

And, having looked more closely at that, it does cover the use of '/MP3', two digit folders, three or four digit filenames. in Appendix 2. It does seem that all combinations are supported.

Maybe I should split the MP3 file up into smaller pieces and play them as separate files and that way I can check the day or night better so as not to have an extended play into the evening by using a long playing file that might start just before dusk?
I don't think you need to split the track if it works. You should be able to set the track repeating forever, PAUSE or SLEEP for a shorter period of time, check the LDR, sleep again, or issue a '$16' stop command., reissue a '$08' repeat command when you want the racket beautiful birdsong to start again.

There are various volume control commands so you can fade-out before stopping so it's not a sudden silence.

Rather than jumping to 'start' and deciding what to do I would have something more akin to a 'state machine' where you simply progress through doing things, let various subroutines check LDR and return or not. Perhaps something like -
Code:
MainLoop:
  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
Then you only need to provide the code for each subroutine to do what they say they will do. For example -
Code:
WaitForDawn:
  Do
    Pause 1000
    ReadAdc LDR, b0
  Loop Until b0 > 105
  Return

WaitForDusk:
  Do
    Pause 1000
    ReadAdc LDR, b0
  Loop Until b0 < 95
  Return
Those 'Pause 1000' could become 'Gosub SleepForSomeTime' to conserve current.
 

regpye

New Member
Rather than jumping to 'start' and deciding what to do I would have something more akin to a 'state machine' where you simply progress through doing things, let various subroutines check LDR and return or not. Perhaps something like -
Thanks once again Hippy, you have my grey matter working overtime, can't get old while in this state of mind.
Some good ideas there, I will try and work them out, seems like the poor little 08M2 is not going to sit there too idle as first thought, it is going to have to work a little harder ha..ha..:D
 

regpye

New Member
I tried but I have lost the plot, got lost on the way. Don't know what I am doing.
Code:
; *******************************
;    Written by:
;    Function:        
;    Target PICAXE:    
; *******************************
#picaxe 08m2       ; Optional for verification
#no_data           ; Optional to speed downloading program modifications
setfreq m8        ; increase frequecy 
#terminal 9600
hsersetup B9600_16,%10
pause 2000         ; Recommended to give the terminal time to start up

Symbol Busy_Pin = PinC.1
Symbol Busy_Delay = 200
Symbol TX = B.4
Symbol BAUD_FREQ = M8
Symbol BAUD = T9600_8
Symbol LDR = C.2
Symbol cmd = b0
Symbol arg = w1 ; b3:b2
Symbol arg.lsb = b2
Symbol arg.msb = b3

SYMBOL XW = W2
SYMBOL XL = B4
SYMBOL XH = B5
SYMBOL YW = W3
SYMBOL YL = B6
SYMBOL YH = B7
SYMBOL ZW = W4
SYMBOL ZL = B8
SYMBOL ZH = B9


MainLoop:
  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop

WaitForDawn:
  Do
    Pause 1000
    ReadAdc LDR, b10
  Loop Until b10 > 105
  Return
SetVolumeToFull:

cmd = $06 : arg = 0001 : Gosub Send    ; set volume
Pause 500
Gosub PlayTrackWithRepeat
 Return
PlayTrackWithRepeat:
  Do
cmd = $08 : arg = 0001 : Gosub Send    ;play tracks from the default \MP3 folder - 0001 = track 1 $08 repeat track
Pause 500
loop
  Return
WaitForDusk:
  Do
    Pause 1000
    ReadAdc LDR, b10
  Loop Until b10 < 95

  Return
  
FadeDownVolume:
  Do
cmd = $05 : arg = 0001 : Gosub Send  ;Decrease volume track one
loop
  Return
StopTrack:
  Do
cmd = $16 : arg = 0001 : Gosub Send  ;stop playing track one 
loop
  Return
SleepForSomeTime:
  Do
sleep 500  ; NOT SUREHOW TO USETHIS
loop
  Return
  
  Send:
 SetFreq BAUD_FREQ
 Pause 10
 SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
 gosub Check_Busy
 SetFreq MDEFAULT
 Return

Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0
Return
 

kfjl

Member
I tried but I have lost the plot, got lost on the way. Don't know what I am doing.
I'm not surprised. I doubt anyone on the forum will agree with me, but for me, too many symbols make a program unreadable. Especially symbols you don't use!
The best thing would probably be to start again, using only code you do understand and have got working, and leave out the "complications" such as day, night or sleeping.
I don't use the busy pin. The DFPlayer already has the behaviour I consider normal, that is: if I press the "next" button I don't want to have to listen to the end of the current track before going to the next one. (some of the "Harry Potter" audio book chapters last for more than an hour!)

PS
It looks like you're using the same code I used (ages ago). Do you have a link to the original?
 

regpye

New Member
PS
It looks like you're using the same code I used (ages ago). Do you have a link to the original?
No the code is made up from what I have been learning (or trying to learn) here on the forum in this thread.
The night and day is important for this project as I don't want it playing at night and getting the neighbours upset.
 

1968neil

Senior Member
No the code is made up from what I have been learning (or trying to learn) here on the forum in this thread.
The night and day is important for this project as I don't want it playing at night and getting the neighbours upset.
Code:
;*****************************************************************************
;*                 Input Output pin Definitions                              *
;*****************************************************************************
Symbol Busy_Pin = PinC.1       ; DF Player Busy Pin to 08M2 Pin
Symbol LDR = C.2               ; LDR to 08M2 Pin
Symbol TX = C.4               ; Picaxe Data Out to DF Player Data in Pin

;*****************************************************************************
;*                          Variable Definitions                             *
;*****************************************************************************
Symbol cmd = b0
Symbol arg = w1 ; b3:b2
Symbol arg.lsb = b2
Symbol arg.msb = b3
Symbol Light_Level = b10 ;  Variable for LDR ADC Read value
;*****************************************************************************
;*             Timing & ADC Value Preset Definitions                         *
;*****************************************************************************

Symbol BAUD_FREQ = M8
Symbol BAUD = T9600_8
Symbol Busy_Delay = 200  ; Change Busy delay value here
Symbol Dark  = 105       ; Change Darkness trigger level value here
Symbol Light = 95        ; Chanhe Daylight Trigger Level value Here
;*****************************************************************************
;*                            THE MAIN PROGRAM                               *
;*****************************************************************************
MAIN:
  Gosub Check_If_Dawn_Or_Dusk      ; Goto sub routine and check if its Light or Dark
 ; Sleep 10                        ; adding the ; symbol before a command removes it from the program flow
                                   ; the sleep command works in multiples of 2.3 seconds (1-65525)
  Goto Main


Check_If_Dawn_Or_Dusk:

ReadAdc LDR, Light_Level
if light_Level > 105 then Gosub Dawn
if light_Level < 95  then Gosub Dusk
Return

;******************************************************************************
;*             Routine that Run when it becomes Light (Dawn)                  *
;******************************************************************************
Dawn:
cmd = $06 : arg = 0001 : Gosub Send    ;  SetVolumeToFull:
Pause 500
cmd = $08 : arg = 0001 : Gosub Send    ;play tracks from the default \MP3 folder - 0001 = track 1 $08 repeat track

Return



;******************************************************************************
;*             Routine that Run when it becomes Dark (Dusk)                   *
;******************************************************************************
Dusk:
 cmd = $05 : arg = 0001 : Gosub Send  ;Decrease volume track one
 Pause 500
 cmd = $16 : arg = 0001 : Gosub Send  ;stop playing track one
Return
 
;*************************************************************************************
;* Routine that Sends the commands to the DF Player and Checks if DF Player is Busy  *
;*************************************************************************************
 
Send:
 SetFreq BAUD_FREQ
 Pause 10
 SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
 gosub Check_Busy
 SetFreq MDEFAULT
 Return

Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0
Return
Try this i have slimmed it down a bit to be easier on the eye and removed all the unused odds and ends and added comments to help you understand the code, Have fun :)
 

regpye

New Member
Try this i have slimmed it down a bit to be easier on the eye and removed all the unused odds and ends and added comments to help you understand the code, Have fun
Thanks for that Neil, it does make it easier to understand and is certainly a lot cleaner and simplified.
 

hippy

Ex-Staff (retired)
I'm with 'kfjl' that often the best thing to do is to start from scratch. That doesn't mean throwing away all you have done - that can still be used, but you may have to mould it to fit the new version.

I would also agree that it doesn't seem necessary to use the BUSY pin as its main use is in being able to tell when a track has ended and, with a repeated forever track, it never will.

I would normally try and show how I would do it in steps but in this case 'write the main code then fill in the missing subroutines' isn't much better than showing what my final result would be in full, though I have split it into distinct parts. All untested -
Code:
#Picaxe 08M2
#No_Data

; Circuit

;      .----__----.
;     -| V+    0V |-
;     -| SI    SO |-
;  .---| X4    X1 |-
;  |  -| I3    X2 |<--- LDR Input
;  |   `----------'
;  `------------------> DFPlayer RX

; Define hardware

Symbol LDR                = C.2
Symbol TX                 = C.4

Symbol LDR_LEVEL_AT_DAWN  = 105      ; LDR reads higher than this during the day
Symbol LDR_LEVEL_AT_DUSK  =  95      ; LDR reads less than this at night

Symbol BAUD_FREQ          = M8
Symbol BAUD               = T9600_8

Symbol MP3_REPEAT_PLAY    = $08      ; Repeat play a track
Symbol MP3_STOP           = $16      ; Stop track playing
Symbol MP3_SET_VOLUME     = $06      ; Set the volume

Symbol MAX_VOLUME         = $1E      ; Max volume level, $1E = 30

; Variables

Symbol cmd                = b0
Symbol arg                = w1 ; b3:b2
Symbol arg.lsb            = b2
Symbol arg.msb            = b3

Symbol ldrLevel           = b4

; Main program

PowerOnReset:
  High TX

MainLoop:
  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
Code:
; Day and night handling

WaitForDawn:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel > LDR_LEVEL_AT_DAWN
  Return

WaitForDusk:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel < LDR_LEVEL_AT_DUSK
  Return

SleepForSomeTime:
  Sleep 1 ; Doesn't really matter how long we sleep for at this stage
  Return

ReadTheLdrLevel:
  ReadAdc LDR, ldrLevel
  Return
Code:
; Handle track playing

PlayTrackWithRepeat:
  cmd = MP3_REPEAT_PLAY : arg = 0001 : Gosub Send ; Repeat track '/MP3/0001.mp3'
  Return

StopTrack:
  cmd = MP3_STOP : arg = 0 : Gosub Send ; Stop playing track
  Return
Code:
; Handle volume control

SetVolumeToFull:
  cmd = MP3_SET_VOLUME : arg = MAX_VOLUME : Gosub Send ; Set volume
  Return

FadeDownVolume:
  For arg = MAX_VOLUME To 0 Step -1  
    cmd = MP3_SET_VOLUME : Gosub Send ; Set volume, 'arg' set by outer FOR-NEXT
    Pause 1000
  Next
  Return
Code:
; Communication with DFPlayer

Send:
  SetFreq BAUD_FREQ
  Pause 10
  SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
  SetFreq MDEFAULT
  Return
Please feel free to ask any questions you have.
 

regpye

New Member
Please feel free to ask any questions you have.
I will be away for a few days, but I am going to try by myself to use this code and play two files, one that starts off in the morning playing Australian magpies followed by a second and repeating file of little birds for the rest of the day.
I think I can work it out and will post my try out in a few days when I get back.
 

hippy

Ex-Staff (retired)
will be away for a few days, but I am going to try by myself to use this code and play two files, one that starts off in the morning playing Australian magpies followed by a second and repeating file of little birds for the rest of the day.
That should be possible. That will need to use the BUSY pin to detect when the first has ended but it should be easy enough to modify the main loop which I proposed -
Rich (BB code):
  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayFirstTrack
    Gosub WaitForTrackToEnd
    Gosub PlaySecondTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
 

regpye

New Member
I got back early, bad weather.
I have tried a few things, got the DFPlayers today when I got back and made up a circuit to test.
Made a few changes as suggested, but not all is working correctly.
The LDR doesn't seem to work player is playing in the dark as well as the light, and the first file keeps repeating, even if I don't select that file at all.

When I first ran the unmodified version of Hippy's the sound was almost non existent, I increased the pauses a lot and the sound increased to a loud level and was clear.

Code:
; *******************************
;    Written by:
;    Function:        
;    Target PICAXE:    
; *******************************
#No_Data

; Circuit

'      .----__----.
;     -| V+    0V |
;     -| SI    SO |-
;  .---| X4    X1 |-
;  |  -| I3    X2 |<--- LDR Input
;  |   `----------'
;  `------------------> DFPlayer RX

; Define hardware

Symbol LDR                = C.2
Symbol TX                 = C.4
Symbol Busy_Pin           = PinC.1
Symbol LDR_LEVEL_AT_DAWN  = 105      ; LDR reads higher than this during the day
Symbol LDR_LEVEL_AT_DUSK  =  95      ; LDR reads less than this at night

Symbol BAUD_FREQ          = M8
Symbol BAUD               = T9600_8

Symbol MP3_PLAY          = $12     ; play track
Symbol MP3_REPEAT_PLAY    = $08      ; Repeat play a track
Symbol MP3_STOP           = $16      ; Stop track playing
Symbol MP3_SET_VOLUME     = $06      ; Set the volume

Symbol MAX_VOLUME         = $1E      ; Max volume level, $1E = 30

; Variables

Symbol cmd                = b0
Symbol arg                = w1 ; b3:b2
Symbol arg.lsb            = b2
Symbol arg.msb            = b3

Symbol ldrLevel           = b4
Symbol Busy_Delay = 200  ; Change Busy delay value here
; Main program

PowerOnReset:
  High TX

  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayFirstTrack
    Gosub WaitForTrackToEnd
    Gosub PlaySecondTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
; Day and night handling

WaitForDawn:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel > LDR_LEVEL_AT_DAWN
  Return

WaitForDusk:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel < LDR_LEVEL_AT_DUSK
  Return

SleepForSomeTime:
  Sleep 1 ; Doesn't really matter how long we sleep for at this stage
  Return

ReadTheLdrLevel:
  ReadAdc LDR, ldrLevel
  Return
  
PlayFirstTrack: ; Handle track playing
cmd = MP3_PLAY : arg = 0001 : Gosub Send ;
gosub WaitForTrackToEnd
 Return
 
 WaitForTrackToEnd:
 gosub Check_Busy        ; ???????????? Don't know what I am doing here
 Return
 
PlaySecondTrackWithRepeat:
  cmd = MP3_REPEAT_PLAY : arg = 0002 : Gosub Send ; Repeat track '/MP3/0002.mp3'
  Return

StopTrack:
  cmd = MP3_STOP : arg = 0 : Gosub Send ; Stop playing track
  Return

; Handle volume control

SetVolumeToFull:
  cmd = MP3_SET_VOLUME : arg = MAX_VOLUME : Gosub Send ; Set volume
  Return

FadeDownVolume:
  For arg = MAX_VOLUME To 0 Step - 1  
    cmd = MP3_SET_VOLUME : Gosub Send ; Set volume, 'arg' set by outer FOR-NEXT
    Pause 2000
  Next
  Return

; Communication with DFPlayer

Send:
  SetFreq BAUD_FREQ
  Pause 1000
  SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
  gosub Check_Busy
  SetFreq MDEFAULT
  Return
  
Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0
Return
 

kfjl

Member
The night and day is important for this project as I don't want it playing at night and getting the neighbours upset.
You should be OK as long as you tell the neighbours to switch off their headlights when they drive past your house. 😇
 

1968neil

Senior Member
I got back early, bad weather.
I have tried a few things, got the DFPlayers today when I got back and made up a circuit to test.
Made a few changes as suggested, but not all is working correctly.
The LDR doesn't seem to work player is playing in the dark as well as the light, and the first file keeps repeating, even if I don't select that file at all.

When I first ran the unmodified version of Hippy's the sound was almost non existent, I increased the pauses a lot and the sound increased to a loud level and was clear.

Code:
; *******************************
;    Written by:
;    Function:       
;    Target PICAXE:   
; *******************************
#No_Data

; Circuit

'      .----__----.
;     -| V+    0V |
;     -| SI    SO |-
;  .---| X4    X1 |-
;  |  -| I3    X2 |<--- LDR Input
;  |   `----------'
;  `------------------> DFPlayer RX

; Define hardware

Symbol LDR                = C.2
Symbol TX                 = C.4
Symbol Busy_Pin           = PinC.1
Symbol LDR_LEVEL_AT_DAWN  = 105      ; LDR reads higher than this during the day
Symbol LDR_LEVEL_AT_DUSK  =  95      ; LDR reads less than this at night

Symbol BAUD_FREQ          = M8
Symbol BAUD               = T9600_8

Symbol MP3_PLAY          = $12     ; play track
Symbol MP3_REPEAT_PLAY    = $08      ; Repeat play a track
Symbol MP3_STOP           = $16      ; Stop track playing
Symbol MP3_SET_VOLUME     = $06      ; Set the volume

Symbol MAX_VOLUME         = $1E      ; Max volume level, $1E = 30

; Variables

Symbol cmd                = b0
Symbol arg                = w1 ; b3:b2
Symbol arg.lsb            = b2
Symbol arg.msb            = b3

Symbol ldrLevel           = b4
Symbol Busy_Delay = 200  ; Change Busy delay value here
; Main program

PowerOnReset:
  High TX

  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayFirstTrack
    Gosub WaitForTrackToEnd
    Gosub PlaySecondTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
; Day and night handling

WaitForDawn:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel > LDR_LEVEL_AT_DAWN
  Return

WaitForDusk:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel < LDR_LEVEL_AT_DUSK
  Return

SleepForSomeTime:
  Sleep 1 ; Doesn't really matter how long we sleep for at this stage
  Return

ReadTheLdrLevel:
  ReadAdc LDR, ldrLevel
  Return
 
PlayFirstTrack: ; Handle track playing
cmd = MP3_PLAY : arg = 0001 : Gosub Send ;
gosub WaitForTrackToEnd
Return

WaitForTrackToEnd:
gosub Check_Busy        ; ???????????? Don't know what I am doing here
Return

PlaySecondTrackWithRepeat:
  cmd = MP3_REPEAT_PLAY : arg = 0002 : Gosub Send ; Repeat track '/MP3/0002.mp3'
  Return

StopTrack:
  cmd = MP3_STOP : arg = 0 : Gosub Send ; Stop playing track
  Return

; Handle volume control

SetVolumeToFull:
  cmd = MP3_SET_VOLUME : arg = MAX_VOLUME : Gosub Send ; Set volume
  Return

FadeDownVolume:
  For arg = MAX_VOLUME To 0 Step - 1 
    cmd = MP3_SET_VOLUME : Gosub Send ; Set volume, 'arg' set by outer FOR-NEXT
    Pause 2000
  Next
  Return

; Communication with DFPlayer

Send:
  SetFreq BAUD_FREQ
  Pause 1000
  SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
  gosub Check_Busy
  SetFreq MDEFAULT
  Return
 
Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0
Return
I would suggest running the LDR on its own and reading the value back to see where they actually sit, it sounds to me like the ADC values are probably incorrect.

Run this and open serial monitor:
You should get the values display in the terminal window.
Unfortunately not all LDR's will have equal values due to manufacturing tolerances of such a cheap part.

Code:
Symbol LDR                = C.2

main:   

readadc LDR, b1        ; read the value
    sertxd ("LDR Value = ",#b1,cr,lf)    ; show the value read
    pause 500        ; wait a short while
    goto main        ; repeat
Once you have the values then you can update the your program with known values and go from there.
Good luck :)

25847
 

regpye

New Member
Once you have the values then you can update the your program with known values and go from there.
Good luck
I tried that out and used a torch in a darkened room to do the test and this is what I got back
My circuit is the same.

Code:
[98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][E0][98]`0[18]3[00][F8]x3[06]<[E0][9E]f?f<[00][F8][E6][F8][00][F8][18][0F]f[0F]f[0F][E6][80][98][80][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][00][00][00][00][FF][00]f[E6][98][98][00][FF][00][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00]f[E6][98][98][00][00][00][00][00][FF][00][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00]f[E6][98][98][00][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00]f[E6][98][98][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][00][00][00][FF][00][00][FF][00][00][00][FF]f[E6][98][98][00][00][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00]f[CC][98][98][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][00][00][00][00][00]f[E6][98][98][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][00][00][FF][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00]f[E6][98][98][00][00][00][FF][00][00][00][00][00][FF][00][00][00][00][FF][00][00][00][00][00][FF][00][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][00][FF][00][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][00][FF]f[E6][98][98][00][00][FF][00][00][FF][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF][00][00][FF][00][00][00][00][00][FF][00][00][FF][00][00][00][FF][00][00][00][FF][00][00][FF][00][00][FF][00][00][00][FF]f[E6][98][98][00][00][00][00][FF][00][00][FF][00][00][00][00][FF][
 

regpye

New Member
I used data link and got this back, maybe the LDR is shorted out or disconnected.

LDR Value = 253
LDR Value = 253
LDR Value = 252
LDR Value = 255
LDR Value = 255
LDR Value = 255
LDR Value = 255
LDR Value = 255
LDR Value = 255
LDR Value = 253
 

regpye

New Member
I changed the wiring a little and got better results

LDR Value = 3
LDR Value = 2
LDR Value = 163
LDR Value = 4
LDR Value = 109
LDR Value = 105
LDR Value = 2

I reprogrammed the chip with the song player code and now the LDR is working, but once the song starts after the light has been shined on the LDR the file keeps playing when the LDR is in the dark.
Still getting the first file playing repeatedly, I must have done something wrong in the code I think.
 
Last edited:

regpye

New Member
I am now trying to sort out the file sort system.
I formatted an SD card to fat32.
I then made 3 files in a text to speak program and saved out a MP3 file from each in the following order 0001.mp3 first, 0003.mp3 second, 0002.mp3 third.
I did it in that order to be able to test the code and player.
the results were 0001 played first followed by 0002 repeating, just as it should be.
Then I put the SD card in that has the bird calls and I just get 0001 repeating (bloody magpies) all the time.
0002.mp3 never plays
 

regpye

New Member
SOLVED the file order system.
I used a free program FAT32Sorter on the SD card and now it is playing as it should, the first file plays and stops followed by the second file.

Now only have to work out why the LDR part is not turning the system off when in the dark. On first start-up while in subdued light the player doesn't start, but if I shine a light on the LDR it will come alive and start playing. If I leave it in the dark it continues to play but should slowly fade in volume and stop playing.
 

1968neil

Senior Member
SOLVED the file order system.
I used a free program FAT32Sorter on the SD card and now it is playing as it should, the first file plays and stops followed by the second file.

Now only have to work out why the LDR part is not turning the system off when in the dark. On first start-up while in subdued light the player doesn't start, but if I shine a light on the LDR it will come alive and start playing. If I leave it in the dark it continues to play but should slowly fade in volume and stop playing.
Try commenting out the check busy line in the code with. ;
It looks like it’s ignoring the dark setting because it’s busy playing and keeping the program in that loop until it’s finished maybe ?
 

regpye

New Member
Try commenting out the check busy line in the code with. ;
Okay I am trying that, have to wait now a few hours to see what happens
Actually I didn't have to wait long, it seems to work better, I will give it a full test in real daylight tomorrow.
Using just a torch to activate it and the turning the torch off the second file started and then faded away before turning off. Not sure about the first file as it didn't play for vey long before the second file started.
 
Last edited:

regpye

New Member
With the check_busy remmed out the first file plays for a few seconds and then the second file starts.
There needs to be some code change that allows the first file to complete and then close that file before starting the second file.
Please feel free to ask any questions you have.
Need you help Hippy, I've got stuck now, works well one way and not another, notes at bottom of code.
Code:
#No_Data

; Circuit

'      .----__----.
;     -| V+    0V |
;     -| SI    SO |-
;  .---| X4    X1 |-
;  |  -| I3    X2 |<--- LDR Input
;  |   `----------'
;  `------------------> DFPlayer RX

; Define hardware

Symbol LDR                = C.2
Symbol TX                 = C.4
Symbol Busy_Pin           = PinC.1
Symbol LDR_LEVEL_AT_DAWN  = 105      ; LDR reads higher than this during the day
Symbol LDR_LEVEL_AT_DUSK  =  95      ; LDR reads less than this at night

Symbol BAUD_FREQ          = M8
Symbol BAUD               = T9600_8

Symbol MP3_PLAY          = $12     ; play track
Symbol MP3_REPEAT_PLAY    = $08      ; Repeat play a track
Symbol MP3_STOP           = $16      ; Stop track playing
Symbol MP3_SET_VOLUME     = $06      ; Set the volume

Symbol MAX_VOLUME         = $1E      ; Max volume level, $1E = 30

; Variables

Symbol cmd                = b0
Symbol arg                = w1 ; b3:b2
Symbol arg.lsb            = b2
Symbol arg.msb            = b3

Symbol ldrLevel           = b4
Symbol Busy_Delay = 2000  ; Change Busy delay value here
; Main program

PowerOnReset:
  High TX

  Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayFirstTrack
    Gosub WaitForTrackToEnd
    Gosub PlaySecondTrackWithRepeat
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
; Day and night handling

WaitForDawn:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel > LDR_LEVEL_AT_DAWN
  Return

WaitForDusk:
  Do
    Gosub SleepForSomeTime
    Gosub ReadTheLdrLevel
  Loop Until ldrLevel < LDR_LEVEL_AT_DUSK
  Return

SleepForSomeTime:
  Sleep 1 ; Doesn't really matter how long we sleep for at this stage
  Return

ReadTheLdrLevel:
  ReadAdc LDR, ldrLevel
  Return
  
PlayFirstTrack: ; Handle track playing
cmd = MP3_PLAY : arg = 0001 : Gosub Send ;
gosub WaitForTrackToEnd
 Return
 
 WaitForTrackToEnd:
 gosub Check_Busy        ; ????????????

 Return
 
PlaySecondTrackWithRepeat:
  cmd = MP3_REPEAT_PLAY : arg = 0002 : Gosub Send ; Repeat track '/MP3/0002.mp3'
  Return

StopTrack:
  cmd = MP3_STOP : arg = 0 : Gosub Send ; Stop playing track
  Return

; Handle volume control

SetVolumeToFull:
  cmd = MP3_SET_VOLUME : arg = MAX_VOLUME : Gosub Send ; Set volume
  Return

FadeDownVolume:
  For arg = MAX_VOLUME To 0 Step - 1  
    cmd = MP3_SET_VOLUME : Gosub Send ; Set volume, 'arg' set by outer FOR-NEXT
    Pause 2000
  Next
  Return

; Communication with DFPlayer

Send:
  SetFreq BAUD_FREQ
  Pause 1000
  SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
  gosub Check_Busy
  SetFreq MDEFAULT
  Return
  
Check_Busy:
Do
pause Busy_Delay
loop while Busy_Pin=0  
;While this is in the code the files work properly but the LDR,fading and stop playing don't work. Take this code out and the first file starts when there is light, runs fora few seconds and then the second file will start. Take the light away and the audio will fade out and the file will stop playing.
Return
 

1968neil

Senior Member
Try changing the busy loop to this as a temp measure you can then play with the light levels without having to wait. Remember to change it back for the finished article.
Code:
Check_Busy:
Do
pause Busy_Delay

ReadAdc LDR, Light_Level          ; Read LDR Whilst waiting for busy signal to change
if light_Level < 95  then exit    ; if the light level changes to the opposite LDR level exit the loop

loop while Busy_Pin=0
Return
 

hippy

Ex-Staff (retired)
I can see two issues. Firstly the 'Gosub WaitForTrackEnd' doesn't need to be within the 'PlayFirstTrack' routine as that is handled in the main DO-LOOP. Checking it has ended twice isn't problematic but it's not how the code was intended to be.

Second is calling 'Gosub CheckBusyPin' in the 'Send' routine. I suspect, as '1968neil', does that it's blocking until the track has completed playing. It therefore doesn't return to check the LDR until the track has finished which isn't what we want.

1) Remove the Gosub CheckBusyPin' from the 'Send' routine
2) Rename 'CheckBusyPin:' as 'WaitForTrackToEnd:'.
 

hippy

Ex-Staff (retired)
u should be OK as long as you tell the neighbours to switch off their headlights when they drive past your house. 😇
We can revisit night and day detection once we have it working with a torch. It should be possible to add code which will filter out 'false daylight' when headlights are detected and 'false night time' when the storm clouds gather. And that should be possible without changing the main DO-LOOP.

My good news is the 'badda-boom' bird seems to have migrated. The bad news is his 'bwaaah-bwaaah-bwaaah' pal has arrived doing a perfect impersonation of my alarm clock. And he's brought along a Guinness World Record wannabe emitting an impressively long 'twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik-twik'.

Suffice to say, if anyone ever needs a 'pre-dawn nuisance machine with plausible deniability', I have some tips.
 

regpye

New Member
Try changing the busy loop to this as a temp measure you can then play with the light levels without having to wait. Remember to change it back for the finished article.
This looks very promising a combination of both Hippy and 1968Neil fixes, seems to work with the torch on and off, I will check again in daylight tomorrow.
1) Remove the Gosub CheckBusyPin' from the 'Send' routine DONE
2) Rename 'CheckBusyPin:' as 'WaitForTrackToEnd:'. DONE
This has been a very interesting exercise

Code:
WaitForTrackToEnd:
Do
pause Busy_Delay
ReadAdc LDR, ldrLevel
if ldrLevel <95 then exit
loop while Busy_Pin=0  

Return
 

regpye

New Member
This looks very promising a combination of both Hippy and 1968Neil fixes, seems to work with the torch on and off, I will check again in daylight tomorrow.
1) Remove the Gosub CheckBusyPin' from the 'Send' routine DONE
2) Rename 'CheckBusyPin:' as 'WaitForTrackToEnd:'. DONE
This has been a very interesting exercise

Code:
WaitForTrackToEnd:
Do
pause Busy_Delay
ReadAdc LDR, ldrLevel
if ldrLevel <95 then exit
loop while Busy_Pin=0 

Return
SUCCESS, tested it today in day light and left it going all day and the birds went to bed at dusk, slowly quietening down before stopping their racket altogether. The magpies only make a racket early in the morning for about 12 minutes followed by little birds twitting all day until dusk. I couldn't tell the difference from real birds in the neighbourhood which are sometimes very quiet.
To give the second file a fade in, I altered the file itself using Audacity software, Hippy's code gave the fade out perfectly.

I would like to be able to put the system to sleep during the night and I guess that I could do that by adding a couple of sleep commands after the LDR has a reading of <95. That way I could save on battery power as there would be no day light to charge the battery and the charge during the day would be minimal because the system would be running and drawing current at the same time as charging. Not sure how long a sleep can be made in one go, so maybe a few will be needed to give about 6-7 hours sleep?
 

regpye

New Member
If I have worked it our correctly I should be able to make another section that goes like this;
I think it should give me about a 7 hour sleep which should cover most of the night time and save some battery power.
Code:
bedtime:
ReadAdc LDR, ldrLevel
if ldrLevel <95 then sleep 10900
endif
Return
 

hippy

Ex-Staff (retired)
To give the second file a fade in, I altered the file itself using Audacity software, Hippy's code gave the fade out perfectly.
I would have edited the main loop and added some more routines ...
Rich (BB code):
 Do
    Gosub WaitForDawn
    Gosub SetVolumeToFull
    Gosub PlayFirstTrack
    Gosub WaitForTrackToEnd
    Gosub SetVolumeToOff
    Gosub PlaySecondTrackWithRepeat
    Gosub FadeUpVolume
    Gosub WaitForDusk
    Gosub FadeDownVolume
    Gosub StopTrack
  Loop
Code:
SetVolumeToOff:
  cmd = MP3_SET_VOLUME : arg = 0 : Gosub Send ; Set volume to off (zero)
  Return

FadeUpVolume:
  For arg = 0 To MAX_VOLUME  
    cmd = MP3_SET_VOLUME : Gosub Send ; Set volume, 'arg' set by outer FOR-NEXT
    Pause 1000
  Next
  Return
I would like to be able to put the system to sleep during the night and I guess that I could do that by adding a couple of sleep commands after the LDR has a reading of <95.
That's one way though I would recommend not sleeping for so long just in case you get a 'false night' if a cloud passes over or some insect or dragon alights on the sensor. If that happens it won't recover and get back into sync for however many hours you are sleeping. Sleeping for minutes and checking the LDR should not drain the battery significantly more than sleeping for hours.

The second issue with putting the sleep in the 'WaitForDawn' routine is it will make it more difficult to implement any filtering out of 'false days' and 'false nights'. It would be better to integrate it in that mechanism but you'll have to wait for that episode to arrive.
 
Top