what control does is look at the difference of where you are and where you want to be. we call that error or off-set
ERROR = PV - SV // [find the difference between where you are and where you want to be ]
// error = process value less set-point value
this will be either a positive number or a negative number, and on very rare occasions, it will be 0
int SV = 50 ; // fixed value of 50.00000 degrees, can be changed in programming
int rawSensor = A0 // temperature sensor into pin A0
int ERROR ; // a global variable
int PV ; // a global variable.
int PumpSpeed = 0 ;
ERROR = PV - SV ;
// timer function
delay(1);
timer = timer + 1; // increments once every 1 mS
if( error >= 10){ // WAY off
if (timer > = 1000 ) { // once a second
PumpSpeed = PumpSpeed + 1 ;
timer=0; // reset the timer
}
}
if( error >= 2){ // off but not horrible
if (timer > = 5000 ) { // once ever 5 seconds
PumpSpeed = PumpSpeed + 1 ;
timer=0; // reset the timer
}
}
}
if( error >= 0.2){ // almost dead on
if (timer > = 10000 ) { // once ever ten seconds
PumpSpeed = PumpSpeed + 1 ;
timer=0; // reset the timer
}
}
as you can see that is only to speed up the pump and does not allow to slow it down.
this is crude, but should show you how you can break it into manageable pieces.
you might find that only 2 stages are needed, or that you want 5... as you can see, you can pick things.
the delay is blocking, but not really blocking too much to be in the way.
this is a pseudo fuzzy logic. it uses bands of error on the sensor, but does not react, or over-react like proportional does.
this has fixed times. if you wanted to clean it up, you can add integral to set the times, and then only need to have one of these loops.
INTEGRAL=INTEGRAL + ERROR ;
have to be a long because if the error is large it will jump to a million in seconds.
if ( integral > 100000) ; // if you lose EVERY instance of delay () use a small number like 1 million to start
// this will not work if you use any delay anywhere in your program.
so :
INTEGRAL=INTEGRAL + ERROR ;
if ( INTEGRAL > 100000) ;
pulse = HIGH;
INTEGRAL = 0 ; // ================= ADDED
}
if (pulse = = HIGH ) { // once every time the cumulative error counts up to 1 million
PumpSpeed = PumpSpeed + 1 ;
pulse = LOW;
}
if the error is way off, it will increase pump speed a couple times a second.
if the error is getting close, it will take many seconds to increase pump speed
if you are very close, some values may go negative and it could take minutes
once you are almost dead on, the number of error over and error under should keep the count from ever reaching 1 million
and (hopefully) no change for hours.
I am not saying this is the best solution, but rather here is a look at how you can make it work with what you have without getting krazy.
as a note, say your sketch ran the loop 1 million times a second. your count would roll over once per second for every degree you were off. if you were off 5 degrees, then it would roll over 5 times a second.