So I'm working on my car and now I've got my sights on the electrical system. I've installed a keyless entry system and a pulse timer for the dome light. Now, I'd like to give the dome light the fade effect all the modern cars have. After looking around, it seems an Arduino would be the most reliable.
I currently own an board similar to the Arduino Nano, specifically the SainSmart Nano. So I've determined that I'd power the board with a constant 12V, use pin 7 as input, and use pin 9 as output.
My code:
int domeLightIn = 7; int domeLightOut = 9; int brightness = 0; int fadeAmount = 5; void setup() { pinMode(domeLightOut, OUTPUT); pinMode(domeLightIn, INPUT); digitalRead(domeLightIn); } void loop() { // Case 1: State of Door: Open/Closed // Case 2: State of Switch: On/Off // Case 3: State of KES: Active/Inactive //Case 1: If door is closed and led is off, brightness remains 0; //Case 2: If switch is off and led is off, brightness remains 0; //Case 3: If KES is inactive and led is off, brightness remains 0; if (digitalRead(domeLightIn) == LOW && brightness != 255){ brightness = 0; } //Case 1: If door is open and led is off, brightness will fade in; //Case 2: If switch is on and led is off, brightness will fade in; //Case 3: If KES is active and led is off, brightness will fade in; if (digitalRead(domeLightIn) == HIGH && brightness != 255) { for (brightness = 0; brightness < 255; brightness = brightness + fadeAmount) { analogWrite(domeLightOut, brightness); delay(35); } } //Case 1: If door is closed and led is on, brightness will fade out; //Case 2: If switch is off and led is on, brightness will fade out; //Case 3: If KES is inactive and led is on, brightness will fade out; if (digitalRead(domeLightIn) == LOW && brightness == 255) { for (brightness = 255; brightness > 0; brightness = brightness - fadeAmount) { analogWrite(domeLightOut, brightness); delay (35); } } } I am unsure whether I am using digitalRead() correctly along with wiring the input. So, am I doing this right? or perhaps there is a better way?