Go Down

Topic: DFplayer Mini starts 2 times a file (Read 71 times) previous topic - next topic

Tissi_2

Hi folks!
I am working on a "safe project". I have a lighted main switch, a rotary click encoder, a reed contact, a LED stripe, a LED matrix and a DFplayer. When you put in a certain code with the rotary encoder, the code will be examined.

code right --> open safe, play sound, light up LED strip, relais is activated to open door.
code wrong --> safe still closed, play other sound, light up LED strip in red.

I got the problem that if the code has been entered and compared, the sound starts to play for approx 1 second and then starts again to play it the full length. I am not quite sure why this happens. I assume that it could be the condition code in the loop function.

    if (buttoncounter2 == 9 && angeschaltet==true && code1==8 && code2==4 && code3==11 && rep <3)

The question is - why does it start the sound twice?

Some translations/explainations:
angeschaltet = turned on // lighted main switch pushed at the very beginning
buttonCounter = lighted main button switch counter
buttonCounter2 = rotary click encoder counter
encoderValue = rotary encoder position
code i = when the rotary encoder is pushed, the encoderValue will be stored
rep = another counter


Here is the code:

Code: [Select]
void loop() {

proximity = digitalRead(REED_PIN);
newState = digitalRead( switchPin );
clickEncoder();
  if ( lastState != newState ) {
   
    delay( debounceTime );
    lastState = newState;

    if ( newState == switchOn && toggleState == false ) {
      toggleState = true;
      buttonCounter++;

      digitalWrite( ledPin, HIGH);

    }
    else if ( newState == switchOn && toggleState == true ) {
      toggleState = false;
      buttonCounter++;
      currentMillis2=millis();
      digitalWrite( ledPin, LOW );
      digitalWrite( relaisPin1, LOW );
    }
  }

  if (buttonCounter %2!=0 && millis()-lastFadeTime > 35 && proximity == LOW) {
      pulsingLED();
      }
  if (buttonCounter %2!=0 && millis()-lastFadeTime > 35 && proximity == HIGH) {
      digitalWrite( ledPin, LOW);
      }
  if ( buttonCounter %2==0 && buttonCounter!=0 && millis()-currentMillis2>=2000) {

      digitalWrite( ledPin, HIGH);
     if (rep <2){
     myDFPlayer.play(3);
     delay(20000);
     rep++;
      angeschaltet = true;
      buttoncounter2=0;
      }   
  }
    if (buttoncounter2 == 1 && angeschaltet==true) {
        anfang();
    }
    if (buttoncounter2 == 2 && angeschaltet==true) {
        knopfposition();
        code1=(encoderValue%12);
    }
    if (buttoncounter2 == 3 && angeschaltet==true) {
        gespeichert();
    }
    if (buttoncounter2 == 4 && angeschaltet==true) {
        knopfposition();
        code2=(encoderValue%12);
    }
    if (buttoncounter2 == 5 && angeschaltet==true) {
        gespeichert();
    }
    if (buttoncounter2 == 6 && angeschaltet==true) {
        knopfposition();
        code3=(encoderValue%12);     
    }
    if (buttoncounter2 == 7 && angeschaltet==true) {
        gespeichert();
    }
    if (buttoncounter2 == 8 && angeschaltet==true) {
        text();
    }
    if (buttoncounter2 == 9 && angeschaltet==true && code1==8 && code2==4 && code3==11 && rep <3){
       
        myDFPlayer.play(5);
        delay(5000);
        myDFPlayer.play(6);
        rgbFadeIn_Hold_Out(strip.Color(255,120,0),numLoops1);
        digitalWrite( relaisPin1, HIGH );
        strip.clear();
        strip.show();
        rep++;
    }
    if (buttoncounter2 ==9 && angeschaltet==true && (code1!=8 || code2!=4 || code3!=11) && rep <3 ){
        myDFPlayer.play(5);
        delay(5000);
        myDFPlayer.play(7);
        delay(1000);
        rep++;
    }
    if (buttoncounter2>10){matrix.clear();
        matrix.writeDisplay();}
    else{}
}

Go Up