# LED Lightsaber Effect

162

1

3

Posted

## Introduction: LED Lightsaber Effect

After seeing what other types of ideas my classmates came up with for the project, I wanted to sort of go in the same direction as them but create my own segmented idea. The lightsaber was perfect to combine the LED's with an effect that is much more satisfying but not much more difficult than the simple blink. What you can see happening in the Gif is the pressing of the button, which begins the sequence of lights turning on, creating the effect of a lightsaber.

## Step 1: Wiring the Board

After using an online resistor calculator for the LED's and determining which resistor I would need on the button, I was able to easily work down the row from my LED's in order for the connection to work. The only problem I encountered in the creation of my board was incorporating the button and figuring out how it was laid out. When the button is pressed, the power will flow through the breadboard and connect with the LED's to successfully run the code. This requires aligning the wires and transistors through one side of the button, and when connected running them out the other so that there is the on/off capability.

## Step 2: Code

My code used the basic button example, I then transformed it using ideas from the blink example into the code to run the lightsaber animation. However, one of the issues I struggled with was having the lightsaber stay on until I pressed the button again. My classmate taught me (^=) which really made the difference. However, because I just learned it, I recommend looking for a tutorial because I don't have the best grasp on it.

`const int buttonPin = 7;     // the number of the pushbutton pin`
```const int ledPin1 =  13;      // the number of the first LED pin
const int ledPin2 = 12;        // the number of the second LED pin
const int ledPin3 = 11;         // ^
const int ledPin4 = 10;           // ^
const int ledPin5 = 9;             // ^
const int ledPin6 = 8;              // ^
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int LEDon = 0;
void setup() {
// initialize the LED pins as outputs:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
// initialize the pushbutton pin as an input:
}
void loop() {
// read the state of the pushbutton value:
if (buttonState == HIGH){
LEDon ^= 1;
}
// check if the pushbutton is pressed. If it is, the buttonState is LOW:
if (LEDon == 1) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay(40);
digitalWrite(ledPin2, HIGH);
delay(40);
digitalWrite(ledPin3, HIGH);
delay(40);
digitalWrite(ledPin4, HIGH);
delay(40);
digitalWrite(ledPin5, HIGH);
delay(40);
digitalWrite(ledPin6, HIGH);
delay(40);
}
delay(200);
if (LEDon == 0) {
// turn LED off:
digitalWrite(ledPin6, LOW);
delay(20);
digitalWrite(ledPin5, LOW);
delay(20);
digitalWrite(ledPin4, LOW);
delay(20);
digitalWrite(ledPin3, LOW);
delay(20);
digitalWrite(ledPin2, LOW);
delay(20);
digitalWrite(ledPin1, LOW);
}
}```

## Recommendations

• ### Arduino Class

67,983 Enrolled

• ### Spotless Contest

We have a be nice policy.