⏱️ 35 min 🧩 Servo 1 — D9 (Porta) · Servo 2 — D12 (Janela)
No Arduino IDE: Ferramentas → Gerir Bibliotecas → pesquisa "Servo" → instala "Servo by Arduino". A biblioteca já vem incluída no IDE desde a versão 1.x.
| Módulo | Pino Arduino (Sinal) | Função |
|---|---|---|
| 🚪 Servo 1 | D9 | Porta — 0°=fechado, 90°=aberto |
| 🪟 Servo 2 | D12 | Janela — 0°=fechada, 90°=aberta |
Se o Arduino reiniciar ao mover o servo é porque falta corrente. Nesse caso alimenta a maqueta pelas pilhas AA (9V no jack) em vez do USB — o regulador interno do Arduino aguenta melhor a carga dos 2 servos.
#include <Servo.h>
#define BOTAO 7
#define PINO_PORTA 9
Servo porta;
bool aberta = false;
void setup() {
pinMode(BOTAO, INPUT_PULLUP);
porta.attach(PINO_PORTA);
porta.write(0); // Começa fechada
}
void loop() {
if (digitalRead(BOTAO) == LOW) {
aberta = !aberta;
porta.write(aberta ? 90 : 0);
delay(400); // Anti-bounce
}
}#include <Servo.h>
#define BOTAO 7
#define PINO_PORTA 9
#define PINO_JANELA 12
#define LDR A0
Servo porta;
Servo janela;
void setup() {
pinMode(BOTAO, INPUT_PULLUP);
porta.attach(PINO_PORTA);
janela.attach(PINO_JANELA);
porta.write(0);
janela.write(0);
Serial.begin(9600);
}
void loop() {
// Botão controla a porta
static bool portaAberta = false;
if (digitalRead(BOTAO) == LOW) {
portaAberta = !portaAberta;
porta.write(portaAberta ? 90 : 0);
delay(400);
}
// LDR controla a janela (fecha quando escurece)
int luz = analogRead(LDR);
if (luz > 600) { // Escuro → fecha janela
janela.write(0);
} else { // Claro → abre janela
janela.write(90);
}
}#include <Servo.h>
Servo porta;
void abrirSuave() {
for (int pos = 0; pos <= 90; pos++) {
porta.write(pos);
delay(15);
}
}
void fecharSuave() {
for (int pos = 90; pos >= 0; pos--) {
porta.write(pos);
delay(15);
}
}
void setup() {
porta.attach(9);
porta.write(0);
}
void loop() {
abrirSuave();
delay(2000);
fecharSuave();
delay(2000);
}