// Smart led control with security lock
#define pot A0
#define led 9
String password = "123"; // password to unlock led control
bool accessGranted = false; // track if the user typed the correct passwd
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(31250);
Serial.println("Enter password to control led:" );
}
void loop() {
// put your main code here, to run repeatedly:
if (!accessGranted)
{
// check if user typed in serial monitor
if(Serial.available())
{
String input = Serial.readStringUntil('/n'); // read until end of line
input.trim(); //remove spaces or new line chars
// Check if the input matches the correct password
if (input == password) {
Serial.println("Access granted :) You can control the LED!");
accessGranted = true;
} else {
Serial.println("Access denied. Try again.");
}
}
} else {
// when access is granted:
//read the potentiometer value
int potValue = analogRead(pot); // Read value (0 - 1023)
// map pot value to brightness level (0 to 200 for safety).
int brightness = map(potValue, 0, 1023, 0, 200);
// Apply the brightness to the LED using PWM
analogWrite(led, brightness);
delay(100);
}
}