Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arduino Code: The code I have below is a wireless signal code from HC-12. I am s

ID: 3807882 • Letter: A

Question

Arduino Code:

The code I have below is a wireless signal code from HC-12. I am sending String of numbers and this code is readying it. However I just want to know how to convert the String that are getting sent over into int, so i can work with those numbers. I need to convert the data sent to the String "input" into an int type.
________________________________________________________________
#include

SoftwareSerial mySerial(2, 3); //RX, TX
int ledPin = 13;
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
  
}

void loop() {
if(mySerial.available() > 1){
  
String input = mySerial.readString();//read string
Serial.println(input);
mySerial.flush();//clear the serial buffer for unwanted inputs   
  
delay(10);//delay little for better serial communication

********** This is where i am having problem with the code. It wont accpeted it because it is a string and not an int. How do i convert string "input" into an int type?*******

if( input > 10) {
digitalWrite(ledPin, HIGH)
}
}

Explanation / Answer

You can always use toInt() inbuilt function to convert a string to number. I have modified your code in the IF statement. Please provide feedback if it helped you.

#include

SoftwareSerial mySerial(2, 3); //RX, TX
int ledPin = 13;
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
  
}

void loop() {
if(mySerial.available() > 1){
  
String input = mySerial.readString();//read string
Serial.println(input);
mySerial.flush();//clear the serial buffer for unwanted inputs   
  
delay(10);//delay little for better serial communication

if( input.toInt() > 10) // Added the function to convert the string to int.

{
digitalWrite(ledPin, HIGH)
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote