So I have compiled 3 different routines into one code, but as I near the end of my project, I wanted to start fine-tuning the code by adding in a button trigger. The idea is to press the button to start the routine. Here is where I am stuck, I have added enough code that I'm not sure where my brackets should go and what is the "setup" and what is the "loop" in all of this.
#include <DFRobotDFPlayerMini.h>
#include <Stepper.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define BUTTON_PIN 8
#define BOX_PIN 9
//Box Routine Variable//
int boxRoutine;
//////////Create the Player object///////////////////////////////////////////////////
DFRobotDFPlayerMini player;
// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
//////////////////////////////////////////////////////////////////////////////////
//////////Create Light Variables///////////////////////////////////////////////////
int led = 9; // the PWM pin the LED is attached to
int brightness = 3; // how bright the LED is
int fadeAmount = 2; // how many points to fade the LED by
//////////////////////////////////////////////////////////////////////////////////
//////////Stepper Motor Variables///////////////////////////////////////////////////
// change this to the number of steps on your motor
#define STEPS 200
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);
//////////////////////////////////////////////////////////////////////////////////
void setup() {
//Button Trigger//
pinMode (BOX_PIN, OUTPUT);
pinMode (BUTTON_PIN, INPUT);
//Music Functions///////////////////////////////////////////
// Init USB serial port for debugging
Serial.begin(9600);
// Init serial port for DFPlayer Mini
softwareSerial.begin(9600);
// Start communication with DFPlayer Mini
if (player.begin(softwareSerial)) {
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(23);
// Play the first MP3 file on the SD card
player.play(1);
} else {
Serial.println("Connecting to DFPlayer Mini failed!");
}
/////////////////////////////////////////
//Fairy Lights/////////////////////////////////////////
pinMode(led, OUTPUT); //declare pin 9 to be an output
//////////////////////////////////////////////////////////////////
//Stepper Motor/////////////////////////////////////////
Serial.begin(9600);
Serial.println("Stepper test!");
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
///////////////////////////////////////////////////////////////
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(BOX_PIN, HIGH);
}
else {
digitalWrite(BOX_PIN, LOW);
}
}
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 1 || brightness >= 240) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(45);
//Stepper Motor Loop//
Serial.println("Forward");
stepper.step(STEPS);
Serial.println("Backward");
stepper.step(-STEPS);
}
I created a "int boxRoutine" variable in the off chance that I would require it store the other routines for the button. Not sure if that was necessary - hence why it was never used.
Next, according to the article I was reading, the loop designated the loop as:
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(BOX_PIN, HIGH);
}
else {
digitalWrite(BOX_PIN, LOW);
}
}
inside the loop(), but this was using an LED as an example and not a series of functions, so i was confused as to how to translate multiple processes into one. Hopefully this makes sense - furthermore, I hope my code makes sense.
By itself, the code is doing what it's supposed to do, what I would like it do with a button is wait for the button to be pressed to begin the entire routine at once and then stop once the song is finished