// DEFINES
// Flag to toggle debugging output
#define DEBUG
// INCLUDES
// For interfacing with analogue multiplexer
// https://github.com/stechio/arduino-ad-mux-lib
#include "/Users/jaylonlim/Documents/Arduino/Audio Puzzle/libraries/arduino-ad-mux-lib-master-2/src/Mux.h"
// DEPlayerMini module requires a serial connection. Since we're already using the hardware serial
// connection for the USB interface, we'll create an additional software-emulated serial connection
// using the SoftwareSerial library
#include <SoftwareSerial.h>
// This library exposes functions for playing sounds from DFPlayerMini module using the serial connection
//Download from https://github.com/DFRobot/DFRobotDFPlayerMini
#include "/Users/jaylonlim/Documents/Arduino/Audio Puzzle/libraries/DFRobotDFPlayerMini-master/DFRobotDFPlayerMini.h"
// CONSTANTS
//Define the number of steps in the sequence that the player must follow
const int numInputs = 10;
// The correct sequence of inputs required to solve the puzzle
const int correctSequence[numInputs] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = 12;
// GLOBALS
//We need to read 10 digital inputs, which we'll do using 4 control pins 3-6
Mux mux (2, INPUT, Digital, 6, 5, 4, 3);
//Mux mux(Pin(2, INPUT, PinType::Digital), Pinset(6, 5, 4, 3));
// 16-channel Mux declared with analog input signal on pin A0 and channel control on digital pins 8, 9, 10 and 11.
//Mux mux(Pin(A0, INPUT, PinType::Analog), Pinset(6, 5, 4, 3));
// Initialise a soft serial interface
SoftwareSerial softwareSerial(A0, A1); //Rx,Tx
// Create an object to access the dfPlayer
DFRobotDFPlayerMini dfPlayer;
// Array to keep track of the last state of every button input
bool lastInput[ // DEFINES
// Flag to toggle debugging output
#define DEBUG
//Initialisation
void Setup() {
#ifdef DEBUG
//Initialise serial communications channel with the PC
Serial.begin(9600);
delay(500);
//Initialise the software serial interface used to control the DFPlayer
Serial.print(F("Initialising software serial interface to DFPlayer..."));
#endif
for (int i = 0; i < 10; i++) {
softwareSerial.begin(9600);
delay(500);
if (dfPlayer.begin(softwareSerial)) {
#ifdef DEBUG
Serial.println(F("OR! "));
#endif
break;
} else {
Serial.print(".");
}
if (i == 9) {
#ifdef DEBUG
Serial.println(F("Failed : ("));
#endif
return;
}
}
// Set DFPlayer volume (value from 0 to 30)
dfPlayer.volume(24);
// set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
#ifdef DEBUG
Serial.println(F("Lock secured"));
#endif
// Debug
#ifdef DEBUG
Serial.println("Setup Complete");
#endif
}
//Main program loop runs indefinitely
void Loop() {
for (byte i = 0; i < numInputs; i++) {
//Reads from channel i
int currentReading = mux.read(i);
if (lastInput[i] == LOW && currentReading == HIGH) {
// Play the sound file called "0001 XXXX.way"/ "0001 XXXX.mp3" saved in the "mp3" folder of the SD card
dfPlayer.playMp3Folder(i + 1);
//Is this the correct input?
if (correctSequence[currentstep] == i) {
currentstep++;
#ifdef DEBUG
Serial.print(F("Correct input! Onto step #"));
Serial.println(currentstep);
#endif
// Check whether the puzzle has been solved
if (currentstep == numInputs) {
onSolve();
}
}
// Incorrect input!
else {
currentstep = 0;
Serial.print(F("Incorrect input! Expected"));
Serial.print(correctSequence[currentstep]);
Serial.print(F(" but got"));
Serial.println(i);
}
}
// Update the stored value for this input
lastInput[i] = currentReading;
}
}
// Takes action when the puzzle becomes solved
void onSolve() {
#ifdef DEBUG
// Print a message
Serial.println(F("Puzzle Solved!"));
#endif
// Release the lock
digitalWrite(lockPin, LOW);
delay(500);
digitalWrite(lockPin, HIGH);
// Reset the step counter
currentstep = 0;
}
];
//What step of the sequence is the player currently on?
int currentstep = 0;
//Initialisation
void Setup() {
#ifdef DEBUG
//Initialise serial communications channel with the PC
Serial.begin(9600);
delay(500);
//Initialise the software serial interface used to control the DFPlayer
Serial.print(F("Initialising software serial interface to DFPlayer..."));
#endif
for (int i = 0; i < 10; i++) {
softwareSerial.begin(9600);
delay(500);
if (dfPlayer.begin(softwareSerial)) {
#ifdef DEBUG
Serial.println(F("OR! "));
#endif
break;
} else {
Serial.print(".");
}
if (i == 9) {
#ifdef DEBUG
Serial.println(F("Failed : ("));
#endif
return;
}
}
// Set DFPlayer volume (value from 0 to 30)
dfPlayer.volume(24);
// set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
#ifdef DEBUG
Serial.println(F("Lock secured"));
#endif
// Debug
#ifdef DEBUG
Serial.println("Setup Complete");
#endif
}
//Main program loop runs indefinitely
void Loop() {
for (byte i = 0; i < numInputs; i++) {
//Reads from channel i
int currentReading = mux.read(i);
if (lastInput[i] == LOW && currentReading == HIGH) {
// Play the sound file called "0001 XXXX.way"/ "0001 XXXX.mp3" saved in the "mp3" folder of the SD card
dfPlayer.playMp3Folder(i + 1);
//Is this the correct input?
if (correctSequence[currentstep] == i) {
currentstep++;
#ifdef DEBUG
Serial.print(F("Correct input! Onto step #"));
Serial.println(currentstep);
#endif
// Check whether the puzzle has been solved
if (currentstep == numInputs) {
onSolve();
}
}
// Incorrect input!
else {
currentstep = 0;
Serial.print(F("Incorrect input! Expected"));
Serial.print(correctSequence[currentstep]);
Serial.print(F(" but got"));
Serial.println(i);
}
}
// Update the stored value for this input
lastInput[i] = currentReading;
}
}
// Takes action when the puzzle becomes solved
void onSolve() {
#ifdef DEBUG
// Print a message
Serial.println(F("Puzzle Solved!"));
#endif
// Release the lock
digitalWrite(lockPin, LOW);
delay(500);
digitalWrite(lockPin, HIGH);
// Reset the step counter
currentstep = 0;
}
ERRORS
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:28:1: error: 'Mux' does not name a type
Mux mux (2, INPUT, Digital, 6, 5, 4, 3);
^~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:42:3: error: expected primary-expression before 'void'
void Setup() {
^~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:42:3: error: expected ']' before 'void'
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino: In function 'void Loop()':
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:88:28: error: 'mux' was not declared in this scope
int currentReading = mux.read(i);
^~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:88:28: note: suggested alternative: 'max'
int currentReading = mux.read(i);
^~~
max
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:89:11: error: 'lastInput' was not declared in this scope
if (lastInput[i] == LOW && currentReading == HIGH) {
^~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:93:29: error: 'currentstep' was not declared in this scope
if (correctSequence[currentstep] == i) {
^~~~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:101:13: error: 'onSolve' was not declared in this scope
onSolve();
^~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:114:7: error: 'lastInput' was not declared in this scope
lastInput[i] = currentReading;
^~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino: In function 'void onSolve()':
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:130:5: error: 'currentstep' was not declared in this scope
currentstep = 0;
^~~~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino: At global scope:
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:133:1: error: expected unqualified-id before ']' token
];
^
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino: In function 'void Loop()':
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:182:6: error: redefinition of 'void Loop()'
void Loop() {
^~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:85:8: note: 'void Loop()' previously defined here
void Loop() {
^~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:185:26: error: 'mux' was not declared in this scope
int currentReading = mux.read(i);
^~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:185:26: note: suggested alternative: 'max'
int currentReading = mux.read(i);
^~~
max
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:186:9: error: 'lastInput' was not declared in this scope
if (lastInput[i] == LOW && currentReading == HIGH) {
^~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:211:5: error: 'lastInput' was not declared in this scope
lastInput[i] = currentReading;
^~~~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino: In function 'void onSolve()':
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:217:6: error: redefinition of 'void onSolve()'
void onSolve() {
^~~~~~~
/Users/jaylonlim/Documents/Arduino/Audio Puzzle/Audio Puzzle.ino:120:8: note: 'void onSolve()' previously defined here
void onSolve() {
^~~~~~~
exit status 1
Compilation error: 'Mux' does not name a type