Rc lights with indicators

Discussion in 'General Electronics Chat' started by barneydog, Aug 19, 2010.

  1. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    Hi,

    what im trying to achieve is to make a radio controlled set of direction indicators...

    when steering right 2 leds flash
    when steering left 2 different leds flash.

    I was originally looking at using the 10f206, because of its size and i have some lurking around. i know its more than capable of preforming the tasks needed. using more than one pic isnt a problem, but want to keep them small.

    The receiever already in the remote control truck uses PPM, so i need it to decode that signal. on the servo cable you have 3 wires +, -, and signal.

    the signal goes into the input of the pic, and when the stick on the controller is pushed over 30% the indicators flash. this is what i have so far..

    Input - Comes from receiver channel 2 steering. Determines if steering left or right.

    If steering right - Output 1 goes high and flashes the leds.

    If steering left - Output 2 goes high and flashes the leds.

    If steering centered - no output sent.

    I would like warning/hazard lights on here, but that can wait till i can resolve this.

    I have very limited PIC experience and if anyone can help me, or show me how to do this i would appreciate it. I could just go and buy one, but then i wont get very far learning pic.

    I have been looking all over the net to try and work this out and hit a brick wall.

    Thanks for any help in advance
     
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    You need to measure the duty cycle (sometimes known as mark/space time) of your servo signal. One easy way is to take a number of random samples; if the servo signal is high, increment counter A and B; if it's low, increment B only. Then your duty cycle is A/B as a fraction or (A*100)/B as a percentage from 0 to 100; you could even go from 0 to 255 to make it easier. One easy way to do the division is to count only a power of two of samples, say 256 (B=255), and then shift right 8 times, which is equivalent to a divide by 256 unsigned. You *may* need to work with 16-bit numbers. Remember, your samples must be spaced sufficiently far apart (approximately 10-20 samples per cycle) for this to work.
     
  3. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,770
    970
    Small mechanical microswitches would be the simplest solution..
     
  4. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    thanks tom66...

    do you have any samples of how to do this or examples i can use to base mine on.

    dont get me wrong i want to write my own code, but im very new. only managed to make led chaser so far.
     
  5. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    Unfortunately I have no code examples for this. It's just the most common and easiest solution to measuring duty cycle especially on a processing power limited PIC. Other solutions involve averaging the signal to get a voltage. It should be a fairly trivial example to code. You didn't mention the programming language either.
     
  6. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    only thing i have available is mplabs ide v8.36

    unless you can tell me of some others...

    My programming experience before this is the following

    Pascal - College
    Cobol - College
    MYsql - Work
    PHP - Work
    HTML - Work


    if this problem was in php, be so simple to solve...
     
  7. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    You can program the PICs, and most microcontrollers in two languages: C and Assembly. BASIC language is also available and probably would work well for this.
     
  8. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    C or basic wouldnt be to bad...

    what clients are available to do this in ?
     
  9. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    HI-TECH C is popular for lower end PICs. HI-TECH is now owned by Microchip. There is a free version available (which should be all you need.) You can use it with MPLAB, which is free as well. If you prefer to code in BASIC there are the PICAXE chips, which are specially programmed PIC chips designed to run BASIC.
     
  10. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    ok thank you... i think i have that installed already, came on a cd with the other software...

    i have been hunting around the net looking for what you mentioned above, and really coming up at a loss..

    either its me not understanding what others are saying or i cant work google...

    if you or anyone else could help me with this code would be amazing...
     
  11. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    I can help you out but I cannot write all the code for you. Otherwise, nobody would learn anything.

    If you got a PICkit 2 or 3, it probably already came with MPLAB and a free copy of HI-TECH C (which is also available as a download from the Microchip website.)
     
  12. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    i have pickit2 and a different one off ebay (still usb, and works same as pickit2)

    when the stick is central i have found out it pulses at 1.5ms and changes upto 2ms when moved either direction. What i dont understand here is how it knows the direction its been moved, except on a voltmeter it drops by just over 0.1 of a volt.

    so i need to be able to measure the pulses and then get the pic to determine the direction so for example..

    A (left steer) B (right steer)

    A = 0% - 100% - 0% being central, 100% being full left.
    B = 0% - -100% - 0% being central, -100% being full right.

    so the pic detects the % of movement and then determines if its % or -%

    if it gets % output 1 flashes the 2 left leds
    if it gets -% output 2 flashes the 2 right leds

    i understand what i have to do, and how i need to do it... what i seriously lack is the coding ability to do this outside of php/html....

    if i where to write this in php i would use the following method to make life clean and easy, keeping code to minimum. not done full code as would take to long and is a pointless exercise.

    function(sub routine) rightled {
    flash led_r at 1 second intervals
    }
    function(sub routine) leftled {
    flash led_l at 1 second intervals
    }

    <?

    $input = $_POST[input];

    if ($input < -33%) {
    rightled;
    }
    elseif ($input > 33%) {
    leftled;
    }

    obviously php can only repeat commands on a refresh or button press so it would need some ajax to work without user input or forced refreshes.

    So i do understand the principals, just lack the skills and knowledge to make it into a pic program version
     
  13. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    What you are observing is duty cycle.

    If the stick is at 10% position (assume this is 10% from left in this example) the waveform is like this:

    Code ( (Unknown Language)):
    1. [FONT=Courier New]
    2. __                 __
    3.   |               |  |
    4.   |               |  |
    5.   |               |  |
    6.   |_______________|  |_______________
    7. [/FONT]
    Notice it is on for about 10% of the time.

    The method I posted works by pseudo-randomly sampling the waveform, so you get something like this (numbers/letters indicate samples):


    Code ( (Unknown Language)):
    1. [FONT=Courier New]
    2. __                 __
    3.   |               |  |
    4.   |               |  |
    5.   |               |  |
    6.   |_______________|  |_______________
    7. 1  2  3    4    5  67  8   9 10 11 12[/FONT]
    8.  
    If you count the pulses high, you'll count three out of twelve. This is about 25%, so is a little off, but the more sampling you do the closer you get to your actual number (law of averages/law of large numbers.)

    This is a common technique for measuring duty cycle and I think it's one of the few that works reliably. The problem is, to gather a high resolution it takes time, a power of two more for every bit you add.

    Then your logic simply takes these counts and handles them appropriately, for example indicating left in this case because it's less than about 33%.

    Do you understand?
     
  14. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    so if i have this correct...

    6 out of 12 will be 50%

    so to achieve the 33% i would need 4 out of 12.. (roughly)

    now if i went with 6 out of 12, which is going to be easier to find, when the stick hits 50% i will get the 6 out of 12 i need to turn the indicators on.

    hope im following you correctly
     
  15. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    Yeah, but you'd want to take more samples, like 100 or so, before you could really get a resolution of a few percent. It's unnecessary to do a divide if you scale all your numbers. For example 33% of 256 is 84 rounded down; any number less than or equal to this could be considered left, 66% of 256 is 168 rounded down; any number greater than or equal to this could be considered right; and any number not fitting would be centre position.
     
  16. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    right... now let me check im still following

    if input <= 84 turn left
    if input => 168 turn right

    if input is > 84 and < 168 we are central so do nothing.
     
  17. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    i had a look at picaxe, like you suggested right at the start... and i have come up with this. does it look right?

    have only given the indicators 1 flash and then the program cycles again. If input 1 is high then it sends high to outputs 2 and 4 giving me hazard lights. which at present i dont know what will trigger that, but i think have to be controller side.
     
    Last edited: Aug 20, 2010
  18. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    I don't write PICAXE BASIC, but here's a pseudocode implementation:

    Code ( (Unknown Language)):
    1.  
    2. Def A = 0
    3. Def B = 0
    4.  
    5. GatherSample:
    6.    If Pin1 == 1:
    7.       A += 1
    8.       B += 1
    9.    Else:
    10.       B += 1
    11.  
    12. FlashLeft:
    13.    Turn Left Indicator On
    14.    Wait 100 ms
    15.    Turn Left Indicator Off
    16.    Wait 100 ms
    17.    Done
    18.  
    19. FlashRight:
    20.    Turn Right Indicator On
    21.    Wait 100 ms
    22.    Turn Right Indicator Off
    23.    Wait 100 ms
    24.    Done
    25.  
    26. Main:
    27.    Loop Forever:
    28.       If B < 255:
    29.          Call GatherSample()
    30.       Else:
    31.          If A <= 84: Call FlashLeft
    32.          If A >= 164: Call FlashRight
    33.          A = 0
    34.          B = 0
    35.  
     
  19. barneydog

    Thread Starter New Member

    Dec 22, 2009
    16
    0
    ok that closely matches what i have already.

    It will just need some testing once i have the picaxe kit, to correct the values if needed to.

    I will give this a try and see how it goes.. will let you know...

    Then once i can start understanding the basic code more, i can try other different variations of basic, possibly learning assembly in the future...

    Thank you for all your help, would never have got this far without it.
     
Loading...