Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by wingburen ( 6 years ago )
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

#define CoindoorPin 5                                 // yellow wire
#define LFlipperPin 6                                 // green wire
#define RFlipperPin 7                                 // red wire
#define StartPin 8                                    // orange wire

unsigned long previousMillis=0;                       // millis() returns an unsigned long.
unsigned long attract_time=10 * 1000;                 // time to wait befor init attract sound, 10 sec

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2;                  // Connects to module's RX
static const uint8_t PIN_MP3_RX = 3;                  // Connects to module's TX

int volume = 18;                                      // default volume 6,12,18
int attract_volume = 4;                               // attract mode volume
int inputdelay = 100;                                 // Delay after button press


// variables to cycle the samples for each segment
byte coin = 1;                                        // Cycles w. coindoor button [01-07]
byte music = 8;                                       // Cycles w. right flipper button [08-16]
byte sample = 17;                                     // Cycles w. flipper left button [17-24]
byte start = 25;                                      // Cycles w. start button [25]

SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {
  // Init USB serial port for debugging
  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  pinMode(LFlipperPin, INPUT_PULLUP);
  pinMode(RFlipperPin, INPUT_PULLUP);
  pinMode(StartPin, INPUT_PULLUP);
  pinMode(CoindoorPin, INPUT_PULLUP);

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
    Serial.println("OK");

    player.volume(volume);

  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }
}

void loop() {
  unsigned long currentMillis = millis(); // grab current time
  // read status to variables
  int Coindoor = digitalRead(CoindoorPin);
  int LFlipper = digitalRead(LFlipperPin);
  int RFlipper = digitalRead(RFlipperPin);
  int Start = digitalRead(StartPin);

  if ((unsigned long)(currentMillis - previousMillis) >= attract_time)  { // Check if the attract time is passed
    player.volume(attract_volume);                                        // Set attract mode volume
    player.play(8);                                                       // Attract mode - Play main theme
    delay(inputdelay);
    previousMillis = millis();
  }

    //----------------------------------------------------------------------------------------
    // Samples 01-07
    if (Coindoor == LOW) {
      player.volume(volume);
      player.play(coin);
      coin++;
      if (coin == 8) {
        coin = 1;
      }
      delay(inputdelay);
    }
    while (digitalRead(CoindoorPin) == LOW) {}          // Loop as long as the switch is pressed
    //----------------------------------------------------------------------------------------
    // Samples 08-16
    if (RFlipper == LOW) {
      player.volume(volume);
      player.play(music);
      music++;
      if (music == 17) {
        music = 8;
      }
      delay(inputdelay);
    }
    while (digitalRead(RFlipperPin) == LOW) {           // Loop as long as the switch is pressed
      if (digitalRead(StartPin) == LOW) {               // Easter egg - Play TLK - När Kristoffer Spelar Flipperspel
        player.volume(volume);
        player.play(26);
        delay(inputdelay); 
      }
      if (digitalRead(LFlipperPin) == LOW) {            // Volume cycling 6 LOW - 12 MEDIUM - (18 default)
          volume = volume >= 18 ? 6 : volume + 6;       // If volume is 18 or more, volume = 6. Otherwise raise volume with 6
          player.volume(volume);                        // initiate new volume
          while (digitalRead(LFlipperPin) == LOW) {}    // Loop as long as the switch is pressed
          delay(inputdelay);
      }
    }
    //----------------------------------------------------------------------------------------
    // Samples 17-24
    if (LFlipper == LOW) {
      player.volume(volume);
      player.play(sample);
      sample++;
      if (sample == 25) {
        sample = 17;
      }
      delay(inputdelay);
    }
    while (digitalRead(LFlipperPin) == LOW) {}          // Loop as long as the switch is pressed
    //----------------------------------------------------------------------------------------
    // Sample 25
    if (Start == LOW) {
      player.volume(volume);
      player.play(start);
      delay(inputdelay);
    }
    while (digitalRead(StartPin) == LOW) {}             // Loop as long as the switch is pressed


}

 

Revise this Paste

Your Name: Code Language: