I had a "Serial.begin(9600);" in my setup function but I've since commented it out and the application sketch fails to function still if just supplied with external power. It application sketch only works after upload. Using a jumper to connect RST and GND doesn't seem to do anything.
Here's my full sketch. It's a pro micro with 26 buttons and 5 pots. All it does right now is detect button presses and play sounds via the sound module.
#include <Control_Surface.h> // Include the Control Surface library
#include <AH/Hardware/ExtendedInputOutput/AnalogMultiplex.hpp>
// Uncomment this line to debug the values the remote is sending across the wire.
//#define debug
//CCPotentiometer volumePotentiometers[] {
// { A0, { MIDI_CC::Channel_Volume, CHANNEL_1 } },
// { A1, { MIDI_CC::Channel_Volume, CHANNEL_2 } },
// { A2, { MIDI_CC::Channel_Volume, CHANNEL_3 } },
// { A3, { MIDI_CC::Channel_Volume, CHANNEL_4 } },
// { A10, { MIDI_CC::Channel_Volume, CHANNEL_5 } },
//};
// Instantiate a multiplexer
CD74HC4067 mux_1{4, {6, 7, 8, 9} , }; //input, S0-S3, optional enable
CD74HC4067 mux_2{14, {6, 7, 8, 9} , }; //input, S0-S3, optional enable
//CCButton A1_But1 {mux_1.pin(0), {MIDI_CC::General_Purpose_Controller_1, CHANNEL_1},};
//CCButton A1_But2 {mux_1.pin(1), {MIDI_CC::General_Purpose_Controller_2, CHANNEL_1},};
//CCButton A1_But3 {mux_1.pin(2), {MIDI_CC::General_Purpose_Controller_3, CHANNEL_1},};
//CCButton A1_But4 {mux_1.pin(3), {MIDI_CC::General_Purpose_Controller_4, CHANNEL_1},};
//CCButton A1_But5 {mux_1.pin(4), {MIDI_CC::General_Purpose_Controller_5, CHANNEL_1},};
//CCButton A1_But6 {mux_1.pin(5), {MIDI_CC::General_Purpose_Controller_6, CHANNEL_1},};
//CCButton A1_But7 {mux_1.pin(6), {MIDI_CC::General_Purpose_Controller_7, CHANNEL_1},};
//CCButton A1_But8 {mux_1.pin(7), {MIDI_CC::General_Purpose_Controller_8, CHANNEL_1},};
//CCButton A2_But1 {mux_1.pin(8), {MIDI_CC::General_Purpose_Controller_1, CHANNEL_2},};
//CCButton A2_But2 {mux_1.pin(9), {MIDI_CC::General_Purpose_Controller_2, CHANNEL_2},};
//CCButton A2_But3 {mux_1.pin(10), {MIDI_CC::General_Purpose_Controller_3, CHANNEL_2},};
//CCButton A2_But4 {mux_1.pin(11), {MIDI_CC::General_Purpose_Controller_4, CHANNEL_2},};
//CCButton A2_But5 {mux_1.pin(12), {MIDI_CC::General_Purpose_Controller_5, CHANNEL_2},};
//CCButton A2_But6 {mux_1.pin(13), {MIDI_CC::General_Purpose_Controller_6, CHANNEL_2},};
//CCButton A2_But7 {mux_1.pin(14), {MIDI_CC::General_Purpose_Controller_7, CHANNEL_2},};
//CCButton A2_But8 {mux_1.pin(15), {MIDI_CC::General_Purpose_Controller_8, CHANNEL_2},};
//CCButton B1_But1 {mux_2.pin(0), {MIDI_CC::General_Purpose_Controller_1, CHANNEL_3},};
//CCButton B1_But2 {mux_2.pin(1), {MIDI_CC::General_Purpose_Controller_2, CHANNEL_3},};
//CCButton B2_But1 {mux_2.pin(2), {MIDI_CC::General_Purpose_Controller_1, CHANNEL_4},};
//CCButton B2_But2 {mux_2.pin(3), {MIDI_CC::General_Purpose_Controller_2, CHANNEL_4},};
//CCButton B2_But3 {mux_2.pin(4), {MIDI_CC::General_Purpose_Controller_3, CHANNEL_4},};
//CCButton B2_But4 {mux_2.pin(5), {MIDI_CC::General_Purpose_Controller_4, CHANNEL_4},};
//CCButton B2_But5 {mux_2.pin(6), {MIDI_CC::General_Purpose_Controller_5, CHANNEL_4},};
//CCButton B2_But6 {mux_2.pin(7), {MIDI_CC::General_Purpose_Controller_6, CHANNEL_4},};
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(16, 5); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int playSoundIndex = 1;
//A1
#define A1_But1PIN 0
#define A1_But2PIN 1
#define A1_But3PIN 2
#define A1_But4PIN 3
#define A1_But5PIN 4
#define A1_But6PIN 5
#define A1_But7PIN 6
#define A1_But8PIN 7
//A2
#define A2_But1PIN 8
#define A2_But2PIN 9
#define A2_But3PIN 10
#define A2_But4PIN 11
#define A2_But5PIN 12
#define A2_But6PIN 13
#define A2_But7PIN 14
#define A2_But8PIN 15
//B1
#define B1_But1PIN 0
#define B1_But2PIN 1
#define B1_Pot1PIN A0
#define B1_Pot2PIN A1
#define B1_Pot3PIN A2
#define B1_Pot4PIN A3
#define B1_Pot5PIN A10
//B2
#define B2_But1PIN 2
#define B2_But2PIN 3
#define B2_But3PIN 4
#define B2_But4PIN 5
#define B2_But5PIN 6
#define B2_But6PIN 7
struct SEND_DATA_STRUCTURE
{
uint8_t A1_But1 = 0;
uint8_t A1_But2 = 0;
uint8_t A1_But3 = 0;
uint8_t A1_But4 = 0;
uint8_t A1_But5 = 0;
uint8_t A1_But6 = 0;
uint8_t A1_But7 = 0;
uint8_t A1_But8 = 0;
uint8_t A2_But1 = 0;
uint8_t A2_But2 = 0;
uint8_t A2_But3 = 0;
uint8_t A2_But4 = 0;
uint8_t A2_But5 = 0;
uint8_t A2_But6 = 0;
uint8_t A2_But7 = 0;
uint8_t A2_But8 = 0;
int B1_Pot1 = 512;
int B1_Pot2 = 512;
int B1_Pot3 = 512;
int B1_Pot4 = 512;
int B1_Pot5 = 512;
uint8_t B1_But1 = 0;
uint8_t B1_But2 = 0;
uint8_t B2_But1 = 0;
uint8_t B2_But2 = 0;
uint8_t B2_But3 = 0;
uint8_t B2_But4 = 0;
uint8_t B2_But5 = 0;
uint8_t B2_But6 = 0;
} sendToSerial;
int lastA1_But1State = HIGH; // the previous state from the input pin
int lastA1_But2State = HIGH; // the previous state from the input pin
int lastA1_But3State = HIGH; // the previous state from the input pin
int lastA1_But4State = HIGH; // the previous state from the input pin
int lastA1_But5State = HIGH; // the previous state from the input pin
int lastA1_But6State = HIGH; // the previous state from the input pin
int lastA1_But7State = HIGH; // the previous state from the input pin
int lastA1_But8State = HIGH; // the previous state from the input pin
int lastA2_But1State = HIGH; // the previous state from the input pin
int lastA2_But2State = HIGH; // the previous state from the input pin
int lastA2_But3State = HIGH; // the previous state from the input pin
int lastA2_But4State = HIGH; // the previous state from the input pin
int lastA2_But5State = HIGH; // the previous state from the input pin
int lastA2_But6State = HIGH; // the previous state from the input pin
int lastA2_But7State = HIGH; // the previous state from the input pin
int lastA2_But8State = HIGH; // the previous state from the input pin
int lastB1_But1State = HIGH; // the previous state from the input pin
int lastB1_But2State = HIGH; // the previous state from the input pin
int lastB2_But1State = HIGH; // the previous state from the input pin
int lastB2_But2State = HIGH; // the previous state from the input pin
int lastB2_But3State = HIGH; // the previous state from the input pin
int lastB2_But4State = HIGH; // the previous state from the input pin
int lastB2_But5State = HIGH; // the previous state from the input pin
int lastB2_But6State = HIGH; // the previous state from the input pin
void setup() {
// put your setup code here, to run once:
mySoftwareSerial.begin(9600);
//Serial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30); //Set volume value. From 0 to 30
//myDFPlayer.play(1); //Play the first mp3
mux_1.begin();
mux_2.begin();
for(int i=0; i<=16; i++)
{
mux_1.pinMode(i, INPUT_PULLUP);
mux_2.pinMode(i, INPUT_PULLUP);
}
}
void loop() {
// put your main code here, to run repeatedly:
sendToSerial.A1_But1 = mux_1.digitalRead(A1_But1PIN);
sendToSerial.A1_But2 = mux_1.digitalRead(A1_But2PIN);
sendToSerial.A1_But3 = mux_1.digitalRead(A1_But3PIN);
sendToSerial.A1_But4 = mux_1.digitalRead(A1_But4PIN);
sendToSerial.A1_But5 = mux_1.digitalRead(A1_But5PIN);
sendToSerial.A1_But6 = mux_1.digitalRead(A1_But6PIN);
sendToSerial.A1_But7 = mux_1.digitalRead(A1_But7PIN);
sendToSerial.A1_But8 = mux_1.digitalRead(A1_But8PIN);
sendToSerial.A2_But1 = mux_1.digitalRead(A2_But1PIN);
sendToSerial.A2_But2 = mux_1.digitalRead(A2_But2PIN);
sendToSerial.A2_But3 = mux_1.digitalRead(A2_But3PIN);
sendToSerial.A2_But4 = mux_1.digitalRead(A2_But4PIN);
sendToSerial.A2_But5 = mux_1.digitalRead(A2_But5PIN);
sendToSerial.A2_But6 = mux_1.digitalRead(A2_But6PIN);
sendToSerial.A2_But7 = mux_1.digitalRead(A2_But7PIN);
sendToSerial.A2_But8 = mux_1.digitalRead(A2_But8PIN);
sendToSerial.B1_But1 = mux_2.digitalRead(B1_But1PIN);
sendToSerial.B1_But2 = mux_2.digitalRead(B1_But2PIN);
sendToSerial.B1_Pot1 = analogRead(B1_Pot1PIN);
sendToSerial.B1_Pot2 = analogRead(B1_Pot2PIN);
sendToSerial.B1_Pot3 = analogRead(B1_Pot3PIN);
sendToSerial.B1_Pot4 = analogRead(B1_Pot4PIN);
sendToSerial.B1_Pot5 = analogRead(B1_Pot5PIN);
sendToSerial.B2_But1 = mux_2.digitalRead(B2_But1PIN);
sendToSerial.B2_But2 = mux_2.digitalRead(B2_But2PIN);
sendToSerial.B2_But3 = mux_2.digitalRead(B2_But3PIN);
sendToSerial.B2_But4 = mux_2.digitalRead(B2_But4PIN);
sendToSerial.B2_But5 = mux_2.digitalRead(B2_But5PIN);
sendToSerial.B2_But6 = mux_2.digitalRead(B2_But6PIN);
//A1
if(lastA1_But1State == HIGH && sendToSerial.A1_But1 == LOW){
Serial.println("A1_But1 The button is pressed");
playNextSound();
}
else if(lastA1_But1State == LOW && sendToSerial.A1_But1 == HIGH)
Serial.println("A1_But1 The button is released");
// save the the last state
lastA1_But1State = sendToSerial.A1_But1;
if(lastA1_But2State == HIGH && sendToSerial.A1_But2 == LOW){
Serial.println("A1_But2 The button is pressed");
playNextSound();
}
else if(lastA1_But2State == LOW && sendToSerial.A1_But2 == HIGH)
Serial.println("A1_But2 The button is released");
// save the the last state
lastA1_But2State = sendToSerial.A1_But2;
if(lastA1_But3State == HIGH && sendToSerial.A1_But3 == LOW) {
Serial.println("A1_But3 The button is pressed");
playNextSound();
}
else if(lastA1_But3State == LOW && sendToSerial.A1_But3 == HIGH)
Serial.println("A1_But3 The button is released");
// save the the last state
lastA1_But3State = sendToSerial.A1_But3;
if(lastA1_But4State == HIGH && sendToSerial.A1_But4 == LOW) {
Serial.println("A1_But4 The button is pressed");
playNextSound();
}
else if(lastA1_But4State == LOW && sendToSerial.A1_But4 == HIGH)
Serial.println("A1_But4 The button is released");
// save the the last state
lastA1_But4State = sendToSerial.A1_But4;
if(lastA1_But5State == HIGH && sendToSerial.A1_But5 == LOW) {
Serial.println("A1_But5 The button is pressed");
playNextSound();
}
else if(lastA1_But5State == LOW && sendToSerial.A1_But5 == HIGH)
Serial.println("A1_But5 The button is released");
// save the the last state
lastA1_But5State = sendToSerial.A1_But5;
if(lastA1_But6State == HIGH && sendToSerial.A1_But6 == LOW) {
Serial.println("A1_But6 The button is pressed");
playNextSound();
}
else if(lastA1_But6State == LOW && sendToSerial.A1_But6 == HIGH)
Serial.println("A1_But6 The button is released");
// save the the last state
lastA1_But6State = sendToSerial.A1_But6;
if(lastA1_But7State == HIGH && sendToSerial.A1_But7 == LOW) {
Serial.println("A1_But7 The button is pressed");
playNextSound();
}
else if(lastA1_But7State == LOW && sendToSerial.A1_But7 == HIGH)
Serial.println("A1_But7 The button is released");
// save the the last state
lastA1_But7State = sendToSerial.A1_But7;
if(lastA1_But8State == HIGH && sendToSerial.A1_But8 == LOW) {
Serial.println("A1_But8 The button is pressed");
playNextSound();
}
else if(lastA1_But8State == LOW && sendToSerial.A1_But8 == HIGH)
Serial.println("A1_But8 The button is released");
// save the the last state
lastA1_But8State = sendToSerial.A1_But8;
//A2
if(lastA2_But1State == HIGH && sendToSerial.A2_But1 == LOW){
Serial.println("A2_But1 The button is pressed");
playNextSound();
}
else if(lastA2_But1State == LOW && sendToSerial.A2_But1 == HIGH)
Serial.println("A2_But1 The button is released");
// save the the last state
lastA1_But2State = sendToSerial.A2_But1;
if(lastA2_But2State == HIGH && sendToSerial.A2_But2 == LOW){
Serial.println("A2_But2 The button is pressed");
playNextSound();
}
else if(lastA2_But2State == LOW && sendToSerial.A2_But2 == HIGH)
Serial.println("A2_But2 The button is released");
// save the the last state
lastA2_But2State = sendToSerial.A2_But2;
if(lastA2_But3State == HIGH && sendToSerial.A2_But3 == LOW) {
Serial.println("A2_But3 The button is pressed");
playNextSound();
}
else if(lastA2_But3State == LOW && sendToSerial.A2_But3 == HIGH)
Serial.println("A2_But3 The button is released");
// save the the last state
lastA2_But3State = sendToSerial.A2_But3;
if(lastA2_But4State == HIGH && sendToSerial.A2_But4 == LOW) {
Serial.println("A2_But4 The button is pressed");
playNextSound();
}
else if(lastA2_But4State == LOW && sendToSerial.A2_But4 == HIGH)
Serial.println("A2_But4 The button is released");
// save the the last state
lastA2_But4State = sendToSerial.A2_But4;
if(lastA2_But5State == HIGH && sendToSerial.A2_But5 == LOW) {
Serial.println("A2_But4 The button is pressed");
playNextSound();
}
else if(lastA2_But5State == LOW && sendToSerial.A2_But5 == HIGH)
Serial.println("A2_But4 The button is released");
// save the the last state
lastA2_But5State = sendToSerial.A2_But5;
if(lastA2_But6State == HIGH && sendToSerial.A2_But6 == LOW) {
Serial.println("A2_But6 The button is pressed");
playNextSound();
}
else if(lastA2_But6State == LOW && sendToSerial.A2_But6 == HIGH)
Serial.println("A2_But6 The button is released");
// save the the last state
lastA2_But6State = sendToSerial.A2_But6;
if(lastA2_But7State == HIGH && sendToSerial.A2_But7 == LOW) {
Serial.println("A2_But7 The button is pressed");
playNextSound();
}
else if(lastA2_But7State == LOW && sendToSerial.A2_But7 == HIGH)
Serial.println("A2_But7 The button is released");
// save the the last state
lastA2_But7State = sendToSerial.A2_But7;
if(lastA2_But8State == HIGH && sendToSerial.A2_But8 == LOW) {
Serial.println("A2_But8 The button is pressed");
playNextSound();
}
else if(lastA2_But8State == LOW && sendToSerial.A2_But8 == HIGH)
Serial.println("A2_But8 The button is released");
// save the the last state
lastA2_But8State = sendToSerial.A2_But8;
//B1
if(lastB1_But1State == HIGH && sendToSerial.B1_But1 == LOW){
Serial.println("B1_But1 The button is pressed");
playNextSound();
}
else if(lastB1_But1State == LOW && sendToSerial.B1_But1 == HIGH)
Serial.println("B1_But1 The button is released");
// save the the last state
lastB1_But1State = sendToSerial.B1_But1;
if(lastB1_But2State == HIGH && sendToSerial.B1_But2 == LOW){
Serial.println("B1_But2 The button is pressed");
playNextSound();
}
else if(lastB1_But2State == LOW && sendToSerial.B1_But2 == HIGH)
Serial.println("B1_But2 The button is released");
// save the the last state
lastB1_But2State = sendToSerial.B1_But2;
//B2
if(lastB2_But1State == HIGH && sendToSerial.B2_But1 == LOW){
Serial.println("B2_But1 The button is pressed");
playNextSound();
}
else if(lastB2_But1State == LOW && sendToSerial.B2_But1 == HIGH)
Serial.println("B2_But1 The button is released");
// save the the last state
lastB2_But1State = sendToSerial.B2_But1;
if(lastB2_But2State == HIGH && sendToSerial.B2_But2 == LOW){
Serial.println("B2_But2 The button is pressed");
playNextSound();
}
else if(lastB2_But2State == LOW && sendToSerial.B2_But2 == HIGH)
Serial.println("B2_But2 The button is released");
// save the the last state
lastB2_But2State = sendToSerial.B2_But2;
if(lastB2_But3State == HIGH && sendToSerial.B2_But3 == LOW) {
Serial.println("B2_But3 The button is pressed");
playNextSound();
}
else if(lastB2_But3State == LOW && sendToSerial.B2_But3 == HIGH)
Serial.println("B2_But3 The button is released");
// save the the last state
lastB2_But3State = sendToSerial.B2_But3;
if(lastB2_But4State == HIGH && sendToSerial.B2_But4 == LOW) {
Serial.println("B2_But4 The button is pressed");
playNextSound();
}
else if(lastB2_But4State == LOW && sendToSerial.B2_But4 == HIGH)
Serial.println("B2_But4 The button is released");
// save the the last state
lastB2_But4State = sendToSerial.B2_But4;
if(lastB2_But5State == HIGH && sendToSerial.B2_But5 == LOW) {
Serial.println("B2_But5 The button is pressed");
playNextSound();
}
else if(lastB2_But5State == LOW && sendToSerial.B2_But5 == HIGH)
Serial.println("B2_But5 The button is released");
// save the the last state
lastB2_But5State = sendToSerial.B2_But5;
if(lastB2_But6State == HIGH && sendToSerial.B2_But6 == LOW) {
Serial.println("B2_But6 The button is pressed");
playNextSound();
}
else if(lastB2_But6State == LOW && sendToSerial.B2_But6 == HIGH)
Serial.println("B2_But6 The button is released");
// save the the last state
lastB2_But6State = sendToSerial.B2_But6;
#ifdef debug
debugRoutine();
#endif
}
void playNextSound()
{
if(playSoundIndex==131){
playSoundIndex=1;
} else{
playSoundIndex++;
}
myDFPlayer.playMp3Folder(playSoundIndex);
}
void debugRoutine()
{
Serial.print(sendToSerial.A1_But1); Serial.print(", ");
Serial.print(sendToSerial.A1_But2); Serial.print(", ");
Serial.print(sendToSerial.A1_But3); Serial.print(", ");
Serial.print(sendToSerial.A1_But4); Serial.print(", ");
Serial.print(sendToSerial.A1_But5); Serial.print(", ");
Serial.print(sendToSerial.A1_But6); Serial.print(", ");
Serial.print(sendToSerial.A1_But7); Serial.print(", ");
Serial.print(sendToSerial.A1_But8); Serial.print(", ");
Serial.println();
Serial.print(sendToSerial.A2_But1); Serial.print(", ");
Serial.print(sendToSerial.A2_But2); Serial.print(", ");
Serial.print(sendToSerial.A2_But3); Serial.print(", ");
Serial.print(sendToSerial.A2_But4); Serial.print(", ");
Serial.print(sendToSerial.A2_But5); Serial.print(", ");
Serial.print(sendToSerial.A2_But6); Serial.print(", ");
Serial.print(sendToSerial.A2_But7); Serial.print(", ");
Serial.print(sendToSerial.A2_But8); Serial.print(", ");
Serial.println();
Serial.print(sendToSerial.B1_Pot1); Serial.print(", ");
Serial.print(sendToSerial.B1_Pot2); Serial.print(", ");
Serial.print(sendToSerial.B1_Pot3); Serial.print(", ");
Serial.print(sendToSerial.B1_Pot4); Serial.print(", ");
Serial.print(sendToSerial.B1_Pot5); Serial.print(", ");
Serial.print(sendToSerial.B1_But1); Serial.print(", ");
Serial.print(sendToSerial.B1_But2); Serial.print(", ");
Serial.println();
Serial.print(sendToSerial.B2_But1); Serial.print(", ");
Serial.print(sendToSerial.B2_But2); Serial.print(", ");
Serial.print(sendToSerial.B2_But3); Serial.print(", ");
Serial.print(sendToSerial.B2_But4); Serial.print(", ");
Serial.print(sendToSerial.B2_But5); Serial.print(", ");
Serial.print(sendToSerial.B2_But6);
Serial.println();
}