Arduino Nano GPIO Arduino vs. AVR
Embedded
A push button controls the mode of LEDs (on/off)…
- All LEDs starts to glow when the device is powered on.
- Pressing the button switches the LED on D12 off.
- Pins D[3,10,12] are each connected to ground (0V) via a resistor (300-600Ω) and an LED.
- Pin D8 is connected to the supply voltage (5V) over a simple push button.
- Optional: Connect the button with a debounce circuit using a 10kΩ resistor and a 100nF capacitor wired in parallel to ground (0V).
Implementation…
#include <Arduino.h>
#define _DEBUG
#ifdef _DEBUG
#define DEBUG_PRINT(txt) Serial.print(txt);
#define DEBUG_PRINTLN(txt) Serial.println(txt);
#define DEBUG_VALUE(txt,val) Serial.print(txt); Serial.print(": "); Serial.println(val);
#define DEBUG_BIN(txt,val) Serial.print(txt); Serial.print(": 0b"); Serial.println(val,BIN);
#else
#define DEBUG_PRINT(txt)
#define DEBUG_PRINTLN(txt)
#define DEBUG_VALUE(txt,val)
#define DEBUG_BIN(txt,val)
#endif
#define SERIAL_BAUDRATE 9600
#define D3 3 // label pin D3
void setup() {
.begin(SERIAL_BAUDRATE);
Serial
// Set pin D3 (PD3) as output
(D3,OUTPUT);
pinMode// set pins D10 (PB2) and D12 (PB4) as output
// all other pins including D8 (PB1) as input
= 0b0010100;
DDRB
// set pin D3 high (1)
(D3,HIGH);
digitalWrite// set pins D10 (PB2) and D12 (PB4) high (1)
= 0b0010100;
PORTB
}
void loop() {
// print the value of D8
("D8",digitalRead(8));
DEBUG_VALUE// print the values of pins at port B
("PINB", PINB);
DEBUG_BIN
// read the value of D8
int D8 = 0;
= PINB & 0x01; // 0b00000001 in HEX
D8 if (D8 == 1) {
// set pin D3 low (0)
(D3,LOW);
digitalWrite// set only pin D12 (PB4) on port B low
&= ~(1<<PB4);
PORTB
} else {
// set all pins on port D low
// except of pin D3 high (1)
= 0b00001000;
PORTD // set only pin D12 high (1)
|= 1<<PB4;
PORTB
}
// print the state of the port B register
("PORTB", PORTB);
DEBUG_BIN(100);
delay
}