Sliding door closet light control

Hello,

I have a problem with my project.

The project idea is to control 3 LED strips with 4 limit switches like this: Limit sw. 1 - door 1 (sliding to the left) Limit sw. 2 - door 2 (sliding to the right) Limit sw. 3 & 4 - door 3 (front door sliding to both sides)

and now i would like the led strips to be controled with PWM ( the electronics is not a problem i designed PCB board with all of the components including MOSFT's and the rest)

How i would like it to work :

when Limit sw. 1 will go HIGH led strip 1 will ramp up from 0 to 255 (lets say in 2s) when it will go LOW the LED strip will ramp down from 255 to 0 (lets say in 5s)

same for Limit Switch 2

difference will be in LED strip 3 because it will be controlled by 2 Limit switches so when the door will press 1 of them the ramp up sequence will start, also when it will be depressed the ramp down sequence will start.

i tried the coding but PWM is still a mistery to me.

can any one help please

PS. sorry for my english

The PWM part is reasonably simple. analogWrite(pwmPin, value) will produce a signal on the pin proportional to the value - 0 to 255. You can only use pins 3,5,6,9.10 and 11 for PWM outputs.

At its simplest causing an LED to ramp up over 2 seconds could be done with

for (byte n = 0, n <= 255, n++) {
   analogWrite(pwmPin, n);
   delay(8); // millisecs
}

But that approach is only practical if you are content for the Arduino to do nothing else until the 2 seconds have elapsed. In you case I imagine it is possible (even likely) that another door would be moved immediately after and its LEDs should also go on or off.

To achieve that you need to replace the use of delay() with the technique in the Blink Without Delay example sketch AND you need to make the steps happen without using a FOR loop - so that for each iteration of the loop() function each LED gets a little brighter or darker. The demo several things at a time should give you the idea.

…R

Arduino won’t do anything while the ramp up and down so i dont need lets say multi tasking.

so the code can look like that ?

int led1 = 9;

int switch1 = 2;

boolean currentState = LOW;
boolean lastState = LOW;


void setup()  {
  pinMode(led1, OUTPUT);
  pinMode(switch1, INPUT);

}


void loop(){

if (buttonState == HIGH);
for (byte n = 0, n <= 255, n++) {
   analogWrite(pwmPin, n);
   delay(8); // millisecs
   }
 }

its problably wrong :confused:

ok i changed the coding can any one tell if that will work ?

//LED fade

int ledPin = 9;//the Arduino pin that is connected to the LED

void setup() {                
  pinMode(ledPin, OUTPUT);// initialize the pin as an output
}

void loop() {
  //ramp LED up to full brightness (0 to 255)
  for (int brightness=0;brightness<256;brightness++){
    analogWrite(ledPin,brightness);
    delay(5);
  }

  delay(1000);// wait one second

  //ramp LED down to no brightness (255 to 0)
  for (int brightness=255;brightness>=0;brightness--){
    analogWrite(ledPin,brightness);
    delay(5);
  }

  delay(1000);//wait one second
}

I don’t immediately see anything wrong with your code. Try it and see what happens. The Arduino system is great for learning-by-doing.

I strongly suspect you will change your mind about the multi-tasking after trying stuff out with your doors. Much simpler to build it in from the start.

…R

I tried the code and there is no response to the input the led fades in and out continously :confused:

I don’t know what to do at the moment :confused:

i made a new code but dont know how will it work

//LED fade

const int ledPin = 3;//the Arduino pin that is connected to the LED
const int buttonPin = 2;

void setup() {                
  pinMode(ledPin, OUTPUT);// initialize the pin as an output
  pinMode(buttonPin, INPUT);// initialize button pin as an input
}

void loop() {
  //ramp LED up to full brightness (0 to 255)
  while (buttonPin != HIGH) // Deoes something inside the { }
  {
    for (int brightness=0;brightness<256;brightness++){
      analogWrite(ledPin,brightness);
      delay(5);
      break; //Gets out of the loop
  }
  }
  while (buttonPin != LOW) // Does something inside the { }
  {
    for (int brightness=255;brightness>=0;brightness--){
      analogWrite(ledPin,brightness);
      delay(5);
      break; //Gets out of the loop
  }
  }
}

I am thinking what will happen when the doors will be closed will arduino go into “bad” loop i mean will it be dimming and dimming the led untill it will recive other signal.

Shalvan: I don't know what to do at the moment

You need to think about the problem carefully - that's all anyone here will do, and you can do it just as well.

Take a pencil and paper and work through your code line by line writing down the values of variables as you come to them and following the appropriate path determined by IF and WHILE etc statements. This can be tedious but it is very effective.

Put some Serial.println("here I am A"); statements into the code so you tell where it has got to while it works.

...R

Sounds like you don't have pullup/pulldown resistors. Do you?

Yes i have 10k pull down resistor, the electronics isn't the problem I've already made a PCB with all the needed components such as pull down resistors, MOSFETS, and ARC connectros for switches and LED strips.

The only problem is with coding of the arduino :)