I am not sure how your code is designed to work; may be you just added an example to show the idea here.
I modified your code a little bit as below. May be it is similar to what you need.
Code:INCLUDE "MODEDEFS.BAS" OSCCON = %01100110 'Set for 4Mhz, but need to check the rest! TRISA = %00000000 'All outputs. TRISB = %00010100 'PORTB.2 'START BUTTON 'PORTB.4 'STOP BUTTON 'PORTB.0 'LED 'PORTB.1 'PIEZZO ADCON1 = %11111111 'All digital '===========Variables & Hardware assignment============= ALM_Flag var bit 'Alarm status LED VAR PORTB.0 'Pin for LED PIEZZO var PORTB.1 'Pin for Piezzo Start_BTN VAR PORTB.2 'Pin for Start Button Stop_BTN VAR PORTB.4 'Pin for Stop Button '==========Initial Settings============================= low LED LOW PIEZZO ALM_Flag = 0 Start: IF Start_BTN = 0 THEN WHILE Start_BTN = 0 : WEND 'Wait for finger release! ALM_Flag = 1 'Turns the alarm flag ON once. ENDIF IF Stop_BTN = 0 Then WHILE Stop_BTN = 0 : WEND 'Wait for finger release! ALM_Flag = 0 'Turns the alarm flag OFF once. ENDIF IF ALM_Flag = 1 then Sound PIEZZO, [1,255] 'Run Sound Command 'Depending on the piezzo type you have, you can just use HIGH and LOW commands. 'Example: HIGH PIEZZO 'This will run the piezzo until you LOW the Piezzo pin. HIGH LED 'LED indicates that Alarm is ON. Or do you want this to Flash? else LOW PIEZZO 'Stop sounding. LOW LED 'Alarm is OFF. ENDIF GoTo Start End
There are couple of things to pay attention.
1. Exactly when do you need to TOGGLE the LED?
2. Are buttons pressed by a user? If yes, "WHILE" will help you to wait for finger release. Check the example.
3. Check your piezzo type. You may just use HIGH and LOW commands to drive it. Thus, couple of lines will be good enough for your code.
-----------------------




Bookmarks