How to turn off the LED when Box is closed

Hey to all,
i got a box where LEDs are installed. When i open tzhe box, the light sensor turns on the LEDs. The problem is that the light sensor won´t turn them off while they are on themselves....

Any suggestions how i could solve it? I´m new to arduino and i am using Uno and IDE 1.0.6

/*
"Zapfsäule"

Programmiert
Start: 21.02.2015
Fertiggestellt:XX.XX.2015

Tobias Sobkowiak
sobkowiaktobias(at)gmail.com
www.druckundmedientechnologie.de

http://www.arduino.cc/en/Tutorial/Button
*/
// Pinnummern
const int whiskeycolaButton = 2; // the number of the pushbutton pin
const int colaPin = 13; // Cola Pumpe (+) Pin
const int whiskeyPin = 12; // Whiskey Pumpe (+) Pin

// Variablen werden sich ändern...
int buttonState = 0; // Knopfvariable

//Beleuchtung
int Beleuchtung = 7;
int lightSensor = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(colaPin, OUTPUT);
// initialize the LED pin as an output:
pinMode(whiskeyPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(whiskeycolaButton, INPUT);

//Beleuchtung
Serial.begin(9600);
pinMode(Beleuchtung, OUTPUT);

}

void loop(){
// Lese Whiskey-Cola-Knopf
buttonState = digitalRead(whiskeycolaButton);

// Wenn der Knopf gedrückt wird
if (buttonState == HIGH) {
// Pumpe starten

digitalWrite(colaPin, HIGH);
digitalWrite(whiskeyPin, HIGH);
delay(2000);//Befüllzeit
digitalWrite(colaPin, LOW);
digitalWrite(whiskeyPin, LOW);
digitalWrite(colaPin, HIGH);
delay(7000);//Befüllzeit
digitalWrite(colaPin, LOW);
}

//Beleuchtung
int sensorValue = analogRead(lightSensor);
// print out the value you read:
Serial.println(sensorValue);

if (sensorValue < 900)
{
digitalWrite(Beleuchtung, HIGH);
}
else
{
digitalWrite(Beleuchtung, LOW);
}

}

Perhaps you can screen the sensor so it does not see the LEDs when the lid is closed ?

...R

 if (buttonState == HIGH) {     
    // Pumpe starten   

    digitalWrite(colaPin, HIGH); 
    digitalWrite(whiskeyPin, HIGH);  
    delay(2000);//Befüllzeit 
    digitalWrite(colaPin, LOW);
    digitalWrite(whiskeyPin, LOW); 
    digitalWrite(colaPin, HIGH);
    delay(7000);//Befüllzeit  
    digitalWrite(colaPin, LOW);
  }

Here you tell the program what to do when you get a HIGH on the switch. It's not going to try to figure out what you want to do when you get a LOW. You're going to have to code that part too.

Delta_G:

 if (buttonState == HIGH) {     

// Pumpe starten

digitalWrite(colaPin, HIGH);
   digitalWrite(whiskeyPin, HIGH);  
   delay(2000);//Befüllzeit
   digitalWrite(colaPin, LOW);
   digitalWrite(whiskeyPin, LOW);
   digitalWrite(colaPin, HIGH);
   delay(7000);//Befüllzeit  
   digitalWrite(colaPin, LOW);
 }





Here you tell the program what to do when you get a HIGH on the switch. It's not going to try to figure out what you want to do when you get a LOW. You're going to have to code that part too.

Thanks for the replies and sorry for confusing. The part you where looking at is only the first on.

  1. push a buton
  2. start cola and whiskey pump
  3. stop after XX secs.

...that runs fine. I don´t think i need an else there..., but i plan to install more buttons and then there willl be an else if.

//Beleuchtung
int sensorValue = analogRead(lightSensor);
// print out the value you read:
Serial.println(sensorValue);

if (sensorValue < 900)
{
digitalWrite(Beleuchtung, HIGH);
}
else
{
digitalWrite(Beleuchtung, LOW);
}

}

--> above is the problematic part. I made a kind of cocktail maker and want it to be enlighted inside when i open it and to turn of the leds when i close the door.

I am using this sensor:

Or am i going the wrong way to combine different actions in the loop???

I made a kind of cocktail maker and want it to be enlighted inside when i open it and to turn of the leds when i close the door.

The easiest way, of course, would be to have a sensor activated when the door is closed.

UKHeliBob:
The easiest way, of course, would be to have a sensor activated when the door is closed.

Thats what i am trying, but the leds are too shiny when they are on and the sensor won´t deactivate.

sobo84:
Or am i going the wrong way to combine different actions in the loop???

No. Combining actions in the loop is fine.

As others say, the problem is that the LEDs produce enough light to make it impossible to use a light detector to know if the door is open or closed. This is not an arduino or programming problem, it's a science problem. One possibility may be to put a colour filter over the sensor to mask detection of the LEDS. That may be difficult, or potentially impossible.

Use a door switch.

Hi, use a different sensor, microswitch under the lid, magnet and reed switch, anything that doesn't rely on light to activate.
That way it will even work in the dark.

Tom...... :slight_smile:

use a different sensor

That was what I had in mind.

Thanks for help... :slight_smile: