PDA

View Full Version : Questions on timing



malc-c
- 5th July 2006, 20:54
Hi all,

Thanks to everyone who helped so far with my re-working of the wheel project to make some form of disco lighting unit. I now have a revised idea and need some advice on the best way to go about it.

I have managed to make a bass beat extractor filter, which triggers an NE555 to provide a 5v logic pulse which is fed to an input on the 16F628a PIC, so when its high the pattern advances. ( A video best explaining this can be downloaded from http://www.micro-heli.co.uk/discolights2.avi )

Recently I had an option where a swich toggled the state of a variable and depending on the state of the variable the code would run the music routeen or simply chase at a speed dictated by a pot. However I would like to have the option of having the selection done automatically. The idea is that if the music input is low for a period of time in seconds (15, 20 40 what ever) the code runs the selected pattern in chase mode, then if the pin goes high due to the beat being detected, the code runs the sequence in step with the music. Obviously each time the pin goes high the timer has to be reset.

I've listed the body of the code with the comments where I think the checking needs to be done... but although I've glanced at the manual, I'm not sure what would be the ideal command to use, or how to impliment this, so help is required



;************* set up varibles ************

i var byte ;used for for next loops
D var byte ;used to store the result of the pot on port A1 and thus set timing delay
scale var byte ;used in the POT command
Scale = 254 ;used to set range
SW1 var PORTA.6 ;pattern cycle switch on RA6
mus var PORTA.2 ;music input pin A2
SWcount var byte ;used to count the button SW1 presses
steps VAR BYTE ;used to store the number of steps in the pattern sequence
counts VAR BYTE ;used in the FOR NEXT loop to run through the sequence

;************* main program ****************
counts = 0 ;set the value of counts to the start of each pattern
swcount=1 ;set default to pattern 1

music:
if sw1=0 then swcount=swcount+1 ;cycles through the patterns by adding 1 to SWcount
pause 70 ;debounce delay
If swcount >7 then swcount=1 ;error trap for exceeding max patterns
gosub sel1 ;go to subroutine to select pattern based on SWcount value
If mus = 1 then counts = counts+1
;****if mus = 0 then start timer. If timer > 30 seconds then goto chase ****
gosub sel2 ;go to subroutine to display pattern in current step
If counts >= steps then counts = 0 ;if counts then reset counts to 1
goto music:


;************* Subroutines *****************

Chase:
Pot PORTA.1,scale,D ;used to read value from 10k pot
pause 70 ;debounce delay
gosub sel1 ;go to subroutine to select pattern based on Swcount value
FOR counts = 1 TO steps ;advance to through the entries
gosub sel2 ;go to subroutine to advance through sequence
PAUSE D ;pause period set by varible D
if mus =1 then goto music
NEXT counts ;advance through loop to next position


Sel1:
if swcount = 1 then read Patt1, steps ;read the first value in data string patt1 and place it in steps
if swcount = 2 then read Patt2, steps
If swcount = 3 then read Patt3, steps
if swcount = 4 then read Patt4, steps
if swcount = 5 then read Patt5, steps
If swcount = 6 then read Patt6, steps
If swcount = 7 then Read patt7, steps
return

Sel2:
if swcount = 1 then READ (Patt1+counts), PORTB ;read the next value in patt1 and display it on PORTB
if swcount = 2 then READ (Patt2+counts), PORTB
if swcount = 3 then READ (Patt3+counts), PORTB
if swcount = 4 then READ (Patt4+counts), PORTB
if swcount = 5 then READ (Patt5+counts), PORTB
if swcount = 6 then READ (Patt6+counts), PORTB
if swcount = 7 then READ (Patt7+counts), PORTB
RETURN

malc-c
- 5th July 2006, 21:44
Amazing what a coffee can do !

I've got something working now. I created a variable called time and then changed the music section to



music:
if sw1=0 then swcount=swcount+1 ;cycles through the patterns by adding 1 to SWcount
pause 70 ;debounce delay
If swcount >7 then swcount=1 ;error trap for exceeding max patterns
gosub sel1 ;go to subroutine to select pattern based on SWcount value
If mus = 1 then counts = counts+1
if mus = 0 then time = time +1
gosub sel2 ;go to subroutine to display pattern in current step
If counts = steps then counts = 0 ;if counts then reset counts to 1
if time = 20 then goto chase
goto music:



works fine... just got to try the value for the jump to chase with some music (young one's asleep on the sofa at the moment.. so it will have to wait !)

Sorry for the waste of space on the server.. but I had been trying to get this working for hours before the cafine break !

Pic_User
- 5th July 2006, 22:54
Hi Malcolm,

Anytime, someone posts a question, it helps others with a similar problem.

Anytime, someone posts an answer, it helps everyone learn.

Just because, you posted the question AND the answer, doesn't mean it is not helpful.

(But, it did cause me to go get a cup of coffee...)

-Adam-

malc-c
- 5th July 2006, 23:21
Adam,

Thanks, for the comments, and I hope that someone somewhere might find my problem and answer useful ! I think it was the fact that I needed to walk away from the problem, have a caffine fix and then look at the problem afresh... seemed to work this time !

malc-c
- 6th July 2006, 17:42
Ok having run this in real time I have a problem. The variable time is byte, and so the maximum value I can have is 254. But setting the "if time = 254 then goto chase" still gives too short a period before the jump is made. I don't want to use a pause statement as that will cause the program to miss the odd beat whilst its waiting... What is the correct designation I should use for the varible, so I could increase the value to something like "if time = 1000 then goto chase" - is it var WORD ???

mister_e
- 6th July 2006, 22:38
yes it is.