#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private pinSetpoint As Pin
Private pinFeedback As Pin
Private pinOutput As Pin
Private pinAdditionalOutput As Pin
Private Parameters As PIDParameters
Private parametersID As ULong
Private pidController As PIDController
Dim cont As CONTROLLINO
Dim A As Long = 1000 'Resistance in darkness in KΩ
Dim B As Int = 15 'Light resistance (10 Lux) in KΩ
Dim Rc As Int = 10 'Calibration resistance in KΩ
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
pinSetpoint.Initialize(cont.CONTROLLINO_A1, pinSetpoint.MODE_INPUT) 'Pin 55 - Pot wiper
pinFeedback.Initialize(cont.CONTROLLINO_A0, pinFeedback.MODE_INPUT) 'Pin 54 - LDR
pinOutput.Initialize(cont.CONTROLLINO_D0, pinOutput.MODE_OUTPUT) 'Pin 2 - standard LED
pinAdditionalOutput.Initialize(cont.CONTROLLINO_D1, pinAdditionalOutput.MODE_OUTPUT) 'Pin 3 - 24VDC fan
parametersID = Parameters.Initialize(0.4, 6.0, 0.0001)
pidController.Initialize(parametersID,pidController.DIRECTION_DIRECT)
pidController.Input = MapRange(pinFeedback.AnalogRead, 0, 1023, 0, 255)
pidController.Setpoint = pinSetpoint.AnalogRead
pidController.TurnOn(True)
AddLooper("localLoop")
End Sub
Sub LocalLoop
pidController.Setpoint = MapRange(pinSetpoint.AnalogRead, 0, 1023, 0, 255)
pidController.Input = readFeedback
Log("Setpoint = ", MapRange(pinSetpoint.AnalogRead, 0, 1023, 0, 255))
Log("Feedback = ", readFeedback)
Log("Output = ", pidController.Output)
Log("Error = ", pidController.GetError)
Log(" ")
pidController.Update()
pinOutput.AnalogWrite(pidController.Output)
pinAdditionalOutput.AnalogWrite(pidController.Output)
End Sub
Sub readFeedback As Int 'specific to the LDR I am using as feedback from the "plant"
Dim mean As Float = 0
Dim V As Float
For i = 0 To 99
V = pinFeedback.AnalogRead
mean = mean + (V * A * 10) / (B * Rc * (1024 - V))
Next
mean = mean / 100
Return mean
End Sub