Arduino Code For Automatic Stair LED
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
int led9 = 10;
// button pin assignments
int b_up = 11; //button at bottom of stairs
int b_down = 12; //button at top of stairs
//LED delay settings
int tOn = 550; //LED On delay
int tOff = 5
50; //LED Off delay
int del = 0; //Delay after action (used for debugging - set to 500-1000 to introduce a delay between each action)
//variables
static unsigned activeup = 0; //whether the system is active going upwards
static unsigned activedown = 0; //whether the system is active going downwards
void setup() { //make with the setups
//Set Serial
Serial.begin(9600);
Serial.println("");
Serial.println("Activated");
delay(del);
//Set LED pins as output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
//Set button pins as input
pinMode(b_up, INPUT_PULLUP);
pinMode(b_down, INPUT_PULLUP);
Serial.println("Pins set");
delay(del);
//Clear variables
int activedown = 0;
int activeup = 0;
//Print variables to Serial
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
Serial.println("Variables set");
delay(del);
Serial.println("");
Serial.println("Ready");
}
void loop() { //forever in your loopings
//Set variables based on pin state
int up = digitalRead(b_up);
int down = digitalRead(b_down);
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
if (up == LOW){ //Bottom button pressed
Serial.println("Bottom button pressed!");
delay(del);
//Set variables
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
if (activeup == 1 || activedown == 1){ //checks if system active
//If active
Serial.println("System active!");
delay(del);
OffDown(); //function call
} else {
//if not active
Serial.println("System activated (up)");
delay(del);
OnUp(); //function call
}
}
if (down == LOW){
Serial.println("Top button pressed");
delay(del);
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
if (activeup == 1 || activedown == 1){
Serial.println("System active!");
delay(del);
OffUp(); //function call
} else {
Serial.println("System activated (down)");
delay(del);
OnDown(); //function call
}
}
}
void OnUp(){ //Switches lights on going up
Serial.println("Switching on lights (up)");
delay(del);
//set LEDs on in turn
digitalWrite(led1, HIGH);
delay(tOn);
digitalWrite(led2, HIGH);
delay(tOn);
digitalWrite(led3, HIGH);
delay(tOn);
digitalWrite(led4, HIGH);
delay(tOn);
digitalWrite(led5, HIGH);
delay(tOn);
digitalWrite(led6, HIGH);
delay(tOn);
digitalWrite(led7, HIGH);
delay(tOn);
digitalWrite(led8, HIGH);
delay(tOn);
digitalWrite(led9, HIGH);
activeup = 1; //Set system as active up
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
Serial.println("System now active (up)");
delay(del);
} //end OnUp
void OffUp(){ //switches lights off going up
Serial.println("Switching lights off (Up)");
delay(del);
//sets LEDs off in turn
digitalWrite(led1, LOW);
delay(tOff);
digitalWrite(led2, LOW);
delay(tOff);
digitalWrite(led3, LOW);
delay(tOff);
digitalWrite(led4, LOW);
delay(tOff);
digitalWrite(led5, LOW);
delay(tOff);
digitalWrite(led6, LOW);
delay(tOff);
digitalWrite(led7, LOW);
delay(tOff);
digitalWrite(led8, LOW);
delay(tOff);
digitalWrite(led9, LOW);
//set system as being off
activeup = 0;
activedown = 0;
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
Serial.println("System not active");
delay(del);
} //End OffUp
void OnDown(){ //switches lights on going down
Serial.println("Switching lights on (down)");
delay(del);
//set LEDs on in turn
digitalWrite(led9, HIGH);
delay(tOn);
digitalWrite(led8, HIGH);
delay(tOn);
digitalWrite(led7, HIGH);
delay(tOn);
digitalWrite(led6, HIGH);
delay(tOn);
digitalWrite(led5, HIGH);
delay(tOn);
digitalWrite(led4, HIGH);
delay(tOn);
digitalWrite(led3, HIGH);
delay(tOn);
digitalWrite(led2, HIGH);
delay(tOn);
digitalWrite(led1, HIGH);
activedown = 1; //sets system as active down
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
Serial.println("System now active (down)");
delay(del);
} //End OnDown
void OffDown(){ //switches lights off going down
Serial.println("Switching lights off (down)");
delay(del);
//sets LEDs off in turn
digitalWrite(led9, LOW);
delay(tOff);
digitalWrite(led8, LOW);
delay(tOff);
digitalWrite(led7, LOW);
delay(tOff);
digitalWrite(led6, LOW);
delay(tOff);
digitalWrite(led5, LOW);
delay(tOff);
digitalWrite(led4, LOW);
delay(tOff);
digitalWrite(led3, LOW);
delay(tOff);
digitalWrite(led2, LOW);
delay(tOff);
digitalWrite(led1, LOW);
//set system as not active
activedown = 0;
activeup = 0;
Serial.print("active up: ");
Serial.println(activeup);
Serial.print("active down: ");
Serial.println(activedown);
delay(del);
Serial.println("System not active");
delay(del);
}//End OffDown
Comments
Post a Comment