Patchr
Published © MIT

Arduino Lightsaber

Every hardware Jedi has to build their own lightsaber. Here’s how we built our RGB Arduino saber.

IntermediateFull instructions provided2 hours4,274

Things used in this project

Hardware components

ATmega328
Microchip ATmega328
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
16 MHz Crystal
16 MHz Crystal
×1
Multilayer Ceramic Capacitor, 20 pF
Multilayer Ceramic Capacitor, 20 pF
×2
Resistor 10k ohm
Resistor 10k ohm
×1
Ceramic Disc Capacitor, 0.1 µF
Ceramic Disc Capacitor, 0.1 µF
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1
Adafruit RGBW LED with integrated chip
×1
9V Battery Clip
9V Battery Clip
×1
9V battery (generic)
9V battery (generic)
×1
Rocker Switch, Non Illuminated
Rocker Switch, Non Illuminated
×1

Software apps and online services

Patchr
Patchr
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Bantam Tools Desktop PCB Milling Machine
Bantam Tools Desktop PCB Milling Machine
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

ArduSaber_MainPCB

Gerber Files for the main board.

ArduSaber_ButtonPCB

Gerber Files for the button PCB.

ArduSaber_NeopixelPCB

Gerber Files for the neopixel right angle PCB.

Code

ArduSaber

Arduino
This is definitely a work in progress, there are improvements to be made with the fade on and fade off. I'll update the code with changes, but please suggest changes as well!
//Arduino Lightsaber
//MCU: ATMega328
//PCB Software: Patchr.io
//By: Eric Schneider, 2019
//License: Beerware 2019

#include <Adafruit_NeoPixel.h>

#define PIN 3 //LED attached to digital pin 3

Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_RGB + NEO_KHZ800);

int button = 4; //Button attached to digital pin 4
int delayval = 500;
int pot = A0; //Potentiometer attached to analog pin 0
int potValue = 0;
int potMap = 0;

boolean oldSwitchState = LOW;
boolean newSwitchState = HIGH;

void fadeOn() { //fades on into the first color

  uint16_t i, j;

  for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(0, j, 0, 0);

      strip.show();
      delay(10);
    }
  }
}

void fadeOff0() { //these help each color selection fade out to dark when button is pressed
  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j , 0, 0);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}

void fadeOff1() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j , j, 0);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}

void fadeOff2() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0 , j, 0);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}

void fadeOff3() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0 , 0, j);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}

void fadeOff4() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0 , j, j);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}

void fadeOff5() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j , 0, j);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}


void fadeOff6() {

  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j , j, j);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(1500);
}


void setup() {
  Serial.begin(9600);

  pinMode(button, INPUT);

  strip.begin();
  strip.show(); //pixels set to off position

  fadeOn();

}

void loop() {
  newSwitchState = digitalRead(button);
  potValue = analogRead(pot);
  potMap = map(potValue, 0, 1030, 0, 7); //maps values to 8 points to switch colors, you could remove this and scale the RGB values to whatever color you'd like

  Serial.println(potMap);



  if (potMap == 0) { //green
    strip.setPixelColor(0, 255, 0, 0);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff0();
      delay(delayval);
      strip.show();
      delay (10000);
    }
  }

  if (potMap == 1) { //orange
    strip.setPixelColor(0, 255, 255, 0);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff1();
      delay(delayval);
      strip.show();
      delay (10000);
    }

  }

  if (potMap == 2) { //red
    strip.setPixelColor(0, 0, 255, 0);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff2();
      delay(delayval);
      strip.show();
      delay (10000);
    }

  }

  if (potMap == 3) { //blue
    strip.setPixelColor(0, 0, 0, 255);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff3();
      delay(delayval);
      strip.show();
      delay (10000);
    }

  }

  if (potMap == 4) { //purple
    strip.setPixelColor(0, 0, 255, 255);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff4();
      delay(delayval);
      strip.show();
      delay (10000);
    }

  }

  if (potMap == 5) { //light blue
    strip.setPixelColor(0, 255, 0, 255);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff5();
      delay(delayval);
      strip.show();
      delay (10000);
    }

  }

  if (potMap == 6) { //white
    strip.setPixelColor(0, 255, 255, 255);
    strip.show();
    delay(delayval);

    if (newSwitchState == HIGH) {
      fadeOff6();
      delay(delayval);
      strip.show();
      delay (10000);
    }
  }

}

Credits

Patchr

Patchr

4 projects • 26 followers
Patchr makes PCB design a snap. With our software, you can go from idea to a completed board in days, not weeks.

Comments