Trying to receive a 2 digit integer over serial

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 += err
dt;
//Serial.println(s_integral);
float s_derivative = (err - previous_error)/dt;
//Serial.println(s_derivative);
int U_in_ctrl = (K_P_ctrl
err + 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);
}

why doesn't the following work?

(b) = Serial.readStringUntil ('\n');

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

It is important to provide as much of the information that is needed to solve your problem as you can, in your first posts. The forum link above has guidelines for posting in a standard way that makes it easiest for people to provide you with useful answers. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.