VIDEO
Some time ago born my baby boy. And when he sleep, sometimes needed to go to room in night time. Switch light I don't want, and I bought cheap lamp on batteries. But it was not comfortable, every time needed to search switch, and it was so bright. I decided to remake it .
Main requirements i needed.
- rechargeable batteries
- motion sensor
- light bright control
- motion activation time control
After small research of my shelf with electronic parts i found Arduino pro mini, sensor button, and PIR sensor.
I started remake project.
First I decided what main features I need to realize in controller code. They are:
- Working from batteries or power supply - that means I need to use deep sleep on controller, and use PIR sensor to interrupt sleep and wake up controller.
- Sensor button to configure brightness and time - why sensor button, I don't want to make additional hole in lamp enclosure.
- Sensor button power need to be activated only when controller waked up.
- PIR to waking up controller
- Saving time and brightness to EEPROM
- Simple menu on one button to configure time and brightness. - I think it will be done with leds on lamp. First led "dimming menu" , second third and fourth brightness value. Last led "timing menu", first second and third time interval value.
In bottom you can find project code, and on top of the page is video of making this light.
Code
SZ_BNightLight.ino
#include "GyverTimer.h"
#include <GyverPower.h>
#include "GyverButton.h"
#include <EEPROM.h>
//LEDS 4x2
#define led1 5
#define led2 6
#define led3 9
#define led4 10
//Inputs
#define pir 3
#define sens 8
#define sens_pwr 12
GButton sensor(sens);
int menu = 0;
int isAwake = 0;
int dimming;
String dimming_s;
String timing;
bool menu_true = true;
GTimer LedTime(MS);
void(* resetFunc) (void) = 0; //Reset function
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode(sens_pwr, OUTPUT);
pinMode(pir, INPUT_PULLUP);
pinMode(sens, INPUT_PULLUP);
attachInterrupt(1, isr, FALLING);
power.setSleepMode(POWERDOWN_SLEEP);
//Sensor button settings
sensor.setDebounce(100);
sensor.setTimeout(500);
sensor.setClickTimeout(501);
sensor.setType(LOW_PULL);
defaultEEPROM();
dimming = read_string(3, 1).toInt();
timing = read_string(3, 5);
//Led lightening interval
LedTime.setInterval(timing.toInt() * 1000);
}
void isr() {
power.wakeUp();
}
void loop() {
digitalWrite(sens_pwr, LOW);
power.sleep(SLEEP_FOREVER);
digitalWrite(sens_pwr, HIGH);
isAwake = 1;
LedTime.start();
while (true) {
switch (isAwake) {
case 1: // Default Function
default_f();
break;
case 2: //Menu
menu_f();
break;
case 3://Dimming setting
dimming_f();
break;
case 4: //Timing Function
timing_f();
break;
}
}
}
default_f.ino
void default_f() {
while (true) {
sensor.tick();
digitalWrite(sens_pwr, HIGH);
analogWrite(led1, dimming);
analogWrite(led2, dimming);
analogWrite(led3, dimming);
analogWrite(led4, dimming);
if (sensor.isHolded()) {
isAwake = 2;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
break;
}
if (LedTime.isReady()) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(sens_pwr, LOW);
power.sleep(SLEEP_FOREVER);
}
}
}
dimming_f.ino
void dimming_f() {
while (true) {
sensor.tick();
analogWrite(led1, dimming);
if (dimming == 36) {
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
analogWrite(led4, dimming);
dimming_s = "036";
}
if (dimming == 72) {
digitalWrite(led2, LOW);
analogWrite(led3, dimming);
digitalWrite(led4, LOW);
dimming_s = "072";
}
if (dimming == 108) {
digitalWrite(led2, LOW);
analogWrite(led3, dimming);
analogWrite(led4, dimming);
dimming_s = "108";
}
if (dimming == 144) {
analogWrite(led2, dimming);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
dimming_s = "144";
}
if (dimming == 180) {
analogWrite(led2, dimming);
digitalWrite(led3, LOW);
analogWrite(led4, dimming);
dimming_s = "180";
}
if (dimming == 216) {
analogWrite(led2, dimming);
analogWrite(led3, dimming);
digitalWrite(led4, LOW);
dimming_s = "216";
}
if (dimming == 252) {
analogWrite(led2, dimming);
analogWrite(led3, dimming);
analogWrite(led4, dimming);
dimming_s = "252";
}
if (sensor.isClick()) {
if (dimming <= 252) {
dimming = dimming + 36;
}
if (dimming > 253) {
dimming = 36;
}
}
if (sensor.isHolded()) { ////////need function
write_EEPROM(dimming_s, 1);
analogWrite(led1, dimming);
analogWrite(led2, dimming);
analogWrite(led3, dimming);
analogWrite(led4, dimming);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
resetFunc();
}
break;
}
}
eeprom_f.ino
//////////////////////////Read String EEPROM//////////////////////////
String read_string(int l, int p) {
String temp;
for (int n = p; n < l + p; ++n)
{
if (char(EEPROM.read(n)) != ';') {
if (isWhitespace(char(EEPROM.read(n)))) {
//do nothing
} else temp += String(char(EEPROM.read(n)));
} else n = l + p;
}
return temp;
}
void write_EEPROM(String x, int pos) {
for (int n = pos; n < x.length() + pos; n++) {
EEPROM.write(n, x[n - pos]);
}
}
void defaultEEPROM() { // Check for default donfig
if (read_string(1, 0) != "0") {
write_EEPROM("0", 0); //Save default config on first start
write_EEPROM("252", 1); //Dimming
write_EEPROM("005", 5); //Timing
delay(1000);
resetFunc();
}
}
menu_f.ino
void menu_f() {
while (true) {
sensor.tick();
if (menu == 0) {
analogWrite(led1, dimming);
digitalWrite(led4, LOW);
}
if (menu == 1) {
analogWrite(led4, dimming);
digitalWrite(led1, LOW);
}
if (sensor.isClick()) {
menu = !menu;
}
if (sensor.isHolded()) {
if (menu == 0) {
isAwake = 3;
break;
}
if (menu == 1) {
isAwake = 4;
break;
}
}
}
}
timing_f.ino
void timing_f() {
while (true) {
sensor.tick();
analogWrite(led4, dimming);
if (timing == "005") {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
analogWrite(led3, dimming);
}
if (timing == "010") {
digitalWrite(led1, LOW);
analogWrite(led2, dimming);
digitalWrite(led3, LOW);
}
if (timing == "015") {
digitalWrite(led1, LOW);
analogWrite(led2, dimming);
analogWrite(led3, dimming);
}
if (timing == "030") {
analogWrite(led1, dimming);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
if (timing == "060") {
analogWrite(led1, dimming);
digitalWrite(led2, LOW);
analogWrite(led3, dimming);
}
if (timing == "300") {
analogWrite(led1, dimming);
analogWrite(led2, dimming);
digitalWrite(led3, LOW);
}
if (timing == "600") {
analogWrite(led1, dimming);
analogWrite(led2, dimming);
analogWrite(led3, dimming);
}
if (sensor.isClick()) {
if (timing == "005") {
timing = "010";
}
else if (timing == "010") {
timing = "015";
}
else if (timing == "015") {
timing = "030";
}
else if (timing == "030") {
timing = "060";
}
else if (timing == "060") {
timing = "300";
}
else if (timing == "300") {
timing = "600";
}
else if (timing == "600") {
timing = "005";
}
}
if (sensor.isHolded()) { ////////need function
write_EEPROM(timing, 5);
analogWrite(led1, dimming);
analogWrite(led2, dimming);
analogWrite(led3, dimming);
analogWrite(led4, dimming);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
resetFunc();
}
}
}
Code
Code download link
Libraries used in project https://github.com/AlexGyver/GyverLibs