​ ​ ​ ​ Optoswitch as rev counter - Page 3
Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 33

Thread: Optoswitch as rev counter

  1. #21
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,326

    Default

    Quote Originally Posted by joliebrise View Post
    The problem, I see, is latching the input so it is only read once, not continually the input is high.
    How do you do this?
    I don't really understand the question, how you mean by only reading the input once ?

    The flow of the code is such that it does not progress until high, does not progress until low, does not progress until high again and so on. That's what you have in your flowchart and what you appear to have implemented in your code ( though it's slightly different to the flowchart ).

  2. #22

    Default

    My assumption.
    His code doesn't really check the input has toggled off again, so I assume he means that he keeps reading and incrmenting.
    Thats why I gave the snippet on the previous page.

    If I've got that wrong then he needs to clarify, but he MUST check his hardware first otherwise all our time writing code is wasted

  3. #23
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,326

    Default

    Thanks Dippy. Posted at same time and missed your code over the page.

    You're right, the code is more different to the flowchart than I realised, will continually increment b0, hit 5 almost immediately, and there's the problem ... probably. Your code example should work - and saves me typing what I would have used as well !

    There's a moral there for testing in simulation.

  4. #24
    Moderator
    Join Date
    Jan 1970
    Location
    North Norfolk blow-in.
    Posts
    6,129
    Blog Entries
    1

    Default

    Just to make absolutely sure,

    The circuit in post #13 shows Pin4 ("Leg" 3) as input.

    The code in post #19 shows Pin3 as input.

    Is there a typo somewhere?

    Code:
    let b0 = 0
    main:
    if pin3 = 0 then main
    if pin3 = 1 then 
    pause 300
    let b0 = b0 + 1
    if b0 < 5 then main
    high 1
    endif
    goto main
    e
    Attached Images Attached Images

  5. #25
    New Member
    Join Date
    Apr 2011
    Location
    London
    Posts
    18

    Default

    Eclectic
    Simplified the circuit to reduce room for errors.



    Used Dippy's code and added the switch bounce routine at the start.
    This is now as the flowchart with input being held high until it goes low triggering the count increment.
    My thoughts are that as the count increment is being done when the input goes low there is not need for the switch bounce routine.

    Anyway, it does nothing. Circuit checked with meter.
    Chip voltage at switch on are +4.33/0/0/4.33//0/0/0/-ve All at zero at off.

    Code:
          symbol bounce = b0	;define bounce variable
          symbol counter = b1	;define count variable
    	bounce  = 0
    	counter = 0
    bound:
             if pin3 = 1 then add       ;goto bounce routine
             if pin3 = 0 then bound    ;waits for input high
    add:
             pause 100
             let bounce = bounce + 1
             if bounce < 5 then bound
    
    counting:
             do
              loop while pin3 = 1	' Sit here while pin3 is high
       	
       	   pin3 = 0    		' pin3 going low triggers counting
                inc counter		' increment by +1
                 goto counting		' loop back waiting for the next high/low
                 		
    	   if counter>5 then 	' check for condition counter higher than 5
    	    do
    	     loop while pin1 = 1	' pin1 goes permanently high
    	   endif

  6. #26

    Default

    Hi Tony,

    apologies if I'm off the mark here, but does this do what you want..? If not, I'll go back to my nap.

    Code:
    	symbol bounce = b0		;define bounce variable
    	symbol counter = b1		;define count variable
    	bounce  = 0
    	counter = 0
    
    bound:
    	if pin3 = 1 then add       	;goto bounce routine
    	if pin3 = 0 then bound    	;waits for input high
    
    add:
    	pause 100
    	let bounce = bounce + 1
    	if bounce < 5 then bound
    
    counting:
    	do
    		loop while pin3 = 1	' Sit here while pin3 is high
    	inc counter				' increment by +1
    	if counter>5 then 		' check for condition counter higher than 5
    		high 1
    	endif	
    	goto counting			' loop back waiting for the next high/low
    It's basically your last code snippet with the counting section modded slightly. Reason it wasn't switching the output is that it was looping back at the goto counting and never getting as far as checking the value of counter

    Piers
    Last edited by Piers; 04-06-2011 at 01:01. Reason: extra info

  7. #27

    Default

    I'm confused by your code....

    You do an IF pin3 and the next line you say
    pin3 = 0 ' pin3 going low triggers counting
    eh?

    In your very first post, did your very first bit of code work?

    In your last snippet is something missing? It doesn't look right and it looks a bit lacking structure.

  8. #28
    New Member
    Join Date
    Apr 2011
    Location
    London
    Posts
    18

    Default

    Piers, thanks for the code mods, this is the result

    Input low at power on. Output low
    When input goes high, output remains low
    When input goes low, output goes high and remains high

    Input high at power on, output low
    When input goes low output goes high and remains high

    It is not counting 5 high/low pulses before output going high.

    The objective of this module is to do 5 on/off switching then output goes high and remains high.

    The application is to count 7 shaft revolutions before turning off the motor. The motor carries on turnig for half a turn before stopping. The counter has carry on counting as when the motor is reversed it has to go back to zero count to get back to its original position. The reverse rotation is another story but lets get to that when we can count in the forwards direction first.

    An IR optoswitch works with a 12 slot disc ie 84 pulses before turning motor off. The disc can be left stationary with high or low input for 30mins +
    So to initiate the count the input must be high or go high before going low to trigger the count.

    Nothing has worked to solve the problem. The initial coding was made too complex as I was trying to do too much, hence reducing it down to a switch and led.

    Is there a totally different solution to this problem?
    Last edited by joliebrise; 04-06-2011 at 14:35. Reason: added last line

  9. #29

    Default

    Tony,

    ah understand now I think...

    Try this...
    Code:
    	symbol bounce = b0		;define bounce variable
    	symbol counter = b1		;define count variable
    	bounce  = 0
    	counter = 0
    
    bound:
    	if pin3 = 1 then add       	;goto bounce routine
    	if pin3 = 0 then bound    	;waits for input high
    
    add:
    	pause 100
    	let bounce = bounce + 1
    	if bounce < 5 then bound
    
    counting:
    	do
    		loop while pin3 = 1	' Sit here while pin3 is high
    	inc counter				' increment by +1
    	if counter>5 then 		' check for condition counter higher than 5
    		high 1
    	endif
    	do
    		loop while pin3 = 0	' Sit here while pin3 is low	
    	goto counting			' loop back waiting for the next high/low
    Piers
    Last edited by Piers; 04-06-2011 at 16:15.

  10. #30
    Senior Member
    Join Date
    Feb 2010
    Location
    Don't Mess With My Texas!
    Posts
    2,108

    Default Initialization is important

    I see where you initialize bounce and counter to zero when first run, but I don't see any initialization of these two variables after you start incrementing. That's important because you are comparing them to constants in your loops.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •