For this project I chose to use two different fade methods; 1. Sin Fade & 2. Exponential Fade (for the interruption) from Tom Igoe’s Light Projects repo on Github. Essentially, the light will fade in congruence with the direction the sin wave until the button is pressed and the fade of the light will be interrupted with an immediate increase to 255 brightness or decrease to 0 brightness. Immediately after an interruption that takes the brightness to 0, there will be exponential increase to the brightness to 255 and then back down to the brightness of the light was the the sin fade (prior to button being pressed). See image of Arduino Plotter and code below:
int led = 5;
int button = 2;
int buttonState = 0;
int lastButtonState = 0;
int brightness = 255;
int shift = 1;
byte levelTable[256];
byte expo[256];
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
fillLevelTable();
fillExpo();
}
void loop() {
buttonState = digitalRead(button);
if (brightness <= 0 || brightness >= 255) {
shift = -shift;
}
brightness += shift;
analogWrite(5, levelTable[brightness]);
delay(10);
Serial.println(levelTable[brightness]);
if (buttonState != lastButtonState) {
if ( buttonState == HIGH) {
for (int brightness = 255; brightness >= 0; brightness += 5 ) {
analogWrite(5, expo[brightness]);
Serial.println(expo[brightness]);
delay(45);
} }
else {
if ( buttonState == LOW) {
for (int brightness = 0; brightness <= 255; brightness += 5 ) {
analogWrite(led, expo[brightness]);
Serial.println(expo[brightness]);
delay(45);
}}
delay(1);
}
lastButtonState = buttonState;
}}
void fillLevelTable() {
float maxValue = 255;
for (int l = 0; l <= maxValue; l++) {
float angle = map(l, 0, maxValue, 0, 179);
float lightLevel = (sin((angle * PI / 180) + PI / 2) + 1) * 127.5;
levelTable[l] = lightLevel;
}}
void fillExpo() {
float maxValue = 255;
float scalingFactor = (maxValue * log10(2)) / (log10(255));
for (int l = 0; l <= maxValue; l++) {
int ltLevel = pow(2, (l / scalingFactor)) - 1;
expo[l] = ltLevel;
}}
I programed an Arduino MKRZERO with the above code and breadboard w/ yellow LED and simple switch to emulate the interaction with the LED.
Once this was in good working operation, I found to learn to explore the junk shelf (aka free store) for inspiration. With the requirement being that a light source could not be seen, I was immediately lit up by finding a fiber optic piece from a toy that I could easily glue to an LED. I found a square pushbutton switch as well as a red LED in my tool box and began to solder the light and switch to header pins to connect to the breadboard and MKR Zero I used to program.
I wanted to package this up nicely so I used Make-A-Box IO to quickly draft up a box image that I could later use laser cut and fabricate together. I took the plan to Adobe Illustrator where I added holes for the components. I decided against drafting a hole for a cable as I was planning on using a litho battery but realized afterwards that I needed a larger battery so later drilled a hole in the box side to accommodate.
Completed project below:
Full list of materials:
(1) full-sized breadboard
(1) Arduino MKRZERO
(1) LED
(1) Button Switch
(1) Fiber Optic toy
(1) piece 12 x 12 Pine Plywood
(1) Micro USB cable
(#) wires to connect
(#) tape
(#) wood glue