Hi everyone
I’m using for the first time this DFPlayer.
I have already a code but I integrated on some lines for the audio.
To shorten things, below you will find my wokwi project (You will not find the lines for the Dfplayer programming)
Below also my real uploaded code to the Arduino board (Almost same wokwi code + audio coding)
The idea of the project is to choose a category of questions (Blue Buttons) and then pick the right answer (Green buttons)
For each choice an audio is played
Here what happen (Link to the video below):
the audio will work only when i choose the first answer (One of these green buttons/leds)
and then all buttons / leds will not work anymore (for making other choices)..
Could you please help .THANKS
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <LedControl.h>
static const uint8_t PIN_MP3_TX = 18;
static const uint8_t PIN_MP3_RX = 19;
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
DFRobotDFPlayerMini MP3player;
const int buzzer = 9;
const byte Blueled[] = {28, 26, 24, 22};
const byte Greenled[] = {39, 37, 35, 33, 31, 29, 27};
byte state_greenled [7][7] =
{
{1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1},
};
byte state_blueled [4][4] =
{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
};
const byte Bluebtn[] = {30, 32, 34, 36};
const byte Greenbtn[] = {41, 43, 45, 47, 49, 51, 53};
byte buttonState [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
byte lastbuttonstate [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const byte start = 10;
boolean shifting = true;
const byte adj2 = 8;
const byte adj1 = 7;
const byte nom2 = 6;
const byte nom1 = 5;
int DIN = 2;
int CS = 3;
int CLK = 4;
LedControl lc = LedControl(DIN, CLK, CS, 0);
static unsigned long timer = 0;
unsigned long interval = 50;
void setup()
{
Serial.begin(9600);
softwareSerial.begin(9600);
if (MP3player.begin(softwareSerial))
{
Serial.println("MP3 Player ready");
}
else
{
Serial.println("MP3 Player NOT READY");
}
MP3player.volume(27);
//INPUTS
for (int i = 0; i <= 6; i++)
{
pinMode(Greenbtn[i], INPUT_PULLUP);
}
for (int i = 0; i <= 3; i++)
{
pinMode(Bluebtn[i], INPUT_PULLUP);
}
pinMode(start, INPUT_PULLUP);
pinMode(adj1, INPUT_PULLUP);
pinMode(adj2, INPUT_PULLUP);
pinMode(nom1, INPUT_PULLUP);
pinMode(nom2, INPUT_PULLUP);
//OUTPUTS
for (int i = 0; i <= 6; i++)
{
pinMode(Greenled[i], OUTPUT);
}
for (int i = 0; i <= 3; i++)
{
pinMode(Blueled[i], OUTPUT);
}
lc.shutdown(0, false);
lc.setIntensity(0, 15);
}
//FUNCTIONS
boolean selectedgreenled()//check if there is a choice that has been done
{
for (int i = 0; i <= 6; i++)
{
if (digitalRead(Greenled[i]) == HIGH) true; else false;
}
}
boolean ledongreen(int x)
{
if (millis() - timer > interval)
{
buttonState[x] = digitalRead(Greenbtn[x]);
if (buttonState[x] != lastbuttonstate [x])
{
timer = millis();
lastbuttonstate [x] = buttonState[x];
if (buttonState[x] == LOW)
{
for (int i = 0; i <= 6; i++) digitalWrite(Greenled[i], state_greenled [x][i]);
}
}
}
}
boolean ledonblue(int x)
{
if (millis() - timer > interval)
{
buttonState[x + 4] = digitalRead(Bluebtn[x]);
if (buttonState[x + 4] != lastbuttonstate [x + 4])
{
timer = millis();
lastbuttonstate [x + 4] = buttonState[x + 4];
if (buttonState[x + 4] == LOW)
{
for (int i = 0; i <= 3 ; i++) digitalWrite(Blueled[i], state_blueled [x][i]);
for (int i = 0; i <= 6; i++) digitalWrite(Greenled[i], LOW); lc.clearDisplay(0);
}
}
}
}
void smile()
{
byte SMILE[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
for (int i = 0; i < 8; i++)
{
lc.setRow(0, i, SMILE[i]);
}
}
void sad()
{
byte SAD[8] = {0x3C, 0x42, 0xA5, 0x81, 0x99, 0xA5, 0x42, 0x3C};
for (int i = 0; i < 8; i++)
{
lc.setRow(0, i, SAD[i]);
}
}
void loop()
{
if (digitalRead(start) == LOW)
{
lc.clearDisplay(0);
for (int i = 0; i <= 6; i++) digitalWrite(Greenled[i], LOW);
tone(buzzer, 1000);
delay(250);
noTone(buzzer);
delay(80);
shifting = !shifting;
}
if (shifting == false)
{
for (int i = 0; i <= 6; i++) digitalWrite(Greenled[i], LOW);
for (int i = 0; i <= 3; i++) digitalWrite(Blueled[i], LOW);
digitalWrite(Blueled[random(0, 4)], HIGH);
tone(buzzer, 300);
delay(80);
noTone(buzzer);
delay(50);
}
ledonblue(0);
ledonblue(1);
ledonblue(2);
ledonblue(3);
ledongreen(0);
ledongreen(1);
ledongreen(2);
ledongreen(3);
ledongreen(4);
ledongreen(5);
ledongreen(6);
unsigned pinState = 0;
for (int n = 0; n <= 6; n++)
pinState |= digitalRead (Greenled [n]) << n;
Serial.println (pinState);
switch (pinState) {
case 1: //"x" - MS & Adj1 OU MP & Nom1 OU MP & Adj1
if (digitalRead(Blueled[0]) == HIGH && pinState != 0 )
{
smile(); MP3player.play(14);
}
else {
sad();
}
break;
case 2: //"e" -Feminin singulier & Adjectif type 1
if (digitalRead(Blueled[1]) == HIGH && pinState != 0)
{
smile();MP3player.play(14);
}
else {
sad();
}
break;
case 4: //"es" - Feminin Pluriel & Adjectif type 1
if (digitalRead(Blueled[3]) == HIGH && digitalRead(adj1) == HIGH && pinState != 0)
{
smile();
}
else {
sad();
}
break;
case 8: //"s" - Feminin singulier & Adjectif type 1 OU Feminin singulier & Nom type 1
if ((digitalRead(Blueled[2]) == HIGH && (digitalRead(adj2) == HIGH || digitalRead(nom2) == HIGH) || digitalRead(Blueled[3]) == HIGH && (digitalRead(nom1) == HIGH || digitalRead(nom2) == HIGH)) && pinState != 0 )
{
smile();
}
else {
sad();
}
break;
case 16: //"se" - Feminin singulier & Adjectif type 2
if (digitalRead(Blueled[1]) == HIGH && digitalRead(adj2) == HIGH && pinState != 0)
{
smile();
}
else {
sad();
}
break;
case 32: //"ses" - Feminin Pluriel & Adjectif type 2
if (digitalRead(Blueled[3]) == HIGH && digitalRead(adj2) == HIGH && pinState != 0)
{
smile();
}
else {
sad();
}
break;
case 64: //"Rien"
if ((digitalRead(Blueled[0]) == HIGH && (digitalRead(adj2) == HIGH || digitalRead(nom1) == HIGH || digitalRead(nom2) == HIGH) || digitalRead(Blueled[1]) == HIGH && digitalRead(nom2) == HIGH) && pinState != 0)
{
smile();
}
else {
sad();
}
break;
}
}