Question: Copy the Code: const int ledPin = 9 ; / / LED connected to pin 9 int brightness = 1 2 8 ; / /

Copy the Code:
const int ledPin =9; // LED connected to pin 9
int brightness =128; // Initial brightness (50% duty cycle)
void setup(){
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600);// Start Serial communication
Serial.println("");
Serial.println("Enter 'B' to set LED brightness (0-255).");
Serial.println("Enter 'F' to set blinking frequency (in Hz).");
Serial.println("Enter 'S' to stop blinking.");
}
void loop(){
if (Serial.available()>0){// Check if data is available
char command = Serial.read(); // Read the command
handleCommand(command);// Process the command
}
}
void handleCommand(char command){
if (command =='B'){
Serial.println("Enter brightness (0-255):");
while (Serial.available()==0); // Wait for input
brightness = Serial.parseInt();// Read the brightness
brightness = constrain(brightness,0,255); // Constrain value
analogWrite(ledPin, brightness); // Set LED brightness
Serial.print("Brightness set to: ");
Serial.println(brightness);
Serial.println("Enter 'B' to set LED brightness (0-255).");
Serial.println("Enter 'F' to set blinking frequency (in Hz).");
Serial.println("Enter 'S' to stop blinking.");
}
else if (command =='F'){
Serial.println("Enter blinking frequency (Hz)for 10 blinks:");
while (Serial.available()==0); // Wait for input
int frequency = Serial.parseInt(); // Read frequency
frequency = constrain(frequency,1,10); // Constrain frequency (1-10 Hz)
Serial.print("Blinking at: ");
Serial.print(frequency);
Serial.println(" Hz");
blinkLed(frequency);
Serial.println("Enter 'B' to set LED brightness (0-255).");
Serial.println("Enter 'F' to set blinking frequency (in Hz).");
Serial.println("Enter 'S' to stop blinking.");
}
else if (command =='S'){
Serial.println("Blinking stopped. LED set to steady brightness.");
analogWrite(ledPin, brightness); // Stop blinking and set steady brightness
Serial.println("Enter 'B' to set LED brightness (0-255).");
Serial.println("Enter 'F' to set blinking frequency (in Hz).");
Serial.println("Enter 'S' to stop blinking.");
}
else {
Serial.println("Invalid command. Try 'B','F', or 'S'.");
}
}
void blinkLed(int frequency){
int delayTime =1000/(2* frequency); // Calculate delay time in ms
for (int i =0; i <10; i++){// Blink for 10 cycles
analogWrite(ledPin, brightness);
delay(delayTime);
analogWrite(ledPin,0);
delay(delayTime);
}
analogWrite(ledPin, brightness); // Return to steady brightness
}
Lab Steps
Part 1: Setting Up the Circuit
Assemble the circuit on the breadboard as described above.
Connect the Arduino to your computer via USB.
Part 2: Understanding the Code
Discuss the role of Serial.read(), Serial.available(), and Serial.parseInt() in handling user input.
Explain how analogWrite() is used for PWM control.
Part 3: Running the Program
Open the Serial Monitor from the Arduino IDE (Ctrl + Shift + M).
Set the baud rate to 9600.
Set Serial Monitor to No Line Ending(right side of the screen)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!