Advertisement
Monkfish

Monkfish's LED Stair Lights (proof of concept)

Jan 6th, 2014
7,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 KB | None | 0 0
  1. /*
  2. Monkfish's LED stair lights (proof of concept)
  3. */
  4.  
  5.   //LED pin assignments
  6.   int led1 = 2;
  7.   int led2 = 3;
  8.   int led3 = 4;
  9.   int led4 = 5;
  10.   int led5 = 6;
  11.   int led6 = 7;
  12.   int led7 = 8;
  13.   int led8 = 9;
  14.   int led9 = 10;
  15.  
  16.   // button pin assignments
  17.   int b_up = 11; //button at bottom of stairs
  18.   int b_down = 12; //button at top of stairs
  19.  
  20.   //LED delay settings
  21.   int tOn = 400; //LED On delay
  22.   int tOff = 750; //LED Off delay
  23.   int del = 0; //Delay after action (used for debugging - set to 500-1000 to introduce a delay between each action)
  24.  
  25.   //variables
  26.   static unsigned activeup = 0; //whether the system is active going upwards
  27.   static unsigned activedown = 0; //whether the system is active going downwards
  28.  
  29. void setup() { //make with the setups
  30.  
  31.   //Set Serial
  32.   Serial.begin(9600);
  33.   Serial.println("");
  34.   Serial.println("Activated");
  35.   delay(del);
  36.  
  37.   //Set LED pins as output
  38.   pinMode(led1, OUTPUT);
  39.   pinMode(led2, OUTPUT);
  40.   pinMode(led3, OUTPUT);
  41.   pinMode(led4, OUTPUT);
  42.   pinMode(led5, OUTPUT);
  43.   pinMode(led6, OUTPUT);
  44.   pinMode(led7, OUTPUT);
  45.   pinMode(led8, OUTPUT);
  46.   pinMode(led9, OUTPUT);
  47.  
  48.   //Set button pins as input
  49.   pinMode(b_up, INPUT_PULLUP);
  50.   pinMode(b_down, INPUT_PULLUP);
  51.  
  52.   Serial.println("Pins set");
  53.   delay(del);
  54.  
  55.   //Clear variables
  56.   int activedown = 0;
  57.   int activeup = 0;
  58.  
  59.   //Print variables to Serial
  60.   Serial.print("active up: ");
  61.   Serial.println(activeup);
  62.   Serial.print("active down: ");
  63.   Serial.println(activedown);
  64.   delay(del);
  65.  
  66.   Serial.println("Variables set");
  67.   delay(del);
  68.  
  69.   Serial.println("");
  70.   Serial.println("Ready");
  71. }
  72.  
  73.  
  74. void loop() {  //forever in your loopings
  75.      
  76.       //Set variables based on pin state
  77.       int up = digitalRead(b_up);
  78.       int down = digitalRead(b_down);
  79.    
  80.       Serial.print("active up: ");
  81.       Serial.println(activeup);
  82.       Serial.print("active down: ");
  83.       Serial.println(activedown);
  84.       delay(del);
  85.    
  86.   if (up == LOW){ //Bottom button pressed
  87.    
  88.       Serial.println("Bottom button pressed!");
  89.       delay(del);
  90.      
  91.       //Set variables
  92.       Serial.print("active up: ");
  93.       Serial.println(activeup);
  94.       Serial.print("active down: ");
  95.       Serial.println(activedown);
  96.       delay(del);
  97.      
  98.           if (activeup == 1 || activedown == 1){ //checks if system active
  99.                
  100.                 //If active
  101.                 Serial.println("System active!");
  102.                 delay(del);
  103.                
  104.                 OffDown(); //function call
  105.                        
  106.           } else {
  107.            
  108.             //if not active            
  109.             Serial.println("System activated (up)");
  110.             delay(del);
  111.      
  112.             OnUp(); //function call
  113.            
  114.           }
  115.      
  116.      
  117.   }
  118.      
  119.   if (down == LOW){
  120.      
  121.       Serial.println("Top button pressed");
  122.       delay(del);
  123.      
  124.       Serial.print("active up: ");
  125.       Serial.println(activeup);
  126.       Serial.print("active down: ");
  127.       Serial.println(activedown);
  128.       delay(del);
  129.      
  130.       if (activeup == 1 || activedown == 1){
  131.            
  132.                 Serial.println("System active!");
  133.                 delay(del);
  134.                
  135.                 OffUp(); //function call
  136.                        
  137.           } else  {
  138.        
  139.             Serial.println("System activated (down)");
  140.             delay(del);
  141.      
  142.             OnDown(); //function call
  143.            
  144.           }
  145.        
  146.   }
  147.  
  148.        
  149. }
  150.  
  151. void OnUp(){ //Switches lights on going up
  152.  
  153.   Serial.println("Switching on lights (up)");
  154.                delay(del);
  155.                
  156.                //set LEDs on in turn
  157.                digitalWrite(led1, HIGH);
  158.                delay(tOn);
  159.                digitalWrite(led2, HIGH);
  160.                delay(tOn);
  161.                digitalWrite(led3, HIGH);
  162.                delay(tOn);
  163.                digitalWrite(led4, HIGH);
  164.                delay(tOn);
  165.                digitalWrite(led5, HIGH);
  166.                delay(tOn);
  167.                digitalWrite(led6, HIGH);
  168.                delay(tOn);
  169.                digitalWrite(led7, HIGH);
  170.                delay(tOn);
  171.                digitalWrite(led8, HIGH);
  172.                delay(tOn);
  173.                digitalWrite(led9, HIGH);
  174.                
  175.                activeup = 1; //Set system as active up
  176.                
  177.                Serial.print("active up: ");
  178.                Serial.println(activeup);
  179.                Serial.print("active down: ");
  180.                Serial.println(activedown);
  181.                delay(del);
  182.                
  183.                Serial.println("System now active (up)");
  184.                delay(del);
  185.                                
  186. } //end OnUp
  187.  
  188. void OffUp(){ //switches lights off going up
  189.  
  190.          Serial.println("Switching lights off (Up)");
  191.          delay(del);
  192.            
  193.          
  194.          //sets LEDs off in turn
  195.          digitalWrite(led1, LOW);
  196.          delay(tOff);
  197.          digitalWrite(led2, LOW);
  198.          delay(tOff);
  199.          digitalWrite(led3, LOW);
  200.          delay(tOff);
  201.          digitalWrite(led4, LOW);
  202.          delay(tOff);
  203.          digitalWrite(led5, LOW);
  204.          delay(tOff);
  205.          digitalWrite(led6, LOW);
  206.          delay(tOff);
  207.          digitalWrite(led7, LOW);
  208.          delay(tOff);
  209.          digitalWrite(led8, LOW);
  210.          delay(tOff);
  211.          digitalWrite(led9, LOW);
  212.          
  213.          //set system as being off
  214.          activeup = 0;
  215.          activedown = 0;
  216.          
  217.          Serial.print("active up: ");
  218.          Serial.println(activeup);
  219.          Serial.print("active down: ");
  220.          Serial.println(activedown);
  221.          delay(del);
  222.          
  223.          Serial.println("System not active");
  224.          delay(del);
  225.          
  226. } //End OffUp
  227.  
  228. void OnDown(){ //switches lights on going down
  229.  
  230.          Serial.println("Switching lights on (down)");
  231.          delay(del);
  232.                
  233.          //set LEDs on in turn
  234.          digitalWrite(led9, HIGH);
  235.          delay(tOn);
  236.          digitalWrite(led8, HIGH);
  237.          delay(tOn);
  238.          digitalWrite(led7, HIGH);
  239.          delay(tOn);
  240.          digitalWrite(led6, HIGH);
  241.          delay(tOn);
  242.          digitalWrite(led5, HIGH);
  243.          delay(tOn);
  244.          digitalWrite(led4, HIGH);
  245.          delay(tOn);
  246.          digitalWrite(led3, HIGH);
  247.          delay(tOn);
  248.          digitalWrite(led2, HIGH);
  249.          delay(tOn);
  250.          digitalWrite(led1, HIGH);
  251.          
  252.          activedown = 1; //sets system as active down
  253.          
  254.          Serial.print("active up: ");
  255.          Serial.println(activeup);
  256.          Serial.print("active down: ");
  257.          Serial.println(activedown);
  258.          delay(del);
  259.          
  260.          Serial.println("System now active (down)");
  261.          delay(del);
  262.  
  263. } //End OnDown
  264.  
  265. void OffDown(){ //switches lights off going down
  266.  
  267.        Serial.println("Switching lights off (down)");
  268.        delay(del);      
  269.        
  270.        //sets LEDs off in turn
  271.        digitalWrite(led9, LOW);
  272.        delay(tOff);
  273.        digitalWrite(led8, LOW);
  274.        delay(tOff);
  275.        digitalWrite(led7, LOW);
  276.        delay(tOff);
  277.        digitalWrite(led6, LOW);
  278.        delay(tOff);
  279.        digitalWrite(led5, LOW);
  280.        delay(tOff);
  281.        digitalWrite(led4, LOW);
  282.        delay(tOff);
  283.        digitalWrite(led3, LOW);
  284.        delay(tOff);
  285.        digitalWrite(led2, LOW);
  286.        delay(tOff);
  287.        digitalWrite(led1, LOW);
  288.        
  289.        //set system as not active
  290.        activedown = 0;
  291.        activeup = 0;
  292.        
  293.        Serial.print("active up: ");
  294.        Serial.println(activeup);
  295.        Serial.print("active down: ");
  296.        Serial.println(activedown);
  297.        delay(del);
  298.        
  299.        Serial.println("System not active");
  300.        delay(del);    
  301.  
  302.  
  303. }//End OffDown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement