Menu

Help with Code Please.

Help
Keith
2011-10-01
2013-05-30
  • Keith

    Keith - 2011-10-01

    Hi everyone, I'm looking for some help with this code loop. I have gone back to the original code given to me by Kent to see if I can iron out the problem myself (Fat Chance !)

    Do While Rx_Input On
    wait 12 us
    Count = Count + 1
    Loop
    If count >= 100 Then
    Set LED On
    delay_10mS
    Else
    Set LED OFF
    End If
    count = 0
    goto Main

    Sub delay_10mS
    Wait 10 ms
    End Sub

    What is happening is that the  it is bouncing on the switching point and what it needs is bandwidth between the Switch On  and switch Off.

    I have tried to include a second IF statement in place of the ELSE which went something like IF count < 90 then  Switch Off but with little success, 

    Ideally I feel this second IF should work giving my the desired bandwidth between 100 being On and 90 being Off but in reality it doesn't work.

    Once again any help would be really appreciated.

     
  • kent_twt4

    kent_twt4 - 2011-10-02

    The source is just "one" channel taken from a RC receiver, correct? (i.e. chan1 or chan2, etc.).

    The two IF statements should do it, not sure what is going on there.  Here is a proposal to try out, and should provide a more positive on/off output?  There is no reason to have a 10ms delay in the if statement?  The idea is to use the original IF/Else statement,  and instead using a digital out set LED off statement, use a digital input with a pull down resistor (10K).

    dir LED In
    Main:
    Do While Rx_Input On
        wait 12 us
        Count = Count + 1
    Loop
    If count >= 100 Then
        dir LED Out
        Set LED ON
    Else
        dir LED In  'Hi Z  LED pin, and pull to ground with use of 10k pull down resistor
    End If
    count = 0
    goto Main
    
     
  • Keith

    Keith - 2011-10-03

    Hi once again Kent, thank you for your input which is really appreciated. Firstly, I think the reason why it is troublesome using the two IF Statments is that the switching point is logically Off until the count of 100 or more is reached, if it is at 100 then drops back to 99 it will turn the switching point off. this is what I think is causing the Switch to 'jitter. Secondly, 10 mS second delay gives a more stable output as the Pulse duty cycle is increased to approximately 9:1  . Without it the LEd putput switches On and Off rapidly. I have not tested your solution as yet, but I will do when I get home tonight and get back to you. 

     
  • kent_twt4

    kent_twt4 - 2011-10-03

    Doh!  The count is getting cleared every time the pulse goes low on the input.  Let's try dumping the counter into a newcounter, to just save the valid high pulses.

    dir LED In
    count = 0
    Main:
    Do While Rx_Input On
        wait 12 us
        count = count + 1
    Loop
    If count > 10 Then newcount = count 'Ignore low periods/noise 
    count = 0
    If newcount >= 100 Then
        dir LED Out
        Set LED ON
    Else
        dir LED In  'Use 10k pulldown on this pin
    End If
    goto Main
    

    To add hysteresis, you would have two different set points, for the two IF statements.  This should work with the new counter method above, try this before the pulldown method.

    If newcount >= 100 Set LED On
    If newcount <= 85 Set LED Off
    
     
  • kent_twt4

    kent_twt4 - 2011-10-03

    Ha, my signature sloppy, forgetful  coding, where is the edit button :)?  Of course I meant:

    If newcount > 100 Then Set LED On
    If newcount < 85 Then Set LED Off
    
     
  • Keith

    Keith - 2011-10-03

    Hi again. That works a treat !

    By using the values of 88 as On and 85 as Off gives a switching point of 1,650uS and 1,580uS respectively. And, without the 10mS WAIT Statement in the equation !  EXCELLENT. Thanks once again.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.