Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Event Timer Light Fader*/
- /*how to use this sketch and how to make the controlling circuit board is at
- https://steamtraininfo.com/for-modelers/arduino-projects/led-fade-up-fade-down */
- #define streetSwitch A2 /*controls on and off function. */
- /*Declartions for lights that will fade on and off. All are PWM pins. */
- int Light3 = 3;
- int Light5 = 5;
- int Light6 = 6;
- int Light9 = 9;
- int Light10 = 10;
- int Light11 = 11;
- int valStreet = 0;
- int brightness = 0;
- /* how many points to fade the LED by. A higher amount means the steps will be larger. */
- int fadeupAmount = 2;
- int fadedownAmount = 5;
- /*these set the time between steps. So if you are making street lights, for example, you can have them, just like they do in the real world,
- fade on in small steps over a long time and have them fade off quickly.*/
- int stepupdelay = 150; /* slows the fade on time by setting time between steps in change of brightness set above */
- int stepdowndelay = 100; /* slows the fade off time by setting time between steps in change of brightness set above */
- void setup() {
- pinMode(Light3 , OUTPUT);
- pinMode(Light5 , OUTPUT);
- pinMode(Light6 , OUTPUT);
- pinMode(Light9 , OUTPUT);
- pinMode(Light10 , OUTPUT);
- pinMode(Light11 , OUTPUT);
- pinMode(streetSwitch, INPUT);
- }
- void loop() {
- valStreet = digitalRead(streetSwitch);
- if (valStreet== HIGH) {
- analogWrite(Light3, brightness);// set the brightness of pin 3:
- analogWrite(Light5, brightness);// set the brightness of pin 5:
- analogWrite(Light6, brightness);// set the brightness of pin 6:
- analogWrite(Light9, brightness);// set the brightness of pin 9:
- analogWrite(Light10, brightness);// set the brightness of pin 10:
- analogWrite(Light11, brightness);// set the brightness of pin 11:
- brightness = brightness + fadeupAmount; // change the brightness for next time through the loop:
- delay(stepupdelay);/* slows the fade time by setting time between steps in change of brightness. change as you wish in declarations above */
- if (brightness >= 255) { // keeps the LEDS bright once they reach max brigthness
- brightness = 255;
- }
- }
- if (valStreet == LOW) {
- analogWrite(Light3, brightness);// set the brightness of pin 3:
- analogWrite(Light5, brightness);// set the brightness of pin 5:
- analogWrite(Light6, brightness);// set the brightness of pin 6:
- analogWrite(Light9, brightness);// set the brightness of pin 9:
- analogWrite(Light10, brightness);// set the brightness of pin 10:
- analogWrite(Light11, brightness);// set the brightness of pin 11:
- brightness = brightness - fadedownAmount; // change the brightness for next time through the loop:
- delay(stepdowndelay);/* slows the fade time by setting time between steps in change of brightness. change as you wish in declarations above */
- if (brightness <= 0) {
- brightness = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment