
Variable Numeric SlotMotor1
Variable Numeric SlotMotor2
SlotMotor1 = 1
SlotMotor2 = 3

Variable Numeric Pos1
Variable Numeric Pos2

Button 1 Text Home MOTORS
Button 2 Text Test1
Button 3 Text Test2
Button 5 Text Loop

Option GlobalKeys Enabled

Key Right		Gosub MovePos1
Key Left 		Gosub MoveNeg1
Key Up 			Gosub MovePos2
Key Down 		Gosub MoveNeg2

Key A Goto Loop
Key S Goto Stop
Key H Gosub Home MOTORS

'Gosub "HOME MOTORS"

Load Vars
Pos1 = v1
Pos2 = v2
Gosub PrintValues

Label Stop
Stop

' ==========================================
'  LOOP TEST
' ==========================================
'
Label Test1
	Pos1 = 177.69
	Gosub MoveMotors
	Wait Seconds 3
Return

Label Test2
	Pos1 = 46
	Gosub MoveMotors
	Wait Seconds 3
Return

Label Loop
	'Pos1 = 177.69
	'Gosub MoveMotors
	'Wait Seconds 3
	''
	'Pos1 = 46
	'Gosub MoveMotors
	'Wait Seconds 1
	''
	'Pos1 = 147
	'Gosub MoveMotors
	'Load "SUONO_DEL_VENTO_MARE.mp3"
	'Wait Seconds 1
	'Load Stop
	''
	'Pos1 = 0
	'Pos2 = 0
	'Gosub MoveMotors
	'Wait Seconds 4
	'''
	'Pos1 = -6.50
	'Pos2 = -33.60
	'Gosub MoveMotors
	'Load "Calipso Eleni.wav"
	'Wait Seconds 10
	'Load Stop
	''
	'Pos1 = 90
	'Gosub MoveMotors
	'Wait Seconds 1
	'
	'Pos1 = 170
	'Pos2 = -350
	'Gosub MoveMotors
	''Load "Calipso Eleni.wav"
	'Wait Seconds 0.05
	''Load Stop
	'
	'Pos1 = 170
	'Pos2 = -360
	'Gosub MoveMotors
	''Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	'Wait Seconds 0.05
	''Load Stop
	
	Pos1 = 117.90
	Pos2 = -388.20
	Gosub MoveMotors
	'Load "Calipso Eleni.wav"
	Wait Seconds 1
	'Load Stop
	
	Pos1 = 134.60
	Pos2 = -397.70
	Gosub MoveMotors
	'Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	Wait Seconds 1
	'Load Stop
	
	Pos1 = 93.30
	Pos2 = -405.30
	Gosub MoveMotors
	'Load "Calipso Eleni.wav"
	Wait Seconds 1
	'Load Stop
	
	Pos1 = 109.40
	Pos2 = -420.80
	Gosub MoveMotors
	'Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	Wait Seconds 1
	'Load Stop
	
	Pos1 = 146.20
	Pos2 = -423.00
	Gosub MoveMotors
	'Load "Calipso Eleni.wav"
	Wait Seconds 1
	'Load Stop
	
	Pos1 = 153.20
	Pos2 = -442.40
	Gosub MoveMotors
	'Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	Wait Seconds 1
	
	'Pos1 = 164
	'Pos2 = -387
	'Gosub MoveMotors
	''Load "Calipso Eleni.wav"
	'Wait Seconds 0.1
	''Load Stop
	'
	'Pos1 = 147
	'Pos2 = -396.3
	'Gosub MoveMotors
	''Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	'Wait Seconds 0.1
	''Load Stop
	''
	'Pos1 = 182.3
	'Pos2 = -407.1
	'Gosub MoveMotors
	''Load "Calipso Eleni.wav"
	'Wait Seconds 0.1
	''Load Stop
	'
	'Pos1 = 118
	'Pos2 = -413.5
	'Gosub MoveMotors
	''Load "Paco De Lucia, Al Di Meola, John McLaughlin - The Guitar Trio - 2 - Beyond The Mirage.mp3"
	'Wait Seconds 0.1
	''Load Stop
	'
Goto Loop


' ==========================================================
'  MOVE MOTORS
' ==========================================================
Label MoveMotors
	Slot (SlotMotor1) = Pos1
	Slot (SlotMotor2) = Pos2
	SlotMotor = SlotMotor1
	Gosub WaitMotor
	SlotMotor = SlotMotor2
	Gosub WaitMotor
	Gosub PrintValues
Return

' ==========================================================
'  HOME MOTORS
' ==========================================================
Label HOME MOTORS
	SlotMotor = SlotMotor1
	SlotSensor = 5
	SpeedSearch = -1      ' search direction and speed (1 = max speed)
	SpeedReturn = +0.01    ' return direction and speed
	DeclaredZero = 0
	InitialPosition = -58.40   ' relative to DeclaredZero
	Gosub Home
	Pos1 = 30
	
	SlotMotor = SlotMotor2
	SlotSensor = 6
	SpeedSearch = -1    ' search direction and speed (1 = max speed)
	SpeedReturn = +0.01   ' returndirection and speed
	DeclaredZero = 0
	InitialPosition = 0.40  ' relative to DeclaredZero
	Gosub Home
	Pos2 = 30
	
	Gosub PrintValues
Return

' ==========================================================
'  Home the Motor(SlotMotor) to the Sensor(SlotSensor)
' ==========================================================
Variable Numeric SlotMotor
Variable Numeric SlotSensor
Variable Numeric SpeedSearch
Variable Numeric SpeedReturn
Variable Numeric DeclaredZero
Variable Numeric InitialPosition

Label Home
	' ------------------------- detach if sensor is active
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn
	EndIf
    ' ------------------------- search the sensor
Label SearchHome
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedSearch
	Wait Seconds 0.001
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Slot(SlotMotor) = Reset
		Wait Seconds 0.1
		Slot(SlotMotor) = 0
		Wait Seconds 0.1
		Goto DetachSensor
	EndIf
Goto SearchHome
    ' ------------------------- detach from the sensor
Label DetachSensor
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn
	Wait Seconds 0.001
	If Slot (SlotSensor) < 500
		Slot(SlotMotor) = Reset
		Wait Seconds 0.1
		Slot(SlotMotor) = DeclaredZero
		Wait Seconds 0.1
		Goto SetFinalValues
	EndIf
Goto DetachSensor
    ' ------------------------- reset and move to InitialPos
Label SetFinalValues
	Slot(SlotMotor) = InitialPosition
	Gosub WaitMotor
Return

' ==========================================================
'   Wait until the motor(SlotMotor) is stopped
' ==========================================================
Label WaitMotor
	Wait Seconds 0.01
	Label Wait1
		If Abs(Slot(SlotMotor + 1)) < 0.01
			Return
		EndIf
		Wait Seconds 0.005
	Goto Wait1
Goto WaitMotor


' ================================================
'  JOG WITH keyboard
' ================================================
Label MovePos1
	Gosub SpeedModifiers
	Pos1 = Pos1 + JogSpeed
	Slot(SlotMotor1) = Pos1
	Gosub PrintValues
Return

Label MoveNeg1
	Gosub SpeedModifiers
	Pos1 = Pos1 - JogSpeed
	Slot(SlotMotor1) = Pos1
	Gosub PrintValues
Return

Label MovePos2
	Gosub SpeedModifiers
	Pos2 = Pos2 + JogSpeed
	Slot(SlotMotor2) = Pos2
	Gosub PrintValues
Return

Label MoveNeg2
	Gosub SpeedModifiers
	Pos2 = Pos2 - JogSpeed
	Slot(SlotMotor2) = Pos2
	Gosub PrintValues
Return

Label PrintValues
	Print "Pos1 = " + Format(Pos1, "0.00") + "  Pos2 = " + + Format(Pos2, "0.00")
Return

Variable Numeric JogSpeed
Label SpeedModifiers
	JogSpeed = 0.1
	Select True
		Case Key("SHIFT")
			JogSpeed = JogSpeed * 100
		Case Key("CTRL")
			JogSpeed = JogSpeed * 10
		Case Key("ALT")
			JogSpeed = JogSpeed * 0.1
	EndSelect
Return

Label EventStop
	v1 = Pos1
	v2 = Pos2
	Save Vars
End


