Hey guys. I need help coding a night light with a push button that acts as a switch (eg. Push once = off, push again to turn on)
I have the code for dimming a led respective of lighy sensor and ive made a code for turning an led on and off using switch but I cant mix the two. When I tried it would turn the led off and then when I pushed it again it took a reading feom the light sensor and dimmed the led. I want it so it keeps taking readings and dimming the light when on.
Can you please help or tell me what kind of code I need
Sure. Post your code. (use the "#" code tags tool button.)
int ledPin = 2;
int buttonPin = 4;
int lightLevel;
int ledLevel;
boolean currentState = LOW;
boolean lastState = LOW;
boolean ledState = LOW;
void setup(){
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){
Serial.println("pressed");
delay(1);
if (ledState == HIGH){
digitalWrite(ledPin, LOW);
ledState = LOW;
} else {
lightLevel = analogRead(A0);
ledLevel = ((1023 - lightLevel)/4);
Serial.print("light level:");
Serial.println(lightLevel, DEC);
analogWrite (ledPin, ledLevel);;
ledState = HIGH;
}
}
lastState = currentState;
}
I have the code for dimming a led respective of lighy sensor and ive made a code for turning an led on and off using switch but I cant mix the two. When I tried it would turn the led off and then when I pushed it again it took a reading feom the light sensor and dimmed the led. I want it so it keeps taking readings and dimming the light when on.
Can you please help or tell me what kind of code I need
I'm having trouble understanding what you want this program to do
List the criteria for the program below as logical statements
PROGRAM CRITERIA (NIGHTLIGHT)
DO THIS
IF THIS (ie: if button pressed ) DO THAT (ie: read sensor )
etc.
I should be able to look at the list you create, look at the circuit and tell if it is working correctly from the list.
I want a night light that you can turn on and off with a push button. I thought it would be easy but its not.
draw a schematic of how your circuit is wired and take a photo with a cell phone.
Define what you want the button to do.
You talk about dimming the led but it is not clear why the led is dimming.
You have to list your criteria
for example
Operation of led: Led turns on when button is pressed first time , turns off second time or led dims if button is pressed etc.
It is still not clear how this night light is supposed to work. turning a led on and off is easy . If I knew what you needed the light sensor for then it might be clear
Operation of light sensor : for example , etc
If you have a light sensor and a night light , what is the sensor sensing ? Is it sensing daylight turning to evening or what ?
int ledPin = 2;
analogWrite (ledPin, ledLevel);;
pin 2 is not a PWM pin.
PWM pins are 3,5,6,9,10, & 11
Ill try and explain kinda what im after.
I have a light sensor module reading the light. The led is dimmed with an inverse relationship to the light sensor value (eg. light sensor not picking up light led = 100% bright, light sensor picking up 50% light led = 50% brightness, and so one). I want this constantly running until the button is pressed, when the button is pressed i want the led to turn off. when it is pressed again i want the led to go back to the way it was before (dimming according to the light sensor).
i dont know how to upload image either
Can you just use non momentary push button?
understood
boolean currentState = LOW;
boolean lastState = LOW;
boolean ledState = LOW;
boolean
A boolean holds one of two values, true or false. (Each boolean variable occupies one byte of memory.)
analogWrite (ledPin, ledLevel);;
What's wrong with this picture ?
Im not sure. Kinda new to this and was hoping for a script for me to copy and paste and learn from that. Ive looked eveeywhere for a solution but couldnt find one ![]()
Do you know another way to make a momentary button into a on off switch?
Yes but answer the question in my last post.
It only takes one reading them stops?
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
I looked at the link and tried to add the
lightLevel = analogRead(A0);
ledLevel = ((1023 - lightLevel)/4);
Serial.print("light level:");
Serial.println(lightLevel, DEC);
analogWrite (led, ledLevel);
to the end so when the ledState was high it would do the loop but it didnt work again.
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
int lightLevel;
int ledLevel;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 17; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop()
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState)
{
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH)
{
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
Serial.print("ledState:");
Serial.println(ledState);
//delay(1000);
if (ledState == HIGH)
{
digitalWrite(ledPin, HIGH);
ledState = HIGH;
// Serial.print("ledState:");
// Serial.println(ledState);
}
else
{
// ledState = !ledState;
if (ledState ==LOW)
{
lightLevel = analogRead(A0);
ledLevel = ((1023 - lightLevel)/4);
analogWrite (ledPin, ledLevel);
Serial.print("light level:");
Serial.println(lightLevel, DEC);
// delay(1000);
}
//delay(1000);
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
This sketch does what you asked but your premise of using an analogWrite as a "dimmer" doesn't work very well because of the
PWM switching waveform results in flickering when in the "dimmer" mode. The flickering can be reduced somewhat by adding
a capacitor from the + (anode) lead of the led to GND but it cannot be completely eliminate. It does "dim" as ambient light increases and get brighter as ambient light reduces but at the lower light levels it flickers.
The attached file is the sketch above.
Here's a small sketch that performs the SWITCH function you asked for earlier:(see attached)
/* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
int inPin = 4; // the number of the input pin
int outPin = 3; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
Night_Light.ino (3.27 KB)
SWITCH.ino (1.39 KB)
Thabk you very much for all your help. If there a way I can give possitive feedback for you?
Your welcome.
Yes, just click on my karma button on the left of the screen and it will increment the number by one.
All done ![]()