const int ledPin = 10; // LED connected to digital pin D10
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.println("Enter a string:");
}
void loop() {
if (Serial.available() > 0) {
// Step 1: Get input from user as a string
String inputString = Serial.readString();
inputString.trim(); // Remove any extra whitespace or newline characters
String combinedBinary = ""; // To store the combined binary sequence
// Step 2, 3: Convert string to ASCII, then to binary, and combine the binary
for (int i = 0; i < inputString.length(); i++) {
char currentChar = inputString.charAt(i);
int asciiValue = int(currentChar); // Convert to ASCII value
String binaryValue = String(asciiValue, BIN); // Convert to binary
// Make sure binary value is 8 bits long (pad with leading zeros if necessary)
while (binaryValue.length() < 8) {
binaryValue = "0" + binaryValue;
}
// Combine the binary string
combinedBinary += binaryValue;
}
// Step 4: Blink LED based on the combined binary sequence
Serial.print("Combined Binary Sequence: ");
Serial.println(combinedBinary);
// Blink LED based on the combined binary sequence
for (int i = 0; i < combinedBinary.length(); i++) {
if (combinedBinary.charAt(i) == '1') {
digitalWrite(ledPin, HIGH); // LED ON
} else {
digitalWrite(ledPin, LOW); // LED OFF
}
delay(200); // Delay to see the blink clearly (adjust timing if needed)
}
// Ask for the next input string after a short delay
Serial.println("\nEnter another string:");
delay(2000); // Delay before accepting the next input
}
}
Loading
xiao-esp32-c3
xiao-esp32-c3