I'm working on a project where I need to collect data from a load cell using the serial monitor. I can collect this data fine, the problem is that it comes with some random letters before and lbs after. I want to change this data from a char value to an int so I can just collect the numerical data and use that. The problem is I cannot find a way to print the data other than using the char variable type. Can anyone help? I will list the section of code that I use to collect this below.
Please post an example sketch that illustrates the problem, using code tags when you do
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Thanks for the help. Here is an example of my code along with a picture of the serial data I receive. The data itself is all good, but I want to be able to extract just the numbers.
#include <LiquidCrystal_I2C.h> // Including LCD
#include <SoftwareSerial.h>
SoftwareSerial THRUST(10, -1); // Setting up analog communications for loadcell
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set up LCD display.
void setup() {
Serial.begin(9600); // Begin serial communication.
THRUST.begin(9600); // Begin load cell communication.
lcd.begin(); // Begin LCD display.
lcd.backlight();
}
void loop() {
while (THRUST.available()) {
Serial.print(char(THRUST.read())); // Live prints thrust values in serial monitor.
}
lcd.print("THRUST");
lcd.print(THRUST); // Doesn't work currently but want to recieve interger to put here.
}
I changed Serial.available to THRUST.available and it worked for me! Thank you so much, I was messing around with the parseInt function for hours with no luck. Do you know how I can take this data now and print it on an LCD? I tried like this but it doesn't let me use the 'data' term to display it.
#include <LiquidCrystal_I2C.h> // Including LCD
#include <SoftwareSerial.h>
SoftwareSerial THRUST(10, -1); // Setting up analog communications for loadcell
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set up LCD display.
void setup() {
Serial.begin(9600); // Begin serial communication.
THRUST.begin(9600); // Begin load cell communication.
lcd.begin(); // Begin LCD display.
lcd.backlight();
}
void loop() {
if (THRUST.available())
{
int data = THRUST.parseInt();
Serial.println(data);
}
lcd.clear();
lcd.print("THRUST");
lcd.print(data);
}
That worked perfectly for me. Thank you so much again for your help. I'm very new to using Arduino and have been searching for a solution for hours so I appreciate it a lot.
Not particularly with the 'parseInt' function. I tried something similar before with the 'while' function instead and had no luck. I also believe I understand the scope of the variables too as the 'data' variable before was only defined within the 'if' function before which is why I couldn't use it outside, correct? Everything else is very clear to me now I think.
#include <LiquidCrystal_I2C.h> // Including LCD
#include <SoftwareSerial.h>
SoftwareSerial THRUST(10, -1); // Setting up analog communications for loadcell
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set up LCD display.
void setup() {
Serial.begin(115200); // Begin serial communication.
THRUST.begin(9600); // Begin load cell communication.
lcd.begin(); // Begin LCD display.
lcd.backlight();
}
void loop() {
if (THRUST.available())
{
int data = THRUST.parseInt();
while (THRUST.available() > 0)THRUST.read();
if (data == 0)return;
Serial.println(data);
lcd.clear();
lcd.print("THRUST ");
lcd.print(data);
}
}
Yes. In reply #6 I made it a local variable in the loop() function. It could have been made a global variable to make it available anywhere or its value could have been passed to a function to make it available to that function. In fact that is what is happening then you do Serial.println(data);