Trying to send a temperature set point over serial and read it back to make sure its interpreted correctly.
When I send a value like 75 it returns a 7 on one line and then a 5 on the next.
Tried many things unsuccessfuly.
Any help is much appreciated.
It's the variable called (b) in the Code below:
// variables and definitions
//Important parameter, set to match environment
const int dt = 1000; // [ms] time constant in milliseconds (controller clock rate = 1/(dt/1000) [Hz])
#define SetTemp 95 // [degF] set temperature in DegF
#define MinTemp 50 // [degF] minimum expected temperature (needed for rescaling inputs)
#define MaxTemp 90 // [degF] maximum allowed temperature, over which heater is turned off (needed for rescaling inputs)
//#define Heatcool 0
// int mode;
char (a); // This is the character assign to the incoming heat or cool command from the bluetooth
String (b); // This is the character assigned to the incoming temperature setpoint from the blutooth
//I/O pins - don't edit unless replaced
#define thermistorPin A0
#define FETPin 3
//#define LEDPin //number of LED pin (optional)
//control parameters - editing not recommended
double K_P_ctrl = 3; //proportional gain
double K_I_ctrl = 0; //integral gain (set to lower values i.e. 10^-3)
double K_D_ctrl = 0; //derivative gain
// including headers and definitions
#include <math.h>
//Inititalization
//target temperature reached?
bool bInRange = 0;
//ticks per ms
int TicksPerMS = floor(1000/dt);
//Initialize PID variables:
float previous_error = 0;
float s_integral = 0;
//Thermistor code
double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0;
return Temp;
}
//PID controller code
void Control_PID(double iTemp){
//PID subroutine
float err = SetTemp - iTemp;
if (a == 'h'){
err = err1;
}
if (a == 'c') {
err = err -1;
}
// Serial.println ();
Serial.print ("Delta ");
Serial.println(err);
s_integral += errdt;
//Serial.println(s_integral);
float s_derivative = (err - previous_error)/dt;
//Serial.println(s_derivative);
int U_in_ctrl = (K_P_ctrlerr + K_I_ctrls_integral + K_D_ctrls_derivative)/(MaxTemp-MinTemp)*255;
previous_error = err;
// put voltage to output and write value to serial monitor
Serial.print("Output PWM frequency: ");
if (U_in_ctrl<=255){
if (U_in_ctrl > 0){
analogWrite(FETPin, U_in_ctrl);
Serial.println(U_in_ctrl);
}
else
{
analogWrite(FETPin, 1);
Serial.println("1 - cca. 0 V");
}
}
else{
analogWrite(FETPin,255);
Serial.println("255 - cca. 5 V");
}
}
void setup() {
Serial.begin(9600); // Set the baudrate to 9600
pinMode(FETPin, OUTPUT); //Asign FETPin as the Output for the pwm
}
void loop() {
//Take a temperature reading and display it
double Temp = double(Thermistor(analogRead(thermistorPin)));
Serial.print("Temperature:");
Serial.println(Temp); // display temperature;
Serial.print ("Set Point : ");
Serial.println (SetTemp);
//Check for incoming serial commands from bluetooth, a value of h for heating and c for cooling.
// If h, set mode equal to 1.
// If c, set mode equal to -1
if (Serial.available() > 0) //If something is on the serial line...read it.
{
(a) = Serial.read(); //Assign the character (a) to the value on the incoming serial line.
Serial.println (a); //Print the the value (a).
}
//Check for setpoint change
if (Serial.available()) //If something is on the serial line...read it.
{
(b) = Serial.readStringUntil ('\n');
Serial.println (b);
}
//Call controller algorithm
Control_PID(Temp); // call controller algorithm
//End line in serial monitor...
Serial.println("");
Serial.println("");
//wait dt before next cycle
delay(2000);
}