Rc lights with indicators

Thread Starter

barneydog

Joined Dec 22, 2009
16
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
 

tom66

Joined May 9, 2009
2,595
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.
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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.
 

tom66

Joined May 9, 2009
2,595
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.
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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...
 

tom66

Joined May 9, 2009
2,595
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.
 

tom66

Joined May 9, 2009
2,595
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.
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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...
 

tom66

Joined May 9, 2009
2,595
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.)
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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
 

tom66

Joined May 9, 2009
2,595
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:

Rich (BB code):
__                 __
  |               |  |
  |               |  |
  |               |  |
  |_______________|  |_______________
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):


Rich (BB code):
__                 __
  |               |  |
  |               |  |
  |               |  |
  |_______________|  |_______________
1  2  3    4    5  67  8   9 10 11 12
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?
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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
 

tom66

Joined May 9, 2009
2,595
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.
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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.
 

Thread Starter

barneydog

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

'Scania Truck Indicators / With Hazrds
'V1.0
symbol A1 = b0


main:
check_signal: pulsin 3,1,A1
if pin1 = 1 then check_signal7
if A1 <= 64 then leftflash 'Compare command
if A1 >= 164 then rightflash 'Compare command
'Decision command
goto check_signal
check_signal7:
high 2
high 4
pause 1000 'Wait command
low 2
low 4
goto check_signal
rightflash: low 4
pause 1000 'Wait command
high 4
goto check_signal
leftflash: high 2
pause 1000 'Wait command
low 2
goto check_signal
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:

tom66

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

Rich (BB code):
Def A = 0
Def B = 0

GatherSample:
   If Pin1 == 1:
      A += 1
      B += 1
   Else:
      B += 1

FlashLeft:
   Turn Left Indicator On
   Wait 100 ms
   Turn Left Indicator Off
   Wait 100 ms
   Done

FlashRight:
   Turn Right Indicator On
   Wait 100 ms
   Turn Right Indicator Off
   Wait 100 ms
   Done

Main:
   Loop Forever:
      If B < 255:
         Call GatherSample()
      Else:
         If A <= 84: Call FlashLeft
         If A >= 164: Call FlashRight
         A = 0
         B = 0
 

Thread Starter

barneydog

Joined Dec 22, 2009
16
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.
 
Top