Dim IsPicaxeOn, IsSquirtBigEnough As Long
Dim PicaxeIsOn, TooSmallSquirt, BigEnoughSquirt As Double
'----------------------------------------------------------

Setting these values stand In For Input signals that obviously won't be present in a simulation

SetVar(310,1)
SetVar(311,1)
SetVar(312,0)
'311 = 1 simulates successful 'squirt' ie enough paint deposited; focus passes back to gcode when macro ends

PicaxeIsOn = GetVar(310)	'If this is Input1 =1, it's like the picaxe being on, if it's 0 it's like picaxe hasn't switched on
BigEnoughSquirt = GetVar(311)	'If this is Input1 =1, it's like the squirt big enough so input1 gets switched on
TooSmallSquirt = GetVar(312)	'If this is Input2 =1, it's like the squirt being too small so input2 gets switched on

'----------------------------------------------------------

For k = 1 To 3			'has 3 goes at trying this routine before giving up

	ActivateSignal(Output1)	'switches on picaxe and light sensor
 	ActivateSignal(Output2)	'switches on relay powering up LED over light sensor

	IsPicaxeOn = 0		'initial values
	IsSquirtBigEnough = 0
	Sleep 150

	'For i = 1 To 100	'spends 1 second waiting for signal that picaxe sends when it switches on
	For i = 1 To 3
		'If IsActive(Input1) Then
		If PicaxeIsOn = 1 Then
			IsPicaxeOn = 1
			Message "M400 Picaxe is on"
			Sleep 1000
			Exit For
	Else	
			'Sleep 10
			Sleep 100
		End If
	Next i
	
	Sleep 1000		'timing of picaxe program means that it will take and store an initial light level reading now

	If IsPicaxeOn = 1 Then
		Code "G91" 	'this code deposits paint over the light sensor; size of puddle affects light sensor reading
		'Code "G00 Z150"
		Code "G04 P1000"
		Code "G90"
		Code "G00 A0"
		Code "A0.4"
		Code "G04 P500"
		Code "G00 A0"
		Code "A0.2"
		Code "G04 P500"	
		'could do with sending a message to picaxe to say 'measure light now and compare', but anyway, thats what it now does
	Else
		Message "M400 Picaxe not switched on, faulty, or it's output not reaching BOB"
		End	 					
	End If

	'For j =1 To 100	'spends 1 second waiting for input from picaxe that decides if ratio of before and after 'squirt' light levels means puddle was big enough
	For j =1 To 3
		If BigEnoughSquirt = 1 Then
		'If IsActive(Input1) Then
			IsSquirtBigEnough = 1
		Else 
			If TooSmallSquirt = 1 Then
			'If IsActive(Input2) Then
				IsSquirtBigEnough = 0	
		Else
			Message "M400 Check inputs from picaxe, esp. input#2"
			Sleep 1000		
			End If 
		End If
		
	Sleep 10	
	Next j
	
	'If BigEnoughSquirt = 0 And TooSmallSquirt = 0 Then
	If BigEnoughSquirt = TooSmallSquirt Then		'ie both or neither input working, ie picaxe fault
		IsSquirtBigEnough = 999		
	End If 

	
	Code "G00 C120"			'wipes window clean for next attempt or next time

	If IsSquirtBigEnough = 1 Then
		Message "M400 Squirt big enough, passing back to main program"
		Sleep 1000
		Exit For
	Else 
	If IsSquirtBigEnough = 0 Then
		Message "M400 Squirt not big enough, trying again if this isn't the third try"
		Sleep 1000
	Else
	If IsSquirtBigEnough = 999 Then
		Message "M400 Check inputs from picaxe, esp. input#2 b****cks"
		Sleep 1000
	End If	
	End If
	End If
Next k


If k=3 And IsSquirtBigEnough = 0 Then
	Message "M400 A f*** up has occurred; consult big book of b****cks"
	Sleep 2000
End If
	'message k
End  
