Problem with serial communication

I have the following problem with my Arduino project: i connected to Arduino a DFPlayermp3 and a keypad that inserting a String from the keypad matches a constant string inserted in the skretch, if the two strings matches it plays a specif track on the player. The skretch works fine but sometimes it doesn't take the input from the keypad after a while. What could be the problem?

The problem is probably in the code that you have not posted

#include <Keypad.h>
#include <string.h>
#include "DFRobotDFPlayerMini.h"
#include "Arduino.h"
#include <SoftwareSerial.h>
#define ROWS 4
#define COLS 4
#define LUNGHEZZA_CODICE 5
#define VOLUME 20
#define TRACCIA_1 1
#define TRACCIA_2 2
#define TRACCIA_3 3
#define TRACCIA_4 4
#define TRACCIA_5 5
#define TRACCIA_6 6
#define TRACCIA_7 7
#define TRACCIA_8 8
#define TRACCIA_9 9
#define TRACCIA_10 10

int aggiungiCarattere(char * str,char c,int index);
void resetCodice(char * str);
SoftwareSerial mySoftwareSerial(/*rx =*/12, /*tx =*/13);
DFRobotDFPlayerMini myDFPlayer;
char keymap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
const char indizio1[] = "2412";
const char indizio2[] = "722B";
const char indizio3[] = "A44A";
const char indizio4[] = "55AA";
const char indizio5[] = "64#9";
const char indizio6[] = "";
const char indizio7[] = "";
const char indizio8[] = "";
const char indizio9[] = "";
char codice[LUNGHEZZA_CODICE];
char key;
int index ;
Keypad keypad = Keypad(makeKeymap(keymap),rowPins,colPins,ROWS,COLS);




void setup() {
  
  
  codice[LUNGHEZZA_CODICE - 1] = 0;
  index = 0;
  mySoftwareSerial.begin(9600);
  Serial.begin (9600);
  pinMode(13,OUTPUT);
  if(!myDFPlayer.begin(mySoftwareSerial)){
    Serial.println("Verifica connessione");
    while(true);
  }
 

}

void loop() {

  if(myDFPlayer.readState() == 0 || myDFPlayer.readState() == 2){

   key = keypad.getKey();
   if(key){
    Serial.print("Tasto premuto: ");
    Serial.println(key);
    aggiungiCarattere(codice,key,index++);
    Serial.println(index);
    Serial.println(codice);
  }
  if(strlen(codice) == LUNGHEZZA_CODICE - 1){

  index = 0;

  if(strcmp(codice,indizio1) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_1);
 }
  else if(strcmp(codice,indizio2) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_2);
 }
  else if(strcmp(codice,indizio3) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_3);
 }
  else if(strcmp(codice,indizio4) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_4);
 }
  else if(strcmp(codice,indizio5) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_5);
 }
  else if(strcmp(codice,indizio6) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_6);
 }
  else if(strcmp(codice,indizio7) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_7);
 }
  else if(strcmp(codice,indizio8) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_8);
 }
  else if(strcmp(codice,indizio9) == 0){
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_9);
 }
 else {
  myDFPlayer.volume(VOLUME);  
  myDFPlayer.play(TRACCIA_10);
 }
 resetCodice(codice);

}
 
   
  }
  else {
   key = keypad.getKey();
    if(key == '*'){
      myDFPlayer.stop();
    }
  
  }
  
  
  

  
  


}


int aggiungiCarattere(char * str,char c,int index){
  if(strlen(str) < LUNGHEZZA_CODICE - 1){
  str[index] = c;
  Serial.println(str);
  return 0;
  }
  else
  return -1;
 }
 void resetCodice(char* str){
  for(int i = 0 ; i < LUNGHEZZA_CODICE -1; i++){
    str[i] = 0;
  }
 }

you only read the keys in this case

what are 0 or 2 representing?

Using arrays would make your code much easier

The 0 rapresents when the player is in Idle and 2 when is sleeping

so you don't want to listen to the keys when playing ?

there were reports in the past that the state was not always reported correctly

you could try something like this (typed here from your code, but using arrays)

#include <Keypad.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>

byte pinRighe[] = {9, 8, 7, 6};
byte pinColonne[] = {5, 4, 3, 2};

constexpr byte RIGHE = sizeof pinRighe;
constexpr byte COLONNE = sizeof pinColonne;

char mappaTasti[RIGHE][COLONNE] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};
Keypad tastiera = Keypad(makeKeymap(mappaTasti), pinRighe, pinColonne, RIGHE, COLONNE);

SoftwareSerial dfSerial(/*rx =*/12, /*tx =*/13);
DFRobotDFPlayerMini mp3Player;
const byte VOLUME = 20;

const byte LUNGHEZZA_CODICE = 4;
char codice[LUNGHEZZA_CODICE + 1];
byte posizioneCodice = 0;
bool nuovoCodiceRicevuto = false;

struct Tracce {
  const char * indizio;
  byte traccia;
};

Tracce tracce[] = {
  {"2412", 1},
  {"722B", 2},
  {"A44A", 3},
  {"55AA", 4},
  {"64#9", 5},
  {"", 6},
  {"", 7},
  {"", 8},
  {"", 9},
  {"", 10},
};
const byte conteggioTracce = sizeof tracce / sizeof * tracce;

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  dfSerial.begin(9600);

  if (!mp3Player.begin(dfSerial)) {
    Serial.println("Verifica connessione");
    while (true);
  }
  mp3Player.volume(VOLUME);
}

void loop() {
  int tasto = tastiera.getKey();
  if (tasto != NO_KEY) {
    Serial.print("Tasto premuto: ");
    Serial.println(tasto);
    codice[posizioneCodice++] = tasto;
    codice[posizioneCodice]  = '\0';
    if (posizioneCodice == LUNGHEZZA_CODICE) {
      nuovoCodiceRicevuto = true;
      posizioneCodice = 0;
    }
  }

  if (nuovoCodiceRicevuto) {
    for (byte indice = 0; indice < conteggioTracce; indice++) {
      if (strcmp(codice, tracce[indice].indizio) == 0) {
        mp3Player.stop();
        mp3Player.play(tracce[indice].traccia);
        break;
      }
    }
    nuovoCodiceRicevuto = false;
  }
}

Exactly, i don't, thanks for the code. I'll try and I make you know

the code above does listen for keys all the time and will stop the current song if it's playing and go to the next one if there is a match (supposedly - typed that here so fully untested)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.