Car Dome Light

Hi guys, sorry if i opened an allready existing topic. I'm trying to make a controll unit for a car dome light. I'm currently using this code for fading on and off when i press it

const int buttonPin = 7;     
const int ledPin = 11;        
const int fadingDelay = 50;  


int buttonState = 0;        
boolean fadingState = false;

void setup() {

  pinMode(ledPin, OUTPUT);
 
  pinMode(buttonPin, INPUT);
}

void loop() {
 
  buttonState = digitalRead(buttonPin);

 
  if (buttonState == HIGH) {
   
    if (fadingState == false) {
      
      for (int i = 0; i <= 255; i += 5) {
        analogWrite(ledPin, i);
        delay(fadingDelay);
      }
    } else {
     
      for (int i = 255; i >= 0; i -= 5) {
        analogWrite(ledPin, i);
        delay(fadingDelay);
      }      
    }
    fadingState = !fadingState;  
  }
}

It works fine for turning it on and off but i want to add some extra functions like fading in when i open the door and fading out after some time (~30s), fading in when i unlock the car and fading off when i lock it (i can get a signal from the ECU for this) and fading in when i turn off the ignition.

The original ECU functions for the dome light are, solid on when i open the door or unlock the car (no fade), delay 30 sec and fade out when i close the door, fade out with no delay then i turn the ignition on and it turns straight off with no fade when i lock the car. When i press it it turns on and off with no fade.

Can someone please give me some advice on how i could add those functions?
Thank you

(forgot to mention my ECU turns off after 10min after i lock my car)

Suppose that you put the code that does the actually fading in a function. Suppose that you call the function to fade on or fade off when the appropriate event happens. Would that help?

Can you figure out when the appropriate events happen?

and use code tags!

In your blocking code, if I close the door during the fade up, it will continue fading up, then fade down...
While you're looking at the whole solution, and factor that in. it's not too hard!

Thank you for the replies, i'll try to modify the code and i'll be back with more questions :slight_smile:

lastchancename:

and use code tags!

code, is that like program ?
tags, like labels ?

add labels to the program ?

you know what you said, but the poster has no idea of the idiom.

dedeman19, welcome to the forum.
please read the sticky at the top of every forum called how to use this forum
click on the post to open it
scroll down to #7, read about code tags
come back and edit your first post, the edit button is on the right at the end of your post.
highlight the text that is your code, and click the code tag button

helps us to read your code.
for longer code, save as a text file and attach the file to your post.
long is when the website tells you it is too long.

Thanks, i hope its ok now, sorry for the inconvenience :smiley:

You never took any action on the very important reply #2.