Luz noturna num Arduino Uno
Uma lâmpada vai acendendo quando o pino do sensor fica escuro, com um botão fazendo o papel do sensor de luz. A montagem inteira, um Arduino Uno e mais 5 componentes, roda aqui no seu navegador com o firmware abaixo; abra no editor para mudar a ligação ou o código e rodar de novo.
Dá aula? Passe para a sua turma com um clique.
Iniciante Roda no seu navegador. Grátis, e não precisa de conta.
Os componentes custam cerca de $32 nas lojas. No Mokxi eles são grátis.
O código
O firmware exatamente como o editor abre. Mude uma linha lá e aperte Rodar: ele compila no navegador.
// Night light: the lamp fades up when it goes dark and down when it is light.
//
// pin 2 the light sensor, to GND, internal pull-up
// pin 9 the lamp, through 220 ohm to GND
// pin 8 the indicator, through 220 ohm to GND
//
// The sensor here is a pushbutton, standing in for the light-dependent
// resistor a real night light has. Mokxi has an LDR part, but a button is
// easier to hold, and the two behave the same way as far as the sketch is
// concerned: the pin is high while there is light and low while there is not.
// Hold the button and the room is dark.
//
// The lamp is on pin 9, which is one of the ATmega328P's PWM outputs, so
// analogWrite can give it any brightness from 0 to 255. It walks there rather
// than jumping: a night light that snaps on is a torch, and the ramp is what
// makes it a night light. Eight counts every twenty milliseconds is about six
// tenths of a second end to end.
//
// The indicator on pin 8 is a plain digital output and says what the sensor is
// reading right now, so you can see the input and the output disagree while
// the lamp is still on its way.
const int SENSOR = 2;
const int LAMP = 9;
const int INDICATOR = 8;
const int STEP = 8;
const int FULL = 255;
int level = 0;
static bool dark() { return digitalRead(SENSOR) == LOW; }
void setup() {
pinMode(SENSOR, INPUT_PULLUP);
pinMode(LAMP, OUTPUT);
pinMode(INDICATOR, OUTPUT);
analogWrite(LAMP, 0);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: night light");
Serial.println("the button stands in for a light sensor: hold it for dark");
}
void loop() {
bool night = dark();
digitalWrite(INDICATOR, night ? HIGH : LOW);
int want = night ? FULL : 0;
if (level < want) {
level += STEP;
if (level > want) {
level = want;
}
} else if (level > want) {
level -= STEP;
if (level < want) {
level = want;
}
}
analogWrite(LAMP, level);
static int said = -1;
if (level == want && level != said) {
said = level;
Serial.print(night ? "dark: lamp at " : "light: lamp at ");
Serial.println(level);
}
delay(20);
}
Lista de componentes
7 componentes, mais os jumpers. Eles custam cerca de $32 nas lojas; no Mokxi são grátis.
- 1 × Arduino Uno R3
- 1 × Protoboard grande
- 1 × Botão
- 2 × Resistor, 220 Ω
- 1 × LED amarelo
- 1 × LED branco
Como está ligado
6 ligações, pino por pino, lidas do próprio circuito. Cada linha é um conjunto de pinos ligados entre si, por um jumper ou por uma fileira da protoboard.
- Terra: Arduino Uno R3 pino GND; Botão pino 2a; LED amarelo pino C; LED branco pino C
- Arduino Uno R3 pino 9; Resistor, 220 Ω (1) pino 1
- Arduino Uno R3 pino 8; Resistor, 220 Ω (2) pino 1
- Arduino Uno R3 pino 2; Botão pino 1a
- Resistor, 220 Ω (1) pino 2; LED amarelo pino A
- Resistor, 220 Ω (2) pino 2; LED branco pino A
Mude e guarde
Abra no editor, mude o circuito ou o código e guarde a sua versão numa conta grátis.