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.