So, I am coding an arduino nano for this lightsaber i am making. However, there seems to be far too many errors in this code. Can you guys help me fix them?
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <SD.h>
#include <TMRpcm.h>
#include <MPU6050.h>
#define BUTTON_PIN 2 // Pin for the button
#define NUM_PIXELS 80 // Number of pixels in each LED strip
#define LED_PIN 6 // Pin for the RGB LED strips (shared for 3 strips)
#define SD_PIN 10 // Pin for SD card
#define SPEAKER_PIN 9 // Pin for speaker output
#define I2C_SDA 21 // I2C SDA Pin
#define I2C_SCL 22 // I2C SCL Pin
// Declare the NeoPixel strip
Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Set up the TMRpcm for sound playback from the microSD card
TMRpcm audioPlayer;
// Set up MPU6050
MPU6050 mpu;
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool saberOn = false;
bool clashMode = false;
bool tipDragMode = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the LED strip
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Initialize the microSD card
if (!SD.begin(SD_PIN)) {
Serial.println("SD Card failed or not present.");
while (1); // Stop execution if SD card fails
}
// Initialize the audio player
audioPlayer.attach(SPEAKER_PIN);
audioPlayer.setVolume(5); // Set volume (0 to 7)
// Initialize the MPU6050 sensor
Wire.begin(I2C_SDA, I2C_SCL);
mpu.initialize();
// Initialize button pin
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("Setup complete. Lightsaber is ready!");
}
void loop() {
// Read button state with debounce logic
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
handleButtonPress();
}
}
}
lastButtonState = reading;
// Handle motion (impact, swing, tip drag)
handleMotion();
}
// Handle different button press behaviors
void handleButtonPress() {
if (!saberOn) {
turnOnSaber();
} else if (tipDragMode) {
tipDragEnd();
} else {
randomBlasterShot();
}
}
// Turn on the saber with effect and sound
void turnOnSaber() {
saberOn = true;
// Light up the LEDs from bottom to top as blue, 3 pixels per second
for (int i = 0; i < NUM_PIXELS; i++) {
for (int j = 0; j < 3; j++) {
int pixelIndex = i + j;
if (pixelIndex < NUM_PIXELS) {
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 255)); // Blue color
}
}
strip.show();
delay(333); // Delay to light up 3 pixels per second
}
// Play "Turn on" sound
audioPlayer.play("0001.wav"); // Assuming sound file for 'Turn On' is 0001.wav
// Start playing normal sound
audioPlayer.play("0002.wav"); // Normal sound (0002.wav)
}
// Turn off the saber with effect and sound (3 pixels per second from top to bottom)
void turnOffSaber() {
saberOn = false;
// Turn off the LEDs from top to bottom as blue, 3 pixels per second
for (int i = NUM_PIXELS - 1; i >= 0; i--) {
for (int j = 0; j < 3; j++) {
int pixelIndex = i - j;
if (pixelIndex >= 0) {
strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0)); // Off (turning off)
}
}
strip.show();
delay(333); // Delay to turn off 3 pixels per second
}
// Play "Turn off" sound
audioPlayer.play("0008.wav"); // Assuming sound file for 'Turn Off' is 0008.wav
}
// Handle motion (impact, swing, tip drag)
void handleMotion() {
// Read accelerometer data
Vector rawAccel = mpu.getAcceleration();
Vector rawGyro = mpu.getRotation();
// Check for impact (force detection)
if (abs(rawAccel.x) > 15000 || abs(rawAccel.y) > 15000 || abs(rawAccel.z) > 15000) {
handleImpact();
}
// Check for swing (fast or slow)
if (rawGyro.x > 500 || rawGyro.y > 500 || rawGyro.z > 500) {
handleSwing(rawGyro);
}
// Handle tip drag (sensor-based detection for downward and motion)
if (rawAccel.z < -7000) { // Threshold for pointing down
if (buttonState == LOW && !tipDragMode) { // Button pressed while pointing down
tipDragMode = true;
tipDragStart();
}
}
}
// Impact behavior (random 3 pixels turn white and play clash sound)
void handleImpact() {
if (tipDragMode) {
return; // Don't trigger clash sound if tip drag is active
}
clashMode = true;
// Set 3 random pixels to white for 1 second
for (int i = 0; i < 3; i++) {
int pixelIndex = random(NUM_PIXELS);
strip.setPixelColor(pixelIndex, strip.Color(255, 255, 255)); // White color
}
strip.show();
delay(1000); // Keep them white for 1 second
// Play random clash sound (from 5 possible clash sounds)
int soundIndex = random(1, 6); // Random number between 1 and 5
audioPlayer.play("clash" + String(soundIndex) + ".wav"); // Clash sound (e.g., clash1.wav, clash2.wav)
// Reset to normal sound after clash
audioPlayer.play("0002.wav"); // Normal sound
}
// Handle swing detection (fast and slow)
void handleSwing(Vector rawGyro) {
if (rawGyro.x > 1000 || rawGyro.y > 1000 || rawGyro.z > 1000) {
// Fast swing detected
audioPlayer.play("0004.wav"); // Fast swing sound (0004.wav)
} else {
// Slow swing detected
audioPlayer.play("0005.wav"); // Slow swing sound (0005.wav)
}
// Reset to normal sound
audioPlayer.play("0002.wav"); // Normal sound
}
// Random blaster shot behavior (button press when saber is on)
void randomBlasterShot() {
// Set 3 random pixels to white and play blaster sound
for (int i = 0; i < 3; i++) {
int pixelIndex = random(NUM_PIXELS);
strip.setPixelColor(pixelIndex, strip.Color(255, 255, 255)); // White color
}
strip.show();
delay(1000); // Keep them white for 1 second
// Play blaster shot sound
audioPlayer.play("0006.wav"); // Blaster sound (0006.wav)
// Reset to normal sound
audioPlayer.play("0002.wav"); // Normal sound
}
// Tip drag behavior (button press while pointing downwards)
void tipDragStart() {
// Turn the top pixels white
for (int i = 0; i < 10; i++) { // Top 10 pixels for tip drag
strip.setPixelColor(i, strip.Color(255, 255, 255)); // White color
}
strip.show();
// Play tip drag sound
audioPlayer.play("0007.wav"); // Tip drag sound (0007.wav)
}
void tipDragEnd() {
// Reset to normal sound and LEDs
for (int i = 0; i < NUM_PIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255)); // Blue color
}
strip.show();
// Play normal sound again
audioPlayer.play("0002.wav"); // Normal sound
}
