arduino = Runtime.createAndStart("arduino","Arduino") arduino.connect("COM5") # arduino.setSampleRate(30000) readAnalogPin = 0 arduino.enablePin(readAnalogPin) arduino.addListener("publishPin", "python", "input") pid = Runtime.start("pid","Pid") #set Kp, kd, ki kp = gain, how strong it react kd = how fast it react ki= take care of the sum of errors (differences between target and actual value) in the time pid.setPID("x", 10.0, 0, 1.0) pid.setMode("x", 1) #set the range of the "correction" pid.setOutputRange("x", -5, 5) # pid.setControllerDirection("x", 0) #set a starting analog value, which will pilot the MOSFET on the Gate heaterValue = 512 def onPin(data): thermistorPin = data print 'thermistor value is', thermistorPin.value global heaterValue global futureHeaterValue #target of temperature or target value pid.setSetpoint(150) #input value pid.setInput(thermistorPin.value) pid.compute() correction = pid.getOutput() futureHeaterValue = (heaterValue + correction) if (futureHeaterValue < 1024) and (futureHeaterValue >0): heaterValue = futureHeaterValue arduino.analogWrite(4,futureHeaterValue) print heaterValue else : arduino.analogWrite(4,heaterValue) print heaterValue