Please Help :) Beginner tries to make a day/night switch with button switch to..

Hello all,

I am very new to arduino en trying to get a little project to work. I guess I need some help because I don't understand what I am doing wrong..

What is my goal?
I have a bar-lamp out in my garden. I want it to switch on when it is getting dark in de evening and switch off in de morning when it is getting light. I also want to be able to switch it on and off manually with a button...

I made this circuit as seen in the picture.

I mixed up two codes I found on the internet and produced this:

const int ledPin=3; //the code will flash the LED connected to pin 3
const int sensorPin= 0; //Sensor pin connects to analog pin A0
const int buttonPin = 2; // the number of the pushbutton pin
int level; //the variable that will hold the light level reading
const int threshold=675; //this represents the threshold voltage. If voltage is below 150, this triggers the LED to turn on
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
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); //sets digital pin 3 as output
digitalWrite(ledPin, ledState);
Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the sensor is obtaining on the Serial Monitor
}
void loop(){
level= analogRead(sensorPin); //the sensor takes readings from analog pin A0
if (level < threshold){
digitalWrite(ledPin, HIGH); //if the light level is below the threshold level, the LED turns on
}
else{
digitalWrite(ledPin, LOW); //otherwise, if the light level is above the threshold level, the LED is off
}
{

  • // 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;*
    }
    }

When I press the button the relay will switch. That's oke. I see the led shine bright and I hear the Relay switch.
When I cover the Photosensor with my hand to darken it I see the led on the relay starting shine but not as bright. And the relay don't switch..
I tried several digital outputs, non of them work. Does de relay doesn't get enough power to switch? And if, please tell me why :slight_smile:

Its driving me crazy haha

Plan B:

Arduino Yun/Yun Shield

http://forum.arduino.cc/index.php?topic=288769.msg2024203#msg2024203

Wrote python script:

  • retrieve sunrise, sunset time from database ( Naval, US)
  • remove all crontab with comment='LED'
  • set up crontab with comment='LED' with sunrise, sunset time and 14 hours in total or what ever business logic.
  • No more photo sensor or false trigger by sun eclipse or bird...

Run this python script by crontab at early morning say 2:00 AM every day.

Please modify your Post and use the code button </> so your code looks like this and can easily be copied to a text editor. Then I will have a look at it.

I suspect a Yun is an expensive option for this project - unless you already have one.

...R

Hi Robin.
Thank you very much. That would be very usefull! I hope you see what Im doing wrong..
This is the code:

const int ledPin=3; //the code will flash the LED connected to pin 3
const int sensorPin= 0; //Sensor pin connects to analog pin A0
const int buttonPin = 2;    // the number of the pushbutton pin

int level; //the variable that will hold the light level reading

const int threshold=675; //this represents the threshold voltage. If voltage is below 150, this triggers the LED to turn on

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

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); //sets digital pin 3 as output

digitalWrite(ledPin, ledState);

Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the sensor is obtaining on the Serial Monitor
}

void loop(){
level= analogRead(sensorPin); //the sensor takes readings from analog pin A0
if (level < threshold){
digitalWrite(ledPin, HIGH); //if the light level is below the threshold level, the LED turns on
}
else{
digitalWrite(ledPin, LOW); //otherwise, if the light level is above the threshold level, the LED is off
}
{
  // 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;
}
}

Hi Sonnyyu.

Thank you for you reaction. That looks very cool. The guy with the chicken coop has an nice project in mind. I have a simular goal why I started with arduino. I have two aquariums. I have seen a lot of videos on youtube from people who are using the arduino for there aquarium. They to have a slow sunrise, bright midday en in the evening a sunset. They have feeding programs were the aquariumpump is shutdown and then a automatic feeder starts. If I get better at this, this is my ultimate goal. I guess I have a long way ahead haha.

Thanks for your advice about the Yun Shield. But I guess that needs a lot of more code-work. First I want to understand the simple stuff. But thanks for you reaction :slight_smile:

IMG_20150828_171631.jpg

I tidied up your code with better indenting so I could make sense of it.

const int ledPin=3; //the code will flash the LED connected to pin 3
const int sensorPin= 0; //Sensor pin connects to analog pin A0
const int buttonPin = 2;    // the number of the pushbutton pin

int level; //the variable that will hold the light level reading

const int threshold=675; //this represents the threshold voltage. If voltage is below 150, this triggers the LED to turn on

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

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); //sets digital pin 3 as output

    digitalWrite(ledPin, ledState);

    Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the sensor is obtaining on the Serial Monitor
}

void loop(){
    level= analogRead(sensorPin); //the sensor takes readings from analog pin A0
    if (level < threshold){
        digitalWrite(ledPin, HIGH); //if the light level is below the threshold level, the LED turns on
    }
    else{
        digitalWrite(ledPin, LOW); //otherwise, if the light level is above the threshold level, the LED is off
    }
    // 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;
}

You seem to be setting the same ledPin in two different parts of your code without having any system to prevent one part interfering with the other. LInes 29 and 32 and also at line 63.

I think you need to re-consider your logic.

...R

Hi Robin,

Thanks a lot! That's an eye-opener. Offcourse :slight_smile: At one point in the code I make the pin set to high and in a different part I set it to low. That makes sense. Guess that will be the problem. Im gonna try it this evening when I get home. I will let you know. Thanks for rewriting the code!!!

Greets Korné

Hi Robin,

Thanks again for looking into it. Its still not working. I cant figure it out sadly :frowning:

I tried several options.
I did set two different digital pins as an output. One for the ldr-sensor and one for the button. With a led that worked a little. in light i was able to switch on the led with the button. In dark I was unable to switch the led off with the button.. When i replaced the led with a relay, the sensor was still not able to switch the relay on. I guessed the the button wire did function as a ground wire to it with no power on it.

I tried to use N mosfet's between the two digital out and the relay. Still not working :frowning:
I spend another 5 hours on this braincracker.. I guess I start with some even more basic arduino project and hopefully I will figure it out in the future. Im still having fun playing with the arduino. Thanks for your help!

Greets Korné

Have two variables, one for the light sensor and one for the button(s). Get both to work independently then using the XOR operator "^" you control the relay.

Ex. digitalWrite(relayPin, (lightSen ^ buttonSen) )
If the values are both the same(1 and 1), the relay is off, if they are different (1 and 0), the relay is on.

Korne83:
I tried several options.
I did set two different digital pins as an output. One for the ldr-sensor and one for the button. With a led that worked a little. in light i was able to switch on the led with the button. In dark I was unable to switch the led off with the button.. When i replaced the led with a relay, the sensor was still not able to switch the relay on. I guessed the the button wire did function as a ground wire to it with no power on it.

I can't help because you are not giving us information. At the moment you have all the information and we have none.

You need to post the revised code AND the wiring diagram that goes with it.

It seems to me like you are randomly fiddling with things and getting nowhere. Successful debugging requires a very disciplined systematic approach.

  • Note the current symptoms
  • Make a guess at what the problem might be
  • Change ONE thing
  • Repeat as needed

In your case it may be a good idea to report to the forum after every single change rather than trying to run ahead of your advice.

And keep it simple. Get it working perfectly with LEDs before extending to try relays.

...R

Hi HazardsMind,

That can be the solution :slight_smile: I have read about XOR after your reply and that might be the answer :slight_smile:
When both set to high the pin will be low :slight_smile: That's what I need. Because that was the thing that didn't work. I was not able tot switch the led off when the LDR was dark. Thanks alot. Im gonna try to import XOR in my code :slight_smile: That will be a challange :slight_smile:

@Robin I guess its true im not doing this the most smart way :slight_smile: As said Im just a beginner. I have no experience with programming and no experience with electronic. I'm learning by trying, doing dumb things, burning led's and sometimes having a victory moment :slight_smile: But im still having fun with this arduino and hope to become better at this. Thanks for you help and in the future I will post full schematics en code when I have a question!

Korne83:
But im still having fun

That's the important bit.

...R