Shop OBEX P1 Docs P2 Docs Learn Events
RCTIME problem jittering disco — Parallax Forums

RCTIME problem jittering disco

KidEKidE Posts: 29
edited 2011-12-05 11:08 in BASIC Stamp
Hi All,

Iḿ pretty new to embedded programming so please excuse me if i drop a stupid question here.

Iḿ working through the http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf manual and on page 151 im seeing some unexplained behaviour which is givving me trouble later on in the book.

The code is for measuring the rctime
time VAR Word
PAUSE 1000
DO
HIGH 7
PAUSE 100
RCTIME 7, 1, time
DEBUG HOME, "time = ", DEC5 time
LOOP


The Pot that is provided with the kit is a 10K and has a range for about 640.
When is set the pot to max the measurement displayed in the debug window is pretty steady but when i turn it down tp 640 it starts jittering enormously.
Its jumping up&down in values around 20+/-
I tried a different Pot but without any luck.

The result of this is that exercises later in the book become a complete menace vy turning (supposed to be steady) LED's into a locale disco.

Iḿ using the discovery USB board

Is this behaviour exacted or am i just doing something totally wrong here ?

Regards,

Ernst

Comments

  • ercoerco Posts: 20,256
    edited 2011-11-26 14:32
    First, try a different cap.

    BTW, it's probably jittery when you turn it down to the "zero resistance" side of the pot? That may be an inherent problem, too low a resistance. If so, figure out what the minimum stable resistance value is (measured with a multimeter) in ohms, and add a fixed resistor of that value in series with the pot.
  • KidEKidE Posts: 29
    edited 2011-11-26 14:52
    Ok well I tried 1K pot but I'll try a different resistor instead of the 220
  • $WMc%$WMc% Posts: 1,884
    edited 2011-11-26 15:14
    Try reducing your 2nd pause from 100 to 1
    '
    PAUSE 1
    '
    Take a look at this. The Pause times can effect the readings from the pot.It is time sensitive.
  • KidEKidE Posts: 29
    edited 2011-11-26 15:20
    Ok I'll also try that. Thx for the feedback. But is 1Ms not a little short for charging?
  • $WMc%$WMc% Posts: 1,884
    edited 2011-11-26 15:38
    Not at all...
  • $WMc%$WMc% Posts: 1,884
    edited 2011-11-26 15:49
    Take a look at the StampWorks manuel....Look at the RCTIME examples
    '
    http://www.parallax.com/Portals/0/Downloads/docs/books/sw/Web-SW-v2.1.pdf
    '
    http://www.parallax.com/Portals/0/Downloads/docs/prod/stamps/web-BSM-v2.2.pdf
    '
    The 2nd link is the BASICStampManuel...It has some good info on RCTIME too.
  • KidEKidE Posts: 29
    edited 2011-11-27 09:18
    ok i tried your script and it produces exactly the same as the previous one.

    00001
    00001
    00001
    00001
    00001
    00001
    00001
    00001
    00022
    00069
    00086
    00117
    00168
    00188
    00219
    00243
    00299
    00327
    00343
    00385
    00437
    00544
    00570
    00616
    00630
    00632
    00627
    00626
    00633
    00631
    00632
    00627
    00626
    00632
    00631
    00633
    00628
    00626
    00631
    00631
    00633
    00628
    00626
    00631
    00630
    00634
    00628
    00626
    00635
    00631
    00632
    00628
    00630
    00634
    00633
    00629
    00616
    00607

    you see at the end the value sometimes differs +/- 30. also tried different caps but with no luck. how could this be?
  • $WMc%$WMc% Posts: 1,884
    edited 2011-11-27 09:55
    Does your circuit look like this?
    1024 x 754 - 26K
  • KidEKidE Posts: 29
    edited 2011-11-27 10:50
    you have to help me a little here but i think yes

    Selection_046.png
    338 x 390 - 28K
  • KidEKidE Posts: 29
    edited 2011-11-27 11:15
    to be sure i rebuild your circuit and the values i found were:

    00603
    00605
    00610
    00613
    00615
    00616
    00619
    00621
    00622
    00627
    00629
    00630
    00633
    00634
    00635

    I did a sort -nu over the output to show the increment.

    i changed the 0,1uF for a 0.01uF and the output varied from 0 to 60 where at 54 the jittering starts

    swapping the 10K pot for a 25K gave the following

    01356
    01361
    01373
    01378
    01393
    01399
    01400
    01404
    01408
    01413
    01417
    01419
    01420
    01421
    01422
    01424
    01425
    01426
    01427
    01428
    01429
    01431
    01432
    01433
    01434
    01435
    01436
    01437
    01438
    01439
    01440
    01441
    01442
    01444
  • $WMc%$WMc% Posts: 1,884
    edited 2011-11-27 15:11
    Have you tried putting the Cap on the I/O pin side instead of through the 220ohm resister? (See the circuit I posted above)
    '
    Remember its the I/O pin that charges up the Cap The resister is reducing the charge to the Cap in your circuit. They should be slowly discharging the Cap only.
    '
    Another thing to remember is the Cap does not drain down to zero volts for the RCTIME command. It drains down to the threshold voltage. This voltage is around 2.5volts. So the window of time to measure is pretty tight for the RCTIME command.
  • KidEKidE Posts: 29
    edited 2011-12-01 13:35
    sorry guys it took a while but i have found the solution/cause for/of my problem.

    It seemed i had a faulty jumper wire which was causing all the problems :innocent:

    In the reference manual i found the part regarding RCTIME and i build the following:


    Selection_047.png


    with code:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    time VAR Word
    
    PAUSE 1000
    
    DO
    
      HIGH 2
      PAUSE 1
      RCTIME 2, 1, time
      DEBUG CR, DEC3 time
      PAUSE 10
    LOOP
    
    

    I found that the first "PAUSE 1" makes a major difference in the output and stability of the RCTIME value but i don't yet understand why.
    Is there a good explanation for this? I myself thought that it had to do with the HIGH 2 command which is executed just before.

    Is this assumption correct?
    276 x 130 - 4K
  • KidEKidE Posts: 29
    edited 2011-12-01 14:24
    $WMc% wrote: »
    This voltage is around 2.5volts. So the window of time to measure is pretty tight for the RCTIME command.

    Shouldn't this be 1.4V ? at least thats what i'm reading in the manual or are you talking about another threshold?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-12-01 16:50
    The PAUSE 1 is important because it allows time for the capacitor to charge up to a high level from the Stamp pin as output, and then when the RCTIME command executes, the capacitor discharges through the variable resistor as the voltage is sensed by the same pin as input. The pin input threshold that separates reading high from reading low is close to 1.4 volts. The capacitor in your program continues to discharge during the ensuing DEBUG and the 10 millisecond PAUSE. Then at the top of the loop it repeats by charging up the capacitor high through the pin again as an output.

    Another caveat is that the 220 ohm resistor forms a voltage divider with the variable resistor, so for best results the value of the variable resistor should stay >> 220Ω.
    DO
      HIGH 2
      PAUSE 1
      RCTIME 2, 1, time
      DEBUG CR, DEC3 time
      PAUSE 10
    LOOP
    

    You can rearrange that a little to allow more time for C to charge up to the high level:
    DO
      HIGH 2
      PAUSE 10
      RCTIME 2, 1, time
      DEBUG CR, DEC3 time
    LOOP
    
  • KidEKidE Posts: 29
    edited 2011-12-01 17:10
    Ok so i modified my code as you suggested and the outcome looks like:

    643
    645
    654
    655
    660
    663
    664
    665
    666
    667
    668
    669
    671

    This is when the POT is turned to the max left. The difference is about 28. If i do this with my code the difference is max 2

    So i see your point about the flow but how do i get a stable value that i can use as input for steering while upholding your suggestion and giving the capacitor enough time to fully load?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-12-01 21:36
    Realistically, the 1ms should be enough delay. The time constant of a 0.1µF capacitor with 220Ω is a mere 22 microseconds. The expected RCTIME with 0.1µF and 10kΩ is, 1.347 * R * C / 2 = 1347 µs / 2 = 673.5, so the readings you are seeing are almost spot on for what is expected!

    At the end those readings are quite steady, varying only by 3. The variation of 28 there is only at the beginning of the sequence. I don't know what that means, because I'd have to make too many guesses about the setup and the procedure. I noticed that same trend in some of your other posted results too. They settle down to rather steady values at the end and the big variation is at the beginning. Maybe that is because you were turning the pot, or it was still settling into place, and there are other effects that can happen in the capacitor or the Stamp. I think you are better off with a larger resistance pot, the 25k, or 100k.

    Another thing you have to watch out for with RC time is the length of wires. For example, the wire from the pot and capacitor over to Vss should be pretty short, not a big fat loop. Big loops of wire can pick up lots of external noise and noise produced by digital switching in the BASIC Stamp chip itself. The same is true for the other wires. Also the contacts should be nice and tight. Sometimes the contacts in a BOE become so loose that they do not make good contact. The best results with RCtime always come from really tight wiring practice. If you want more to read, I have an artice about RCTIME at this URL.

    There will always be some jitter, but once you have it down to a reasonable level, more can be removed with a simple software lowpass filter.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-12-02 23:57
    KidE
    '
    Glad to here you got it working!
    '
    I said 2.5 volts...I meant around 1.5 volts sorry.
    '
    My point was it was not zero volts.
  • KidEKidE Posts: 29
    edited 2011-12-03 04:45
    once you have it down to a reasonable level, more can be removed with a simple software lowpass filter.

    I indeed noticed that unequal length of wire can cause strange things here.

    But please enlighten me about this low pass filter. How would you do this in this case?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-12-03 13:45
    A low pass smoothing filer has an accumulator that changes gradually as it is continually averaged with each new sample that comes in. Here is a simple example, where each new sample contributes 1/2 to the accumulation of old values:
    time VAR word
    accumulator VAR word
      HIGH 2
    DO
      PAUSE 2
      RCTIME 2, 1, time
      HIGH 2
      accumulator = (accumulator + time)/2
      DEBUG CR, DEC time, tab, DEC accumulator 
    LOOP
    
  • KidEKidE Posts: 29
    edited 2011-12-05 00:46
    Wow... how things can be that simple.. the accumulator works like a charm and i got steady values now for steering.

    But why call this a low-pass filter and not high-pass?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-12-05 11:08
    It is called low-pass because it lets thru only the slow (low frequency) changes, and smooths out the jitter (high frequency) that jumps from reading to reading.
Sign In or Register to comment.