/*
MSJ Researchers World
Date - 2nd DEC 2024
Mentor - Mr. Siranjeevi M
Contact - 7373771991
*/
#define LED_PIN 9 // RX Updating LED Signal
void setup()
{
// Initialize serial communication at 9600 baud rate
Serial.begin(9600);
// Set LED pin as output
pinMode(LED_PIN, OUTPUT);
Serial.println("Send data to the Arduino:");
Serial.println("You can send an integer, float, string, or character.");
}
void loop()
{
// Check if data is available to read
if (Serial.available() > 0) {
// Turn on the LED when data is received
digitalWrite(LED_PIN, HIGH);
// Read the incoming data as a string
String input = Serial.readStringUntil('\n'); // Read until newline character
// Remove any extra whitespaces or newline characters
input.trim();
// Display the received data
Serial.print("Received: ");
Serial.println(input);
// Determine and parse the input type
if (input.length() == 1 && isAlpha(input[0])) {
Serial.print("It's a character: ");
Serial.println(input[0]);
} else if (input.indexOf('.') != -1) { // Check if the input contains a decimal point
Serial.print("It's a float: ");
Serial.println(input.toFloat(), 4); // Display float with 4 decimal places
} else if (input.toInt() != 0 || input == "0") {
Serial.print("It's an integer: ");
Serial.println(input.toInt());
} else {
Serial.println("It's a string.");
}
Serial.println();
// Turn off the LED after a short delay
delay(200); // LED stays on for 200 ms
digitalWrite(LED_PIN, LOW);
}
}