#include <Arduino.h>
// Define the LED pins and serial communication baud rate
const int ledPin1 = 14;
const int ledPin2 = 12;
const long baudRate = 9600;
// The predefined correct answer string
const char* correctAnswer = "password";
// Variables for the blinking logic
bool isBlinking = true;
unsigned long previousMillis = 0;
long currentInterval = 1000; // This will change dynamically
// Variables for the timer logic
const long timerDuration = 10000; // 10 seconds in milliseconds
unsigned long startTimer;
bool timerActive = true;
// A buffer to store incoming serial data
char incomingData[10];
// A custom function to get the blink interval based on remaining time
// This function is specialized for this application and uses the global timerDuration
long getBlinkInterval(long remainingTime) {
// Input range for remainingTime: 0 to timerDuration
// Output range for the blink interval: 0ms (fastest) to 1000ms (slow)
return remainingTime * (1000 - 0) / (timerDuration - 0) + 0;
}
void setup() {
// Initialize both LED pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Start serial communication at the specified baud rate
Serial.begin(baudRate);
// Inform the user about the initial state and how to play
Serial.println("LED 1 is blinking. What's the secret word? You have 10 seconds!");
Serial.println("Enter your answer below and press Enter.");
// Start the timer
startTimer = millis();
}
void loop() {
// Handle serial input first
if (Serial.available() > 0) {
// Read the incoming bytes until a newline character is found
int bytesRead = Serial.readBytesUntil('\n', incomingData, sizeof(incomingData) - 1);
// Add a null terminator to the string
incomingData[bytesRead] = '\0';
// Check if the received command matches the correct answer
if (strcmp(incomingData, correctAnswer) == 0) {
Serial.println("That's the correct answer! ");
// Stop the blinking and timer, then turn LED 1 and 2 on permanently
isBlinking = false;
timerActive = false;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
Serial.println("LED 2 is now also on permanently.");
} else {
// The answer was incorrect, reset the timer
Serial.println("Wrong answer. You have 10 seconds to try again!");
digitalWrite(ledPin1, LOW); // Turn off the LED to restart the blink cycle
startTimer = millis(); // Reset the timer
}
}
// Check if the timer has expired
if (timerActive && millis() - startTimer >= timerDuration) {
Serial.println("Time's up! The LED is off. Try again!");
digitalWrite(ledPin1, LOW); // Turn off the LED to reset
startTimer = millis(); // Restart the timer for a new attempt
}
// Blinking logic only runs if isBlinking is true
if (isBlinking) {
// Calculate the time remaining
long elapsedTime = millis() - startTimer;
long remainingTime = timerDuration - elapsedTime;
// Dynamically adjust the blinking speed using the new function
currentInterval = getBlinkInterval(remainingTime);
// Check if the blinking interval has passed
if (millis() - previousMillis >= currentInterval) {
// Save the last time the LED was toggled
previousMillis = millis();
// Toggle the state of the first LED
if (digitalRead(ledPin1) == LOW) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
}
}
}