Question: Copy the Code: const int ledPin = 9 ; / / LED connected to pin 9 int brightness = 1 2 8 ; / /
Copy the Code:
const int ledPin ; LED connected to pin
int brightness ; Initial brightness duty cycle
void setup
pinModeledPin OUTPUT; Set LED pin as output
Serial.begin; Start Serial communication
Serial.println;
Serial.printlnEnter B to set LED brightness ;
Serial.printlnEnter F to set blinking frequency in Hz;
Serial.printlnEnter S to stop blinking.";
void loop
if Serialavailable Check if data is available
char command Serial.read; Read the command
handleCommandcommand; Process the command
void handleCommandchar command
if command B
Serial.printlnEnter brightness :;
while Serialavailable; Wait for input
brightness Serial.parseInt; Read the brightness
brightness constrainbrightness; Constrain value
analogWriteledPin brightness; Set LED brightness
Serial.printBrightness set to: ;
Serial.printlnbrightness;
Serial.printlnEnter B to set LED brightness ;
Serial.printlnEnter F to set blinking frequency in Hz;
Serial.printlnEnter S to stop blinking.";
else if command F
Serial.printlnEnter blinking frequency Hzfor blinks:";
while Serialavailable; Wait for input
int frequency Serial.parseInt; Read frequency
frequency constrainfrequency; Constrain frequency Hz
Serial.printBlinking at: ;
Serial.printfrequency;
Serial.println Hz;
blinkLedfrequency;
Serial.printlnEnter B to set LED brightness ;
Serial.printlnEnter F to set blinking frequency in Hz;
Serial.printlnEnter S to stop blinking.";
else if command S
Serial.printlnBlinking stopped. LED set to steady brightness.";
analogWriteledPin brightness; Stop blinking and set steady brightness
Serial.printlnEnter B to set LED brightness ;
Serial.printlnEnter F to set blinking frequency in Hz;
Serial.printlnEnter S to stop blinking.";
else
Serial.printlnInvalid command. Try BF or S;
void blinkLedint frequency
int delayTime frequency; Calculate delay time in ms
for int i ; i ; i Blink for cycles
analogWriteledPin brightness;
delaydelayTime;
analogWriteledPin;
delaydelayTime;
analogWriteledPin brightness; Return to steady brightness
Lab Steps
Part : Setting Up the Circuit
Assemble the circuit on the breadboard as described above.
Connect the Arduino to your computer via USB.
Part : 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 : Running the Program
Open the Serial Monitor from the Arduino IDE Ctrl Shift M
Set the baud rate to
Set Serial Monitor to No Line Endingright side of the screen
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
