Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Subroutines for Reading a Multiplexer


jdgile Oct 24, 2018 05:36 PM

I have  datalogger which is hooked up directly to an instrument that reads every minute as well as two multiplexers which read daily.  I would like to be able to read the mux'es at will without duplicating a bunch of code. 

I am attempting to read the individual multiplexers at will with a "read_mux#_now" boolean toggle in addition to regular scan intervals.  My attempts so far have led me to creating individual subroutines for each mux and then calling them up for the standard scan or when the "read_mux#_now" toggle is true. 

When I placed the multiplexer portion of the code into subroutines I get a compile error: "SubScan used outside of Scan .. NextScan".  I am new at this, I do know why the error is being thrown up, but I do not know what I should be doing instead of this.  For reference my code is below: 

'Declared variables, units and tables removed for brevity

'///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'//////////////////////////////DECLARE SUBROUTINES\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Sub Read_Mux-1
	PortSet(U5,1) 'TURN MUX 1 ON
	Delay(0,150,mSec)
	Count=1
'This is where I have the compile error
	SubScan(0,uSec,15)
		PulsePort(U7,10000) 'SWITCH TO NEXT PORT ON MUX
		VibratingWire(VW(1),1,U1,1200,2800,1,0.01,"",60,1.4051E-3,2.369E-4,1.019E-7) 
		Freq(Count)=VW(1)
		Amp(Count)=VW(2)
		SNRat(Count)=VW(3)
		NFreq(Count)=VW(4)
		DRat(Count)=VW(5)
		DL1_Therm(Count)=VW(6)
		DL1_Digits(Count)=Freq(Count)^2/1000 'CALCULATE DIGITS
		Disp(Count)=(DL1_Digits(Count)-DL1_Digits0(Count))*G_Factor(Count) 'CALCULATE DISPLAY VALUES
		Count=Count+1
	NextSubScan
	FieldCal(4,DL1_Digits(),15,0,DL1_Digits0(),ZMode,0,CIndex,CAvg) 'Zeroing calibration for Jointmeter calculations 'DL1_Digits()'
	PortSet(U5,0) 'Turn AM16/32 Multiplexer Off
	Delay(0,150,mSec)
EndSub

Sub Read_Mux-2
  PortSet(U6,1) 'TURN MUX 2 ON
	Delay(0,150,mSec)
	Count_2=1
	SubScan(0,uSec,8)
		PulsePort(U7,10000) 'SWITCH TO NEXT PORT ON MUX
		VibratingWire(VW_2(1),1,U1,1000,4000,1,0.01,"",60,1.4051E-3,2.369E-4,1.019E-7)
		Freq_2(Count_2)=VW_2(1)
		Amp_2(Count_2)=VW_2(2)
		SNRat_2(Count_2)=VW_2(3)
		NFreq_2(Count_2)=VW_2(4)
		DRat_2(Count_2)=VW_2(5)
		DL1_Therm_2(Count_2)=VW_2(6)
		DL1_Digits_2(Count_2)=Freq_2(Count_2)^2/1000 'DIGITS CALCULATION
		Lvl(Count_2)=(DL1_Digits_2(Count_2)-DL1_Digits0_2(Count_2))*G_Factor_2(Count_2)  'PRESSURE PSI CALCULATION
		ELEV(Count_2)=(Lvl(Count_2)*2.3067)+SELEV(Count_2) 'WATER ELEVATION CALC
		Count_2=Count_2+1
	NextSubScan
	FieldCal(4,DL1_Digits_2(),8,0,DL1_Digits0_2(),ZMode_2,0,CIndex_2,CAvg_2) 'Zeroing calibration for Piezometer calculations 'DL1_Digits_2()' 
	PortSet(U6,0) 'Turn AM16/32 Multiplexer Off
	Delay(0,150,mSec)
EndSub

Sub Initialize_JMCAL
  CIndex=1 : CAvg=1
	For Count = 1 To 15
		DL1_Digits0(Count)=F0DL1_Digits(Count) ': DL1_Therm_20(Count)=F0Temp(Count)
	Next
EndSub

Sub Initialize_VWPCAL
 	CIndex_2=1 : CAvg_2=1
	For Count_2 = 1 To 8
		DL1_Digits0_2(Count_2)=F0DL1_Digits_2(Count_2) 
	Next
EndSub

'///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'//////////////////////////////////MAIN PROGRAM\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

BeginProg
	Scan(1,Min,1,0) 'SETTING SCAN TIME FOR PROGRAM
	'///READING FLOWMETER 2122 PER MAIN SCAN RATE\\\
		VibratingWire(VW_3(),1,U9,100,6500,1,0.01,"",_60Hz,1.4051E-3,2.369E-4,1.019E-7)
		Freq_3=VW_3(1)
		Amp_3=VW_3(2)
		SNRat_3=VW_3(3)
		NFreq_3=VW_3(4)
		DRat_3=VW_3(5)
		DL1_Therm_3=VW_3(6)
 		DL1_Digits_3=Freq_3^2/1000

		If IfTime (0,1440,min) Then
 		    Call Initialize_JMCAL
		    Call Initialize_VWPCAL
		    FCLoaded=LoadFieldCal(True)
	        '///MAIN SCAN\\\
    	            Battery(Battv)
		    PanelTemp(PTemp_C,60)
		'///READ MUX 1 - JOINTMETERS\\\
		    Call Read_Mux-1
		'///READ MUX 2 - PIEZOMETERS\\\
		    Call Read_Mux-2
                Endif

    	If Read_JM_Now = true Then
  	        Call Initialize_JMCAL 'INITIALIZE CALIBRATION FACTORS
  		FCLoaded=LoadFieldCal(True)	  
  		Call Read_Mux-1
  	EndIf

 	If Read_VWP_Now = true Then
	       Call Initialize_VWPCAL 'INITIALIZE CALIBRATION FACTORS
	       FCLoaded=LoadFieldCal(True)
		Call Read_Mux-2'READ PIEZOMETERS
	EndIf
	
		Read_JM_Now=False
		Read_VWP_Now=False
		
	'///Call Tables\\\	
		CallTable Datalogger1
		CallTable Datalogger1Info
		CallTable Display
		CallTable CalHist
		CallTable Fl2122
		
	NextScan
	
EndProg

 I am open to suggestions of better methods.  I am uncertain that this will work as written outside of the one compile error.  Thank you for your time in reading this.


jdgile Oct 24, 2018 11:17 PM

This is solved, I ended up substituting the subscan / nextsubscan for a do/loop statement.  This solved the problem.  It should be noted that this forces the datalogger into sequential mode, which may or may not be important to you.

Log in or register to post/reply in the forum.