Two counts the same time

Roy Spiller

New Member
Hi i have two motors with a hall sensor on each with a full reel of tape on one and an empty spool on the other, i need to run the tape of the full one onto the empty one and back again without the tape running off (going to far). My idea is to count both sensor pulse's until they are the same which would be the middle of the tape then double that amount to reach the end. The motors are working fine but i can only get it to count on one channel at a time therefore i cannot compare both counts. I have just found out the 40x2 does not support parallel tasks. is there any way i can count both pulses the same time on the 40x2, ihave built everthing else around this chip, or will i have to use one of the m type chips. This is one motor and sensor code thats works fine.
Roy

main:
if pinb.4 = 0 then test push button
goto main
test:
w0 = 0
w1 = 0
low b.6
pwmout c.2, 199, 500
do
do:loop until pinb.0 = 0
do:loop until pinb.0 = 1
w1 = w1 + 1
if w1 >= 1200 then high b.6 high a.5 endif brake and led
loop
 

hippy

Technical Support
Staff member
You could poll the two sensors within a do-loop and count each transition of the sensors ...

Code:
Do
  If pinX.X <> bit0 Then
    bit0 = bit0 ^ 1
    w10 = w10 + 1
  End If
  If pinX.Y <> bit1 Then
    bit1 = bit1 ^ 1
    w11 = w11 + 1
  End If
  ::
Loop
 

Roy Spiller

New Member
Hi Hippy
Thanks for the quick reply
How would i incorporate this code. This is the part i have been trying to get to work, when i press start button c.1 mortor runs for 2 seconds then c.2 motor starts slightly faster so the pulse's should catchup and stop the motors however when c.2 motor starts both brakes and the led come on with only a slight movement from c.2 motor.
Roy

main:
if pinb.4 = 0 then test ;start button
goto main
test:
w0 = 0
w1 = 0
low a.6 ;c.1 motor brake off
low b.6 ;c.2 motor brake off
pwmout c.1, 199, 500
pause 2000
pwmout c.2, 199, 400
do
do : loop until pind.1 = 0
do : loop until pind.1 = 0
w0 = w0 + 1
do : loop until pinb.1 = 0
do : loop until pinb.1 = 0
w1 = w1 + 1
if w0 = w1 then
high b.6 high a.6 high a.5 ; brakes on and led on
end if
loop
 

hippy

Technical Support
Staff member
Untested but perhaps something like this ...

Code:
main:
if pinb.4 = 0 then test ;start button
goto main
test:
[b]w10[/b] = 0
[b]w11[/b] = 0
low a.6 ;c.1 motor brake off
low b.6 ;c.2 motor brake off
pwmout c.1, 199, 500
pause 2000
pwmout c.2, 199, 400
do
[b]If pinD.1 <> bit0 Then
    bit0 = bit0 ^ 1
    w10 = w10 + 1
  End If
  If pinB.1 <> bit1 Then
    bit1 = bit1 ^ 1
    w11 = w11 + 1
  End If[/b]
if [b]w10 = w11[/b] then
high b.6 high a.6 high a.5 ; brakes on and led on
end if
loop
 

Roy Spiller

New Member
Hi Hippy
I needed to change the w10 = 0 at the start to w10 = 2 to get both motors to run, they are now running fine however there is a problem inconsistent counting the time it takes for both motors pulses to match varying between 15 and 25 seconds, i only have one counter to test the accuracy of the and it is varying quite a lot with each go. Any thoughts?
Regards
Roy
 

hippy

Technical Support
Staff member
The speed of reel rotation may be higher than the PICAXE can keep up with. You could try adding add a SETFREQ M16 to make the 40X2 run faster.

It might also be necessary to add a resistor-capacitor circuit to each input to stretch the pulses if they are too short for the PICAXE to catch.
 

Roy Spiller

New Member
Hi Hippy
Thanks once again for your quick response. The do loop you suggested for this project at the start, counted the pulse&#8217;s very accurately at the same speed, however I could not get both to count the same time, is it possible to get this system counting two separate pulses the same time?
Regards
Roy
 

Roy Spiller

New Member
Hi Hippy
I've been looking at the project again your code is now working fine. The problem seems to be the psu I was using i tried a bench psu and it is cleared the problem. Thanks for your help.
Roy
 
Top