In: Computer Science
Write the following in C language for Arduino:
Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change.
//analogWrite(0) means a signal of 0% duty cycle.
//analogWrite(255/4=64)means a signal of 25% duty cycle
//analogWrite(255/2=127) means a signal of 50% duty cycle.
//analogWrite(255*3/4=191) means a signal of 100% duty cycle
//analogWrite(255) means a signal of 100% duty cycle
//Initializing LED Pin
int led_pin = 6;//connect led to 6th pin
void setup() {
//Declaring LED pin as output
pinMode(led_pin, OUTPUT);
}
void loop() {
//The Arduino IDE has a built in function “analogWrite()” which can
be used to generate a PWM signal.
//The frequency of this generated signal for most pins will be
about 490Hz and we can give the value from 0-255 using this
function.
analogWrite(led_pin, 255/4);//for 25% brightness
delay(1);//delay 1 second
analogWrite(led_pin, 255/2);//for 50%brightness
delay(1);//delay 1 second
analogWrite(led_pin, (255*3)/4);//for 75% brightness
delay(1);//delay 1 second
analogWrite(led_pin, 255);//for 100% brightness
delay(1);//delay 1 second
analogWrite(led_pin,0); //for 0% brightness
delay(1);//delay 1 second
}
code compiled: