I have this saved as PROTONERD LED STRING CODE (ARDUINO NANO, MP3-TF-16P, MPU6050 GYRO)
I believe that this is just the MPU6050 gyro code, but don't quote me as I have like 0 so far work done with arduino, parts on the way........ link is in post #53 and is ryang's not Protonerds.
Arduino Lightsaber for/with LED string blade#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 accelgyro;
int16_t ax, ay, az; // define accel as ax,ay,az
int16_t gx, gy, gz; // define gyro as gx,gy,gz
int16_t px, py, pz; // previous accel settings
long x, y, z;
long accel; // calc accel
int inPin = 9; // the number of the input pin
int outPin = 10; // the number of the output pin
int ledPin = 11;
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int track = 20;
int prev = 20;
boolean state = false; // the current state of the circuit
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 500; // the debounce time, increase if the output flickers
long looptime = 0; // the last time the loop was played
long gtime = 0;
SoftwareSerial mySerial(0, 1); // RX, TX
void setup()
{
Wire.begin(); // join I2C bus
//Serial.begin(38400); // initialize serial communication
//while (!Serial); // wait for Leonardo enumeration, others continue immediately
Serial.println("INIT");
accelgyro.initialize();
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
mySerial.begin(9600);
mp3_set_serial(mySerial); //set Serial for DFPlayer-mini mp3 module
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // read measurements from device
if(millis() - gtime > 500) {
x=px-ax; y=py-ay; z=pz-az;
x=abs(x); y=abs(y); z=abs(z);
accel = (x+y+z);
px = ax; py = ay; pz = az;
if(state && accel > 15000) {
mp3_play(random(31,34));
//analogWrite(outPin, 255); delay(100);
//analogWrite(outPin, 50); delay(100);
//analogWrite(outPin, 255); delay(100);
//analogWrite(outPin, 200); delay(100);
looptime = millis()-2000;
}
Serial.print(gx);Serial.print(" ");Serial.print(gy);Serial.print(" ");Serial.println(gz);
Serial.println(accel);
gtime = millis();
}
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (!state) {
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(1);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
state = true;
} else {
mp3_play(2);
for(brightness = 200; brightness > -1; brightness--) {
analogWrite(outPin, brightness);
delay(7);
}
digitalWrite(ledPin, LOW);
state = false;
}
time = millis();
}
// if (state && millis() - looptime > 4000) {
// while (track == prev) {
// track = random(21,24);
// }
// mp3_play(track);
// prev = track;
// looptime = millis();
// }
previous = reading;
}