'// ================================================================================================
'//
'// Original code written by Henrik Olsson, free *swenglish* translation by Bengt Sjölund alias tecno
'//
'// Feel free to use the code, please let this text of origin always be included 2008-05-22
'//
'// ================================================================================================



'// Motor connected to Output1, change according to your settings.'// Input connected to Input1, change according to your settings.
'// Max # of tools in changer.
MaxToolNum = 6
'// Time in ms for debounce and filtering of signal.
Debounce = 10    'ms'//Pulses from changer are NEVER shorter than this + /Debounce/ (ms)...'// ...not even when motor runs full speed.
MinPulseWidth = 300    'ms  (NOTE, in my(Bengt/Tecno) changer I set this to 200ms)

'// Pulses from changer are NEVER longer than this (ms)...even if 
'// ..."moves sluggish" or under acceleration.
MaxPulseWidth = 900    'ms (NOTE, in my(Bengt/Tecno) changer I set this to 1650ms)
'// =====================================================================
'//
'// Debounce - Min and Max PulseWidth's are to be changed as far as I can tell, not every ATC runs like my does
'//
'// =====================================================================

'//-----------------------------------------------------------------------
'//---Replace below with GetDRO etc 
OldTool = InputBox("Please input current tool number")
NewTool = InputBox("Please input new tool number")
'//-----------------------------------------------------------------------
'//-----------------------------------------------------------------------


'// Checks if we need to change tool?
'// If not shows a message and finish....
If NewTool = OldTool Then
MsgBox("New tool is same as old tool. No toolchange will occur.")
End
End If
'// At what 'direction'....
If NewTool > OldTool Then
  Steps = NewTool - OldTool		'//...forward
Else
  Steps = MaxToolNum - OldTool + NewTool
'//...bacwards
End If

'// As we are going to pass actual tool pulse
'//   "again" we have to add '1' to /Steps/
Steps = Steps + 1			'//...NOTE, Here I had to change the line to +2 to get correct positioning
				'//...do not know if this has to do with above timing settings or not

'//------------------For Error handling------------------------
MsgBox("Number of pulses to count: " & Steps)
'//--------------------------------------------------------


i = 0					'// Zero 'counter'
ActivateSignal(Output1)			'// Start motor

CountPulses: 

'  First we check if input is active. If NOT we sleep  /MinPulseWidth/ ms
'  before we chaeck again. MinPulseWidth must be set to be SURE we NEVER miss 
'  any pulses but still high 'enough' to minimize CPU load.
'
'  If input IS active then we sleep /Debounce/ ms and and the we check the pulse again to be
'  certain we have a 'genuine' pulse. If so we add to counter /i/ and compare this to our 'target'.
'
'  If counter /i/ matches the "target" /Steps/ then we turn of the motor and terminate the macro.
'
'  If counter does NOT match the "target" then sleep /MaxPulseWidth/ to be certain that we do not count
'  the same pulse again and jump back and check input status again.
'
'  The frequency on incoming pulses may not be higher than:
'  1 / (MaxPulseWidth + MinPulseWidth + Debounce )
'   If MaxPulsewidth is 900, MinPulseWidth is 300 and Debounce 10 the the frequency may not be
'  higher than 1 / 1.11 = 0.8Hz.
 If IsActive(Input1) Then		'// Do we have signal??
  Sleep Debounce			'// Debounce, and filtering...
  If IsActive(Input1) Then		'// ...do we still have signal?
   i = i + 1				'// The we have a real pulse, count this one....
   If i = Steps Then			'// Are we at correct tool position?
   DeActivateSignal(Output1)		'// If so we stop the motor...
'//----For Error handling---
MsgBox("Tool change is done")
'//----------------------

End				'// ...ending of the macro.
End If
   
   Sleep MaxPulseWidth		'// Make certain we DONT count the pulse again .
  End If
End If  
Sleep MinPulseWidth		'// Give some "space" to the system to avoid 100% CPU load
GoTo CountPulses			'// Wait for next pulse....

End  