Write a program to play music on PC for KMK USB 12 Input Card with Microsoft Visual C++ 2008 Express.
We are going to write a program to play music on the PC according to the tact switches signal from the IO card. Please refer to KMK USB 12 Input Card for detail operations.
Since we are going to play 12 music according to the number of the tact switches pressed on the 12 Input Card. Please prepare 12 musics or songs and name them as 01,02....12 respectively.
Step 1 - 2 need to do once on your computer. Just skip steps 1 - 2 if you have done it already, .
Step 1 Install Visual C++ 2008 Express.
Step 2 USB Driver installation.
Step 3 Install KMK USB 12 Input OCX.
Step 4 Load KMK USB 12 Input OCX into C++.
Step 5
Make sure you have attached the IO card to the PC and the OCX is on the form. Then you should see the serial number of the card on the status window as below;
Step 6
We are going to write the code for automatically connecting the IO card while the program is started. Double click on the empty space of the form to enter into the code page. Type the code as below;
Step 7
We need a timer to scan the tact switches status from time to time. This process is known as 'Polling'. From the tool box choose 'Timer' and drag it to the form. Change the property Enable to True.
Step 8
We need to put Windows Media Player to our program for playing music. Right click the ToolBox and select Choose items.
Step 9
Select Windows Media Player control from the COM Component page.
Step 10
Now you should be able to find the Windows Media Player Control on the ToolBox. Drag it to the form.
Step 11
Arrange the location and size of the KMK USB OCX and Windows Media Play as below;
Step 12
Double click the Timer and type the code between the blankets of the timer1_Tick as below;
Step 13
Now Run the program by clicking 'Start Debugging' button on the tool bar or press F5 and press the tact switches on the board. The music should be started to play while you press the switches.
Note: You can connect NO(Normal Open) or NC(Normal Close) Switches such as Puch Buttons, Magnetic Switches, Reed Switches, etc to the IO Card. The above program is essential for creating computer games controlled with external hardware switches.
By the way, do you think you can open a movie instead of a music or song with the above program?