The worst programmer ever to grace this forum - ME!


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Greg,

    Just had a chance to try out your suggestions - it works! (iepin 10 is now high...at this stage, I can't be sure which one of those tips you gave was the one that nailed it, becuase I used them all!)

    Some excellent ideas you've given there too about my main program ...in fact, I reckon I've enough sufficient info get this little program sorted now.

    Many thanks to all who took the time to help me out....the internet working at its finest.



    mackrackit...it was very late, & I'd just quickly googled my problem (as I always do when I have a problem & as a beginner, a search engine often serves me better as I can phrase a question when I'm not totally sure of what I'm asking!)...Google led me to that online PICbasic book & I took it at face value!

    Lesson learned.
    Last edited by HankMcSpank; - 16th March 2009 at 19:30.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Places to get help. for PBP.

    Micro Chip web site for data sheets, other places may not have the current one.
    Melabs web site, can not beat the source for examples.
    rentron.com - Bruce's web site. Very good examples.
    This forum, Darrel has a google search in the FAQ section.

    When you are ready for advanced stuff go to Darrel's web site.

    Any place else and who knows...
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    I've just realised that my little program which counts the number of time a switch has been closed (a magnetic switch, which is closed as 10 successive magnets glued to the perimeter of a DC fan motor pass it by!)...isn't counting correctly - & I don't know why (though I suspect it';s either down to the debounce paramters or perhaps the length of time taken to 'do stuff' in between successive magnets).

    here's my simple code (stripped down for clarity)...

    RB7pin10 VAR PortB.7 ;explicity name the switch pin for button command.
    RA3pin4 VAR PortA.4 ;explicity name the switch pin for button command.
    Temp VAR BYTE ; this is used for debounce/delay of the fan switch)
    total_counter VAR WORD ; this will count the total pulses rx'ed from magnets.
    pitch_counter VAR BYTE ; this will utlimately derive the pitch thread (a divider)
    direction_counter VAR word ; this will changed traversal direction word for counts bigger than 255
    pulse VAR PortC.0 ; stepper feed pin 16 (to pin 11 on stepper IC)
    direction var PortC.1 ;direction level pin 15 (to pin 14 on Stepper IC)


    stepper_enable VAR PortC.3 ; Pin 7 this logic level 0 enables the stepper IC (stepper IC Pin 15)

    Start:

    total_counter = 0 ; make sure we start from zero
    direction_counter = 0 ; make sure we start from zero
    direction = 1 ;we want the feed stepper to move left/right first.
    low stepper_enable ;enable the stepper chip.

    Main:
    Button RB7pin10, 0, 255, 0, Temp, 1, magnetic_switch_closed ; monitor the magnetic switch
    goto Main ;if switch not closed, loop until it does close.

    magnetic_switch_closed: ;ok, so switch is now closed - let's count!
    total_counter = total_counter +1 'increment counter to track number of magnets passing switch.
    IF total_counter = 500 THEN
    goto finish ' if using 10 magnets, after 50 turns of the motor exit the program
    endif
    pulse_the_stepper:
    pulsout Pulse, 20 ; pulse the stepper on Port C.0 with 20us pulse.
    Goto main ;return to start & monitor for the switch being closed again

    finish:
    HIGH stepper_enable ;disable the stepper chip
    End




    So to my question....

    Is there a more scientific way to approach this?

    The motor starts off slow, & then increases under manual control up to a speed somewhere between 150RPM & 350RPM. So the rate of magnets passing the switch can be anywhere between 0 per second & 58 passes per second (350/60 = 5.8 turns per second...there are 10 magnets)

  4. #4
    Join Date
    Apr 2009
    Location
    Boise, Id
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    I see a couple of things that make me wonder, your "Temp" Var should be set to 0 at the beggining before you call "Button", if not "Button" won't work correctly.

    Also, you are looking for a switch being closed at rates up to 58 times per second. 1/58 = 17 ms, so ever 17 milli seconds a magnet will sail past your switch, closing it for a moment, then it will re-open. If your magnets cover 50% of the circumference that would give you a 50% duty cycle. So the switch would be closed for 17/2 = 8.5 ms and open for 8.5 ms. In reality, your switch is probably closed about 10 % of the time or 1.7 ms.

    In your code :

    Code:
    Button RB7pin10, 0, 255, 0, Temp, 1, magnetic_switch_closed
    You have delay set at 255, this will perform debounce, but no auto repeat.

    Default debounce is 10 ms, so the switch needs to be closed for 10 ms before you go to "switch_closed"

    You might try a delay of 0, this shuts off the debounce.

    Also you can change the debounce delay:

    DEFINE BUTTON_PAUSE 1 'set button debouce to 1 ms

    1 ms is still cutting it close.

    Or you can skip the button all together and just read the port, something like:

    Code:
    If RB7pin10 = 0 Then switch_closed
    You can read the state of the switch again in the "switch_closed" code, if it's still closed continue, if not jump back out. That performes a high speed debounce. You can also use PAUSEUS before you double check, so you can wait a couple of micro seconds then double check the switch is really closed.

    Just some idea's.

    Shane

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Hank,
    Why not just use count statement, store count in a variable ?
    Looks like you want to find the end of each revolution and then increment the feed over so the wire does not overlap, yes?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    Apr 2009
    Location
    Boise, Id
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    just another thought on my last post. If you check the state of "magnetic_switch_closed" and jump into you're code where you increment your counter. Before you jump out and look for it being closed again, you should look for it to be open.

    If not then you could count the same swith close multiple times all for the same instance.

    something like:

    Code:
    if RB7pin10 = 1 then main ' make sure the switch opens before looking for another close
    Else stay in loop...
    Shane

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Firstly ....Shane/Joe, thank you for taking the trouble to respond (esp to a basket case of a kludger like me!)

    Ok, to your points...

    Shane you are right about the temp variablle needing to be set to zero...I didn't post up that bit (my program has a lot of variables going on, but I stripped it back to basics so as not to cloud the issue...but I can see it's caused probs!)

    Next....I think you've nailed the issue (my head was fogged last night after realising my program wasn't counting the revs right!)...essentially, my program traps the switch being closed (sort of!), but not the corresponding open. Therefore in reality, my program could see the switch being closed, head off to do some stuff elsewhere in the program, come back to check the switch & by bad luck/timing the next magnet could be passing so it thinks the same (original) magnet is closing the switch. therefore I need to wait for the switch to close, then open, then have my program act.

    Joe... re the counter/variable melarkey - remember, I'm very new to this. I've almost certainly done a tutorial somewhwere & simply applied what I learnt there to my program! (I haven't a lot of time to dedicate to being a slick programmer, so I just throw into the program the little bit that I know!). Could you be so kind as to outline the tighter code for that little increment you've honed in on as being a bit 'first lesson at PICBASIC evening class'!). BTW ...I use this magnet/switch 'count' to derive when to send a pulse to a stepper. This stepper I'm sending the pulse to, moves in synchronicity with the main motor, back & forward (the idea being to neatly feed coppoer wire on to the main turning motor). It's really cool on the odd (seemingly fluke) times I've had it working!

    Shane... the magnets are actually glued around the circumference of a wooden ring (& this ring itself is glued to the fan blades!). The diameter of this ring is about 10cm (4" if you're still in imperial) which makes for a circumference of 31cm ...the magnets themselves are about 1cm wide & there are 10 of them. So the magnets take up a third of the circumference - meaning a duty cycle of 33%?

    Therefore, at a maximum of 350RPM, the magnets will pass by the switch at about 58 times per second. This works out at every 17ms....because of the magnet width, this yields a duty cycle of 33% - therefore the switch will actually be closed for about 5.6ms, every 17ms

    Now my head still spins at the thought of building in the debounce aspect, so
    with these 'knowns' I have, which would be the best approach to ensure an accurate switch 'closing' count?


    (BTW, I'm thinking now that maybe an optical pickup would be a much better solution, as this removes the debounce aspect & I can have more resolution -I will look into this, but for now I'd really like to get the cheap 'n dirty solution working!)

Similar Threads

  1. Melabs U2 Programmer Command Line Options
    By Robert Wells in forum General
    Replies: 5
    Last Post: - 3rd July 2009, 03:11
  2. problems with USB programmer
    By malc-c in forum General
    Replies: 7
    Last Post: - 10th May 2007, 21:14
  3. USB programmer problems
    By uiucee2003 in forum USB
    Replies: 2
    Last Post: - 16th August 2006, 00:47
  4. General Programmer Questions
    By mslaney in forum General
    Replies: 1
    Last Post: - 17th December 2004, 19:16

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