Questions on timing


Closed Thread
Results 1 to 6 of 6
  1. #1
    malc-c's Avatar
    malc-c Guest

    Default Questions on timing

    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

    Code:
    ;************* 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

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
    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 !

  3. #3
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298


    Did you find this post helpful? Yes | No

    Smile Both sides

    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-
    Ohm it's not just a good idea... it's the LAW !

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    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 !

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Bits, bytes and bobs !

    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 ???

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    yes it is.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. General USB questions
    By BobEdge in forum USB
    Replies: 2
    Last Post: - 21st April 2009, 04:19
  2. 18F2550 timing
    By sstt1976 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 22nd August 2008, 00:30
  3. Two quick (and elementary) questions.
    By scorpion990 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th June 2008, 23:03
  4. A few 12F683 questions
    By dhouston in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th May 2008, 03:54
  5. timing questions with different oscillators
    By glkosec in forum General
    Replies: 1
    Last Post: - 22nd August 2007, 05:42

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts