VB4 question

Electronics Computer Programming Q&A
Post Reply
User avatar
CeaSaR
Posts: 1949
Joined: Sat Nov 08, 2003 1:01 am
Location: Phoenixville, PA USA
Contact:

VB4 question

Post by CeaSaR »

I am trying make a clock that chimes on the hour and so far, I have
everything working except:

In the For Next loop, it wont stop. It just keeps on going. Here's the code
(keep in mind this is just the experimental code and it will be cleaned up
when everything is working correctly):

Code: Select all

Private Sub Timer2_Timer()
    Let test1 = Format(Time, "hh")
    Let test2 = test1 * 10000
    Label1.Caption = test2
    Let test3 = Format(Time, "hhmmss")
    Label2.Caption = test3
    Let test4 = test3 - test2
    label3.Caption = test4
    If test3 - test2 = 0 Then label4.Caption = "NOW!" Else label4.Caption = "Not yet..."
    If test3 - test2 = 0 Then Label5.Caption = "NOW!"
    If test1 - 12 <= 0 Then Label5.Caption = test1 Else Label5.Caption = test1 - 12
    If test3 - test2 = 0 Then GoTo cuckoo

cuckoo:
    If test1 > 12 Then test5 = test1 - 12 Else test5 = test1
        For I = 1 To test5 Step 1
            PlaySound "C:\cuckoo.wav", ByVal 0&, SND_FILENAME Or SND_ASYNC
        Next I

End Sub
Once I get into the GOTO "cuckoo" area, it keeps playing the Wav file
endlessly. How do I get it to exit this For Next loop? The on-line help
doesn't specify how.

Thanks for your help
CeaSaR
Hey, what do I know?
dyarker
Posts: 1917
Joined: Fri Aug 22, 2003 1:01 am
Location: Izmir, Turkiye; from Rochester, NY
Contact:

Post by dyarker »

In FOR/NEXT loop try:
PlaySound "C:\cuckoo.wav", ByVal 0&, SND_FILENAME Or SND_SYNC

SND_ASYNC requires a second call to PlaySound to stop.

Cheers,
Dale Y
User avatar
CeaSaR
Posts: 1949
Joined: Sat Nov 08, 2003 1:01 am
Location: Phoenixville, PA USA
Contact:

Post by CeaSaR »

Actually, it stops when not in a loop. I tried your suggestion, but it still
doesn't work. I think I'll go with a Case Select option. I'll see how it
comes out.

Thanks,

CeaSaR

I think I found the problem, the For/Next loop is looking for actual
numbers in the counter and I am trying to use a variable (Dim'd as long).
I'll play with that too...
Hey, what do I know?
dyarker
Posts: 1917
Joined: Fri Aug 22, 2003 1:01 am
Location: Izmir, Turkiye; from Rochester, NY
Contact:

Post by dyarker »

I think I found the problem, the For/Next loop is looking for actual
numbers in the counter and I am trying to use a variable (Dim'd as long).
That would be wierd. If true it's another reason I don't use VB.
Dale Y
reloadron
Posts: 519
Joined: Sat Jun 28, 2008 8:57 am
Location: Cleveland, Ohio
Contact:

Post by reloadron »

If you are still chasing this I came up with something that seems to work. I did it in VB6 simply because I have VB6. I doubt for this there is much difference in the code. I am not a programmer type, far from it but occasionally I have to wite a little code for data acquisition and mess with Visual Basic.

Here is what I came up with:

Code: Select all

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Const SND_SYNC = &H0

Private Sub Command1_Click()
Form1.WindowState = 1  'Minimize form
Timer1.Enabled = True
Call sndPlaySound("F:\Windows\Media\tada.wav", SND_SYNC) 'Start with the sound file to verify it
End Sub

Private Sub Command2_Click()
    End 'End the program
End Sub

Private Sub Timer1_Timer()

Text1.Text = Time 'Show time in hh:mm:ss
Text2.Text = Format(Time, "Nn") 'Show only the Minuits
Text3.Text = Format(Time, "ss") 'Show only the Seconds

If Text2.Text = 0 And Text3.Text = 0 Then 'When Min and Sec equal zero on the hour
    Option1 = True
    Else: Option1 = False
End If

If Option1 = True Then
   Call sndPlaySound("F:\Windows\Media\tada.wav", SND_ASYNC) 'Play the sound
End If

End Sub
The Declaration is Private because if it were Public it would need to be in a seperate module.

Command1 Click event does a few things, it minimizes the Windows form, Starts Timer1 and it calls the wave file to play as a test at start.

I used 3 text boxes which really aren't needed but they show what is going on. Text1 is formatted to display the current system time, Text2 shows only the min and Text3 shows only the seconds.

On the hour the min and seconds will be 00:00 so I shoved an Option button in there as Option1. Option1 will only be true when the min and sec equal 00:00. That True state is only 1 second.

When Option1 equal True we call the wave file to play. By the time the file has completed a play Option1 is False. Since we have Private Const SND_SYNC = &H0 the file will play to completion.

Command2 will simply end the program.

You may want to try using the option button and set its properties accordingly so there is no user intervention. Anyway, this seems to work. Crude but it works.

Ron
reloadron
Posts: 519
Joined: Sat Jun 28, 2008 8:57 am
Location: Cleveland, Ohio
Contact:

Post by reloadron »

Actually looking at this fresh in the morning your code works great. I just don't understand why the loop?

When you get to this point:

Code: Select all

If test3 - test2 = 0 Then GoTo cuckoo
You have the condition you want, you have the zero. You could just use your If statement like this:

Code: Select all

If test3 - test2 = 0 Then
    Call sndPlaySound("F:\Windows\Media\tada.wav", SND_SYNC)
End If
That seems to work well. The condition of zero will only last for a second and will fire the PlaySound. I "think" looking at your loop:

Code: Select all

cuckoo: 
    If test1 > 12 Then test5 = test1 - 12 Else test5 = test1 
        For I = 1 To test5 Step 1 
            PlaySound "C:\cuckoo.wav", ByVal 0&, SND_FILENAME Or SND_ASYNC 
        Next I 


The sound will play for an hour. The condition for the If will be true for an hour. Then it will just be true again and play for an hour.

Ron
dyarker
Posts: 1917
Joined: Fri Aug 22, 2003 1:01 am
Location: Izmir, Turkiye; from Rochester, NY
Contact:

Post by dyarker »

After playing the number of times in the FOR/NEXT (1 to 12) the code goes to END SUB.

If other code not shown immediately calls Sub Timer2_Timer() again, then cuckoo will play continuously.

reloadron - The reason for the loop is to play cuckoo once at 1 o'clock, twice at 2 o'clock, etc.
Dale Y
reloadron
Posts: 519
Joined: Sat Jun 28, 2008 8:57 am
Location: Cleveland, Ohio
Contact:

Post by reloadron »

dyarker wrote:After playing the number of times in the FOR/NEXT (1 to 12) the code goes to END SUB.

If other code not shown immediately calls Sub Timer2_Timer() again, then cuckoo will play continuously.

reloadron - The reason for the loop is to play cuckoo once at 1 o'clock, twice at 2 o'clock, etc.
Thank you Dale. I had it really screwed up in that I thought the idea was simply to have it chime on the hour. I didn't get the once at 1 o'clock, twice at 2 o'clock and so on. I sure screwed that up!

Ron
dyarker
Posts: 1917
Joined: Fri Aug 22, 2003 1:01 am
Location: Izmir, Turkiye; from Rochester, NY
Contact:

Post by dyarker »

CeaSaR,

Try this-
is incomplete pseudo code because I don't know VB syntax.
as is this code will run forever, but only play the right number of Cuckoos on the hour.
To exit use Task Manager, a better way to exit can be added after testing.

Code: Select all

  local CurHour as long rem Current Hour
  local PrevHour as long rem Hour that cuckoo already played for
  local Hour1to12 as long
  local i as long
  do
    CurHour = [whatever function in VB gives hour in binary]
    if CurHour = PrevHour then
      sleep 1000 rem do nothing for 1 second
      iterate do
    else
      if CurHour > 12 then
        Hour1to12 = CurHour - 12
      elseif CurHour = 0 then rem catch midnight
        Hour1to12 = 12
      else
        Hour1to12 = CurHour
      end if
      for i = 1 to Hour1to12 Step 1 rem ??in PowerBASIC default step is 1
        PlaySound "C:\cuckoo.wav", ByVal 0&, SND_FILENAME Or SND_SYNC
        sleep 250 rem 1/4 second silence between cuckoos
      next i      
      PrevHour = CurHour rem prevent replay till next hour
    end if
  loop
(When first started, will cuckoo regardless of minute and second, after that will cuckoo at 0 minutes, 0 seconds)

Using a Windows' API "Waitable Timer Objects" would be a slicker way to do this than sleep/test in a do loop. But for Waitable Timer Objects I can't write code from memory.

good luck,
Dale Y
reloadron
Posts: 519
Joined: Sat Jun 28, 2008 8:57 am
Location: Cleveland, Ohio
Contact:

Post by reloadron »

OK undaunted by my inabilities to code I came up with the following:

Code: Select all

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Const SND_SYNC = &H0

Private Sub Timer1_Timer()
Let test1 = Format(Time, "hh")
Let test2 = test1 * 10000
Let test3 = Format(Time, "hhmmss")
Let test4 = test3 - test2

If test1 > 12 Then
test5 = test1 - 12
ElseIf test1 < 12 Then
test5 = test1
End If

Text1.Text = Format(Time, "hh")
Text2.Text = (Text1.Text) * 10000
Text3.Text = Format(Time, "hh:mm:ss")
Text4.Text = test3 - test2
Text5.Text = test5

If Text4.Text = 0 Then
Dim intLoopIndex, Total
Total = 0
For intLoopIndex = 1 To test5
    Total = Total + 1
    Call sndPlaySound("F:\Windows\Media\tada.wav", SND_ASYNC) 'Play the sound
Next intLoopIndex
    
End If
End Sub
This, to my surprise actually works in VB6. This actually plays the right number of wave files on the hour.

Again, many thanks to dyarker for pointing out my initial errors as to what the original poster was looking to do. The For loop now has the needed internal Loop Index.

Working from this poor example (that actually works) things should fall into place.

Ron
User avatar
CeaSaR
Posts: 1949
Joined: Sat Nov 08, 2003 1:01 am
Location: Phoenixville, PA USA
Contact:

Post by CeaSaR »

Gee, you don't read the forum for a few days and look what happens! :grin:
Thanks a lot guys. Yes Ron, the For/Next was/is to play a single .WAV file
the correct # of times in the same way as a standard cuckoo clock does.
The file I recorded sounds exactly like a standard cuckoo clock "cuckooing"
when strung together. I wanted to use an external sound file in order to
allow the user to change it should he/she wish. Just imagine the fun you
could have putting random sound bites in there: "Laugh it up, fuzzball" or
"Make it so" or even "I'm sorry, did I break your concentration?". Even
Big Ben's bell (or Quasimodo's for that matter! :razz: ) would work. It was
all a matter of getting the "loop" to work.

Again, thanks for all the gracious help. I'll get back to it sometime this
week and let you know how it worked out in VB4 ('cause that's what I
have, and I don't want to shell out the bucks for VB.Net :shock: )

CeaSaR
Hey, what do I know?
User avatar
CeaSaR
Posts: 1949
Joined: Sat Nov 08, 2003 1:01 am
Location: Phoenixville, PA USA
Contact:

Post by CeaSaR »

Ron,

Yes! It definitely works in VB4. Just needed to add one more IF/THEN
statement:

IF test1 = 0 THEN test5 = 12

This allows the clock to chime when it is 12 midnight (0:00:00). Now to
move the main loop out of the timer as it stalls the clock until it finishes.
Of course, it could remain there if the time is set to HH:MM, that way you
wouldn't see it stall, as long as all 12 "chimes" total less than 60 seconds.

A few more "bells and whistles and it'll be ready. Thanks for the help,
you'll both get credit in the ABOUT screen.

CeaSaR
Hey, what do I know?
reloadron
Posts: 519
Joined: Sat Jun 28, 2008 8:57 am
Location: Cleveland, Ohio
Contact:

Post by reloadron »

Glad it worked with the last If. I never caught that but looking at the code it wouldn't chime at 12 without that If in there. Adding a few more features would be nice.

On another note, Microsoft offers Visual Basic.NET in a free express edition:

http://www.microsoft.com/express/vb/

Matter they offer everything in the full VisualStudio.NET in "Express" versions.

Ron
User avatar
CeaSaR
Posts: 1949
Joined: Sat Nov 08, 2003 1:01 am
Location: Phoenixville, PA USA
Contact:

Re: VB4 question

Post by CeaSaR »

As an update, I have uploaded the cuckoo clock, called JustaClock, to my site.
You can navigate through my profile (below) or use this direct >link.< When you
unzip the file, read the Readme.txt file as it contains the instructions to "install"
the software (placement of 1 file). I chose this route so that users would know
exactly where everything is located should they wish to not use the program.

Enjoy.

CeaSaR

EDIT:
For those who have trouble seeing the links inside my post, try these below.

Homepage:
http://www.geocities.com/bergermeister1st/index.htm

Software page:
http://www.geocities.com/ceasarsoftware/index.html

Due to GeoCities closing later this year, I am seaching for a new host. I will update
the links when I move my site(s). Until then, use the above links.
Hey, what do I know?
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests