0% found this document useful (0 votes)
69 views1 page

Lab Week 9: Void Loop (Digitalwrite (Led - Builtin, High) Delay (833) Digitalwrite (Led - Builtin, Low) Delay (167) )

The document discusses blinking an LED at 0.5 Hz and controlling LEDs using UART serial interface. It also covers debouncing a switch using an interrupt attached to the switch pin. The assignment code uses a switch to toggle an LED that blinks a set number of times each time the switch is pressed.

Uploaded by

Kunal Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views1 page

Lab Week 9: Void Loop (Digitalwrite (Led - Builtin, High) Delay (833) Digitalwrite (Led - Builtin, Low) Delay (167) )

The document discusses blinking an LED at 0.5 Hz and controlling LEDs using UART serial interface. It also covers debouncing a switch using an interrupt attached to the switch pin. The assignment code uses a switch to toggle an LED that blinks a set number of times each time the switch is pressed.

Uploaded by

Kunal Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Lab Week 9

1.3: Blinking frequency = 1 / (ton+toff) = 1/2 = 0.5 Hz.


1.4: ton=0.2toff, ton+toff=1/freq=1, 0.2toff+toff = 1  toff =1/1.2 ≈ 0.833s, ton ≈ 0.167s

void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(833);
digitalWrite(LED_BUILTIN, LOW);
delay(167);
}

2.2: Hello World shows up on the serial monitor.


2.4: UART serial interface is used to control the LEDs. The code checks each received
character as follows:
'1' : to turn on LED0.
'2' : to turn off LED0.
'3' : to turn on LED1.
'4' : to turn off LED1.

3.2: Each time the switch is pressed the on-board LED toggles. The existence of switch
bouncing can be expected.

3.3: ASSIGNMENT

#define BLINK_NUM 5
#define SW 2

volatile unsigned int led_state = LOW;


volatile unsigned int sw_flag = 0;
volatile unsigned int cnt = 0;

void setup() {
pinMode(LED_BUILTIN,OUTPUT);
pinMode(SW,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SW),fToggle,FALLING);
digitalWrite(LED_BUILTIN,led_state);
}

void loop() {
if (sw_flag) {
if (cnt<2*BLINK_NUM) {
digitalWrite(LED_BUILTIN,led_state);
delay(500);
led_state = !led_state;
cnt++;
} else {
sw_flag = 0;
}
}
}

void fToggle() {
sw_flag = 1;
cnt = 0;
}

3.4: Switch debouncing using delay(). The LEDs toggle each time the switch is pressed.
4.2: The on-board LED's brightness can be controlled by the potentiometer.

You might also like