Hello, I'd like to make a piece where a pull switch is pulled and turns a light off. After a 2-second delay, the light fades back to full brightness without the switch being pulled again. So, the light never actually turns off.
I'm new to Arduino and want to get started on the correct path.
Does it immediately start fading back to on, reaching full brightness in 2 sec? Or does it stay off for 2 sec, and then fade back to full in another X seconds?
One of the tricks light dimmers use is Pulse Width Modulation (PWM) where they quickly switch it on and off with a variable fraction of off-time and on-time. Dim lights are on for low percentages of the time, and brighter light are on longer.
What sort of light are you thinking of? A normal light bulb? A car light bulb? Or for programming purposes, a simple low voltage LED?
First build the lamp and make sure it meets your expectations for the display location (brightness, looks, etc.). Figure out what switch you want to use, and add that to the lamp. Pull chain switches are available at home improvement stores.
The design of the control circuit depends on what type of light you finally select.
Dimming & controlling depends on the type of light. If you are using a screw-in AC LED light bulb, make sure it's dimmable. (All regular-ole incandescent bulbs are dimmable).
So... You need to read the switch state in a loop waiting the switch to close (or open) and when the switch is activated (probably an if-statement) it goes through the turn-off and dim-up sequence, then starts the main loop over again.
Start by making it go-off when you pull the switch and stay-off while the switch is pulled. Then add a delay to hold off for a few seconds. Then replace the delay with the fade-up.
Buy a breadboard, a momentary button, a few resistors, a few mosfets (logic level) a few LEDs and a bench top power supply. Should get change from 50 of whatever your currency is!
If you are just starting then you will want to connect the minimum stuff first such as a single led and work through examples such as “blink without delay”. When you have a basic understanding of turning an LED off and on without using blocking code you can move on the PWM (fade on/off).
Do the same process with a button always starting a new blank sketch from scratch with a good name. You should complete with a good understanding of debouncing and be able to create a button polling function.
In your final code you will combine the two in a new sketch with appropriate name. In loop you will call your button polling function and if the button is pushed it will trigger your light function and a millis timer.
Iterative approach saves time and a mess of code. You should have lots of iterations with great names showing a gradual but steady progress towards your final complete project. Resist the urge to just mush everything together and try and do the whole thing in one go.
You will get lots of good advice here and when the project moves from breadboard to real life there will be only minor tweaks and tidying to do which will make your project great in the real world
I would suggest starting with the Arduino File/Examples/Analog/Fading as a base to get started:
/*
Fading
This example shows how to fade an LED using the analogWrite() function.
The circuit:
- LED attached from digital pin 9 to ground through 220 ohm resistor.
created 1 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fading
*/
int ledPin = 9; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Modifying it to use an extra variable to be more than "off" for X seconds:
for (int fadeValue = -255 ; fadeValue <= 255; fadeValue += 5) {
int lightValue;
// sets the value (range from 0 to 255):
if (fadeValue >0){
lightValue = fadeValue;
} else {
lightValue = 0;
}
analogWrite(ledPin, lightValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
...replace the delay and for-loop counting logic with a millis() type timer from File/Examples/Basics/ BlinkWithoutDelay; add a button and some logic kick directly into Off mode or stay in full on mode.