Skip to content

Instantly share code, notes, and snippets.

@dragonlock2
Created September 30, 2019 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragonlock2/7ee6c91947dae831a22680cf416bf97d to your computer and use it in GitHub Desktop.
Save dragonlock2/7ee6c91947dae831a22680cf416bf97d to your computer and use it in GitHub Desktop.
Code for my lightsaber v3.
#include <TMRpcm.h> //audio libs
#include <SPI.h>
#include <SdFat.h>
#include "I2Cdev.h" //gyro libs
#include "MPU6050.h"
#include "Wire.h"
SdFat sd;
File file;
TMRpcm audio;
MPU6050 gyro;
int16_t gx, gy, gz; //Wip
int SWING;
int CLASH;
int ON_TIME;
int OFF_TIME;
boolean state = false;
long pulseTime; //used to keep track of time in fading
boolean goinDown; //check to fade up or down
int curBright13;
long blasterTime; //used to see if blaster block or not
int BLASTER_DELAY; //time to press under to trigger blaster
int font;
int numBoot; //attributes
int numClash;
int numOff;
int numOn;
int numSwing;
int curVolume;
int numBlast;
int PULSE_DELAY; //length of pulse change from bright to dim
int MIN_BRIGHT_13; //from 0-255
int MAX_13 = 255; //max brightness for rgb
const char configFile[11] = "config.txt";
const char setFont[8] = "set.txt";
char blast[11] = "blast0.wav";
char zero = '0';
char newLine = '\n';
char boot[16]; //files names
char clash[17];
char hum[14];
char lockup[17];
char off[15];
char on[14];
char swing[17];
#define mainButton A4
#define auxButton A5
#define led 13
#define crystalLED 6
//crystal stuff
int curBright6 = 255;
int CRYSTAL_DELAY = 10;
int minBright6 = 128;
long crystalTime = 0;
boolean crysDown = true;
/*
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
} */
void setup() {
//Serial.begin(57600); //test stuff
//while(!Serial); //test stuff
pwm6(curBright6);
sd.begin(4, SPI_FULL_SPEED);
pinMode(mainButton, INPUT_PULLUP);
pinMode(auxButton, INPUT_PULLUP);
audio.speakerPin = 9;
pinMode(10, OUTPUT); //second audio pin
audio.setVolume(4); //default safe volume
audio.quality(1);
delay(50); //wait a little
if(digitalRead(mainButton) == LOW) { //if button held from start, enter menu
audio.play("choose.wav", 0);
while(audio.isPlaying(0)); //wait for sound to finish
chooseFont();
}
delay(50); //wait a little
getAttributes(); //gets all the variables on number of files from font
audio.setVolume(curVolume);
Wire.begin();
gyro.initialize();
gyro.setFullScaleGyroRange(MPU6050_ACCEL_FS_16);
randomSeed(analogRead(A1));
pwm613config(1); //187.5khz
boot[10] = zero + random(0, numBoot);
audio.play(boot);
while(audio.isPlaying());
audio.loop(1, 1);
}
void loop() {
gyro.getRotation(&gx, &gy, &gz);
gx = abs(gx);
if(digitalRead(mainButton) == LOW) {
state = !state;
if(state)
onAnim();
else
offAnim();
}
if(state) { //if on
if(digitalRead(auxButton) == LOW) {
blasterTime = millis();
while(digitalRead(auxButton) == LOW) {
delay(50); //debounce
if(millis() - blasterTime >= BLASTER_DELAY) { //lockup time
curBright13 = MAX_13;
pwm13(curBright13);
pwm6(curBright13);
audio.play(lockup, 0);
audio.loop(1, 0);
while(digitalRead(auxButton) == LOW);
audio.loop(0, 0);
}
}
if(millis() - blasterTime < BLASTER_DELAY) {
pwm13(MAX_13); //for now cuz no rgb
pwm6(MAX_13);
blast[5] = zero + random(0, numBlast); //need to make settable
audio.play(blast, 0);
while(audio.isPlaying(0));
pwm13(curBright13);
pwm6(curBright13);
}
audio.stopPlayback(0);
}
if(gx >= SWING && gx < CLASH) {
swing[11] = zero + random(0, numSwing);
audio.play(swing, 0);
while(audio.isPlaying(0))
updateLED();
}
if(gx >= CLASH) {
pwm13(MAX_13);
pwm6(MAX_13);
clash[11] = zero + random(0, numClash);
audio.play(clash, 0);
while(audio.isPlaying(0));
pwm13(curBright13);
pwm13(curBright13);
}
updateLED();
}
if(!state)
updateCrystal();
}
void updateCrystal() {
if(millis() - crystalTime >= CRYSTAL_DELAY) {
crystalTime = millis();
if(crysDown) {
curBright6--;
if(curBright6 < minBright6) {
crysDown = false;
curBright6++;
curBright6++;
}
} else {
curBright6++;
if(curBright6 > 255) {
crysDown = true;
curBright6--;
curBright6--;
}
}
pwm6(curBright6);
}
}
void updateLED() {
if(millis() - pulseTime >= PULSE_DELAY) {
pulseTime = millis();
if(goinDown) {
curBright13--;
if(curBright13 < MIN_BRIGHT_13) {
goinDown = false;
curBright13++;
curBright13++;
}
} else {
curBright13++;
if(curBright13 > MAX_13) {
goinDown = true;
curBright13--;
curBright13--;
}
}
pwm13(curBright13);
pwm6(curBright13);
}
}
void onAnim() {
//Serial.println("Turning on");
on[8] = zero + random(0, numOn);
//Serial.println(on);
audio.play(on, 0);
for(int i = 0; i <= MAX_13; i++) {
pwm13(i);
if(i > curBright6)
pwm6(i);
delay(ON_TIME/(MAX_13 + 1));
}
audio.play(hum, 1);
goinDown = true;
curBright13 = MAX_13;
while(audio.isPlaying(0))
updateLED();
}
void offAnim() {
//Serial.println("off");
off[9] = zero + random(0, numOff);
//Serial.println(off);
audio.play(off, 0);
audio.stopPlayback(1);
for(int i = curBright13; i >= 0; i--) {
pwm13(i);
pwm6(i);
delay(OFF_TIME/(curBright13 + 1));
}
while(audio.isPlaying());
for(int i = 0; i <= curBright6; i++) {
pwm6(i);
delay(CRYSTAL_DELAY);
}
}
void chooseFont() {
file = sd.open(setFont);
font = (file.readStringUntil(newLine).charAt(0) - zero); //gets current font
file.close();
file = sd.open(configFile);
int totalFont = file.readStringUntil(newLine).charAt(0) - zero;
file.close();
char bootFile[16]; //files need to be char arrays
(String("font" + String(font) + "/boot0.wav")).toCharArray(bootFile, 16);
//Serial.println(font);
audio.play(bootFile);
while(digitalRead(auxButton)) { //waits for button press to confirm font
if(!digitalRead(mainButton)) { //increments through fonts
font = (font + 1) % totalFont;
bootFile[4] = zero + font;
audio.play(bootFile);
//Serial.println(bootFile);
delay(500);
}
}
saveFont();
}
void saveFont() {
//Serial.println(font);
audio.play("selected.wav", 0);
while(audio.isPlaying(0));
file = sd.open(setFont);
String selectedFont = file.readStringUntil(newLine);
selectedFont.setCharAt(0, zero + font);
file.close();
sd.remove(setFont);
file = sd.open(setFont, FILE_WRITE);
file.println(selectedFont);
file.close();
file = sd.open(setFont); //don't know why i have to open and close again
//while(file.available())
// Serial.write(file.read());
file.close();
}
void getAttributes() {
file = sd.open(setFont);
font = file.readStringUntil(newLine).charAt(0) - zero;
file.close();
file = sd.open(configFile);
file.readStringUntil(newLine); //skip the total font line;
SWING = file.readStringUntil(newLine).substring(0, 5).toInt();
CLASH = file.readStringUntil(newLine).substring(0, 5).toInt();
BLASTER_DELAY = file.readStringUntil(newLine).substring(0, 3).toInt();
numBlast = file.readStringUntil(newLine).substring(0, 1).toInt();
file.close();
String base = "font" + String(font) + "/";
char configFont[18]; //attribute file
(String(base + "numbers.txt")).toCharArray(configFont, 18);
file = sd.open(configFont);
numBoot = file.readStringUntil(newLine).charAt(0) - zero;
numClash = file.readStringUntil(newLine).charAt(0) - zero;
numOff = file.readStringUntil(newLine).charAt(0) - zero;
numOn = file.readStringUntil(newLine).charAt(0) - zero;
numSwing = file.readStringUntil(newLine).charAt(0) - zero;
curVolume = file.readStringUntil(newLine).charAt(0) - zero;
ON_TIME = file.readStringUntil(newLine).substring(0, 4).toInt();
OFF_TIME = file.readStringUntil(newLine).substring(0, 4).toInt();
PULSE_DELAY = file.readStringUntil(newLine).substring(0, 2).toInt();
MIN_BRIGHT_13 = file.readStringUntil(newLine).substring(0, 3).toInt();
MIN_BRIGHT_13 = MAX_13 * MIN_BRIGHT_13 / 100; //calculate now so not later
//crystal stuf
CRYSTAL_DELAY = file.readStringUntil(newLine).substring(0, 2).toInt();
minBright6 = file.readStringUntil(newLine).substring(0, 3).toInt();
file.close();
(String(base + "boot0.wav")).toCharArray(boot, 16);
(String(base + "clash0.wav")).toCharArray(clash, 17);
(String(base + "hum.wav")).toCharArray(hum, 14);
(String(base + "lockup.wav")).toCharArray(lockup, 17);
(String(base + "off0.wav")).toCharArray(off, 15);
(String(base + "on0.wav")).toCharArray(on, 14);
(String(base + "swing0.wav")).toCharArray(swing, 17);
}
void pwm613config(int mode) {
TCCR4A = 0;
TCCR4B = mode;
TCCR4C = 0;
TCCR4D = 0;
TCCR4D = 0;
PLLFRQ = (PLLFRQ&0xCF)|0x30;
OCR4C = 255;
}
void pwm13(int value) {
OCR4A = value;
DDRC|=1<<7;
TCCR4A=0x82;
}
void pwm6(int value) {
OCR4D = value;
DDRD|=1<<7;
TCCR4C|=0x09;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment