Check yourself
Try to answer each question before you open its answer.
About the code
What does
digitalRead(2)give back?Show the answer
HIGHorLOW. It reads pin 2 and says which of the two it is. 5Why does the button need a resistor to GND?
When does the
elsepart run?
About the registers
These are about step 4. If you skipped it, you can skip them too.
Which register bit holds what pin 2 reads?
What does
DDRD &= ~(1 << DDD2);do?Show the answer
It makes pin 2 an input, as
pinMode(2, INPUT)does.pinMode(2, INPUT)clears bit 2 of DDRD, and bit 2 of PORTD too. 2
That was the last lesson. Next, try the missions, or write your own sketch on the Write code page.
All sources for Lesson 4
- ArduinoCore-avr, variants/standard/pins_arduino.h: pins 0 to 7 are port D; pin 2 is PD2; pin 13 is PB5
- ArduinoCore-avr, cores/arduino/wiring_digital.c: pinMode and digitalRead
- Arduino example: Button.ino: public domain
- Arduino tutorial: How to Wire and Program a Button: pull-down resistor; a floating pin
- Arduino reference: digitalRead
- Arduino reference: pinMode
- Arduino reference: digitalWrite
- Arduino reference: if
- Arduino reference: else
- Arduino reference: == (equal to)
- Arduino reference: setup()
- Arduino reference: loop()
- avr-libc, include/avr/iom328p.h: PIND, DDRD and PORTD, and their bit names