[{"data":1,"prerenderedAt":87},["ShallowReactive",2],{"example-page-tiny-nightlight":3},{"id":4,"info":5,"parts":6,"wired":23,"refs":24,"nets":25,"moreNets":81,"code":82},"tiny-nightlight","An ATtiny85 on a 3 V battery: the light sensor RV1 (LDR to GND, R1 10 kΩ to VCC) on A3 (PB3) tells how dark it is. Below about 10 lx the LED on PB0 fades up (PWM from Timer0), above it fades down. Button SW1 on PB2 switches auto \u002F always on \u002F off (pin-change interrupt). While the LED is off the chip sleeps in power-down and the watchdog wakes it every 250 ms: watch the supply current drop to microamps. Change the illuminance of the LDR in the inspector during the run.",[7,10,13,15,17,19,21],[8,9],"resistor",2,[11,12],"attiny85",1,[14,12],"battery",[16,12],"button",[18,12],"capacitor",[20,12],"ldr",[22,12],"led",false,{"BT1":14,"C1":18,"D1":22,"RV1":20,"SW1":16,"U1":11,"R1":8,"R2":8},[26,45,55,61,68,74],{"name":27,"pins":28},"0",[29,32,35,38,40,42],[30,31],"BT1","−",[33,34],"C1","2",[36,37],"D1","K",[39,34],"RV1",[41,34],"SW1",[43,44],"U1","GND",{"name":46,"pins":47},"VCC",[48,50,52,54],[30,49],"+",[33,51],"1",[53,51],"R1",[43,46],{"name":56,"pins":57},"BTN",[58,59],[41,51],[43,60],"PB2 SCL SCK A1",{"name":62,"pins":63},"LED",[64,66],[65,51],"R2",[43,67],"PB0 ~ SDA MOSI",{"name":69,"pins":70},"LED_A",[71,73],[36,72],"A",[65,34],{"name":75,"pins":76},"SENSE",[77,78,79],[53,34],[39,51],[43,80],"PB3 A3",0,{"file":83,"lang":84,"text":85,"lines":86,"more":23},"sketch.ino","cpp","\u002F\u002F ATtiny85 night light\n\u002F\u002F  - A3 (PB3): light sensor, an LDR to GND and 10 kΩ to VCC: the darker, the higher the voltage\n\u002F\u002F  - PB0: LED (PWM, Timer0); it fades up when it gets dark and down when it gets light\n\u002F\u002F  - PB2: button to GND: auto -> always on -> off -> auto (pin-change interrupt)\n\u002F\u002F While the LED is off the chip sleeps (power-down) and the watchdog wakes it every 250 ms to\n\u002F\u002F look at the light again: the supply current drops from milliamps to microamps.\n#include \u003Cavr\u002Fsleep.h>\n#include \u003Cavr\u002Fwdt.h>\n\nconst byte LED = 0, BUTTON = 2, SENSOR = A3;\nbyte mode = 0;                 \u002F\u002F 0 auto, 1 always on, 2 off\nint level = 0;                 \u002F\u002F LED brightness 0..255\nvolatile bool pressed = false;\n\nISR(PCINT0_vect) { if (!(PINB & _BV(PB2))) pressed = true; }\nISR(WDT_vect) {}\n\nvoid setup() {\n  pinMode(LED, OUTPUT);\n  pinMode(BUTTON, INPUT_PULLUP);\n  GIMSK |= _BV(PCIE);          \u002F\u002F pin-change interrupt on PB2\n  PCMSK |= _BV(PCINT2);\n  sei();\n}\n\nvoid sleep250ms() {\n  wdt_reset();\n  WDTCR = _BV(WDCE) | _BV(WDE);\n  WDTCR = _BV(WDIE) | _BV(WDP2);           \u002F\u002F watchdog interrupt after 250 ms\n  set_sleep_mode(SLEEP_MODE_PWR_DOWN);\n  sleep_enable();\n  sleep_cpu();                             \u002F\u002F wakes on the watchdog or the button\n  sleep_disable();\n  WDTCR = _BV(WDCE) | _BV(WDE);\n  WDTCR = 0;                               \u002F\u002F watchdog off\n}\n\nvoid loop() {\n  if (pressed) { delay(30); pressed = false; mode = (mode + 1) % 3; }\n  bool dark = analogRead(SENSOR) > 600;\n  int target = (mode == 1 || (mode == 0 && dark)) ? 255 : 0;\n  if (level \u003C target) level++;\n  else if (level > target) level--;\n  analogWrite(LED, level);\n  if (level == 0 && target == 0) { digitalWrite(LED, LOW); sleep250ms(); }\n  else delay(8);                           \u002F\u002F a full fade takes about 2 s\n}",47,1796304400955]