Night light

My night light will be made out of wood with button cut outs on top. The button cut outs will have a thin layer of vellum paper over. The light will have three major settings, normal light mode, display pattern mode, and night light mode. One can switch around the setting by touching the capacitive sensor. There will also be a photocell that detects the darkness of the room. If the room is dark, the light will turn on to normal light mode automatically. In the nightlight mode, light will gradually fade out after 5 mins, but one can simply turn it back on by gently tapping the top of the light box; the light will still be in night light mode and it will gradually fade up.

Behavior Sketch 

light behavior

 

 

Conceptual Case:

nightlight

 

 

 

Code version 1

#include
CapacitiveSensor cs_4_2 = CapacitiveSensor(4, 2);
long touchVar = cs_4_2.capacitiveSensor(30);
int redPin = 3;
int greenPin = 5;
int photoPin = 0;
int fadePin = 6;
int nightPin = 11;
int nightLevel = 0;
int caseState;
int fadeLevel = 0;
int darkState;
int counter = 0;
boolean fadeUp = false;

void setup() {
// put your setup code here, to run once:
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(fadePin,OUTPUT);
pinMode(nightPin,OUTPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
touchSense();
debug();
}

/touch logic section
int touchSense() {
long start = millis();
long touchVar = cs_4_2.capacitiveSensor(30);
darkState = analogRead(photoPin);
delay(10);
switch (caseState) {
case 0:
digitalWrite(redPin, HIGH);
if (touchVar >= 1000) {
digitalWrite(redPin, LOW);
caseState = 1;
}
break;
case 1:
digitalWrite(greenPin, HIGH);
digitalWrite(nightPin,HIGH);
if (touchVar >= 1000) {
digitalWrite(greenPin, LOW);
digitalWrite(nightPin,LOW);
caseState = 2;
}
break;
case 2:
analogWrite(fadePin, fadeLevel / 2);
fadeLevel++;
if (fadeLevel == 255) caseState = 3;
if (darkState < 250) { fadeLevel = 0; analogWrite(fadePin, fadeLevel); caseState = 4; } if (touchVar >= 1000) {
fadeLevel = 0;
analogWrite(fadePin, fadeLevel);
caseState = 6;
}
break;
case 3:
analogWrite(fadePin, fadeLevel / 2);
fadeLevel--;
if (fadeLevel == 0) caseState = 2;
if (darkState < 250) { fadeLevel = 0; analogWrite(fadePin, fadeLevel); caseState = 4; } if (touchVar >= 1000) {
fadeLevel = 0;
analogWrite(fadePin, fadeLevel);
caseState = 6;
}
break;
case 4:
analogWrite(nightPin, nightLevel);
fadeUp = true;
if (fadeUp == true) nightLevel++;
if (nightLevel == 255) {
fadeUp = false;
caseState = 5;
}
if (darkState > 250) {
nightLevel = 0;
analogWrite(nightPin, nightLevel);
caseState = 2;
}
break;
case 5:
analogWrite(nightPin, nightLevel);
counter++;
if (counter == 3000) {
nightLevel = 0;
analogWrite(nightPin, nightLevel);
caseState = 7;
}
if (darkState > 250) {
nightLevel = 0;
analogWrite(nightPin, nightLevel);
caseState = 2;
}
break;
case 6:
caseState = 0;
break;
case 7:
if(darkState >250) {
counter = 0;
caseState = 2;
}
break;
}
}

//serial monitor display section
int debug() {
Serial.print("caseState =\t");
Serial.print(caseState);
Serial.print("\ttouchVar =\t");
Serial.print(touchVar);
Serial.print("\tfadeLevel =\t");
Serial.print(fadeLevel / 2);
Serial.print("\tdarkState =\t");
Serial.print(darkState);
Serial.print("\tnightLevel =\t");
Serial.print(nightLevel);
Serial.print("\tcounter = \t");
Serial.print(counter);
Serial.println();
}

Leave a reply

Skip to toolbar