int relay = 8;
int buzzer = 12;
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
String str;
Serial.println("Enter a string:");
while (!Serial.available()) {} // Wait for user to input string
delay(100); // Allow time for input to complete
str = Serial.readString(); // Read the input string
str.trim(); // Remove leading and trailing whitespaces
if (isPalindrome(str)) {
Serial.println("The string is a palindrome.");
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);
digitalWrite(relay, LOW);
delay(1000);
} else {
Serial.println("The string is not a palindrome.");
}
}
void loop()
{
}
bool isPalindrome(String str) {
int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
return false; // If characters at symmetric positions don't match, not a palindrome
}
}
return true; // If all characters match, it's a palindrome
}