Proyek bawaan · Arduino Uno R3

Lampu tidur di Arduino Uno

Lampu makin terang saat pin sensor menjadi gelap, dengan tombol tekan sebagai pengganti sensor cahaya. Seluruh rakitannya, Arduino Uno dan 5 komponen lainnya, berjalan di browsermu dengan firmware di bawah; buka di editor untuk mengubah rangkaian atau kodenya, lalu jalankan lagi.

Mengajar? Berikan sebagai tugas ke kelasmu dengan sekali klik.

Pemula Berjalan di browsermu. Gratis, dan tidak perlu akun.

Komponennya dijual sekitar $32. Di Mokxi, semuanya gratis.

Lampu tidur · Arduino Uno R3live0.000 s 0.00x
Tahan tombolnya
Rangkaiannya sendiri, berjalan di simulator di sini. Tekan yang bisa ditekan; klik yang lainnya untuk membukanya di editor.

Kodenya

Firmware persis seperti yang dibuka di editor. Ubah satu baris di sana lalu tekan Jalankan: kodenya dikompilasi di browser.

sketch.ino · Arduino Uno R3
// 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);
}

Daftar komponen

7 komponen, ditambah kabel jumper. Harganya sekitar $32; di Mokxi, semuanya gratis.

Cara merangkainya

6 sambungan, pin demi pin, dibaca langsung dari rangkaiannya. Setiap baris adalah satu set pin yang tersambung, lewat kabel jumper atau jalur breadboard.

  • Ground: Arduino Uno R3 pin GND; Tombol tekan pin 2a; LED, kuning pin C; LED, putih pin C
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 8; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 2; Tombol tekan pin 1a
  • Resistor, 220 Ω (1) pin 2; LED, kuning pin A
  • Resistor, 220 Ω (2) pin 2; LED, putih pin A

Ubah dan simpan

Buka di editor, ubah rangkaian atau kodenya, lalu simpan versimu di akun gratis.