Competition Button

Hello everyone. Please help...
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2; // connect to pin 2 on the DFPlayer via a 1K resistor
static const uint8_t PIN_MP3_RX = 3; // connect to pin 3 on the DFPlayer

// Software serial library
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
// Player
DFRobotDFPlayerMini player;

// Pins for buttons
const unsigned char switches[4] = {A7, A6, A5, A4};

// Pins for LEDs
const unsigned char leds[4] = {4, 5, 6, 7};

// Reset button
const unsigned char switch_reset = A2;

// Prepare the Arduino
void setup() {
// Prepare the pins

pinMode(switch_reset, INPUT_PULLUP);
for (unsigned char sw=0; sw<4; sw++) {
pinMode(switches[sw], INPUT_PULLUP);
pinMode(leds[sw], OUTPUT);
digitalWrite(leds[sw], LOW);
}

// Init serial port for DFPlayer Mini
softwareSerial.begin(9600);
// Start communication with DFPlayer Mini
if (player.begin(softwareSerial)) player.volume(30);
player.play(11);
while (digitalRead(switch_reset) == HIGH) {};
// Stop any playing sfx
player.stop();
}

// Main loop
void loop() {
// Check all 4 switches
for (unsigned char sw=0; sw<4; sw++) {
// Is it pressed?
if (digitalRead(switches[sw]) == LOW) {
// Turn on its LED
digitalWrite(leds[sw], HIGH);
// Play its sfx
player.play(sw+1);
// Wait for the reset button
while (digitalRead(switch_reset) == HIGH) {};
// Stop any playing sfx
player.stop();
// Turn off the LED
digitalWrite(leds[sw], LOW);
// Stop!
break;
}
}
}

how to add right and wrong answer buttons before reset after arduino pins A4-A7 are pressed

consider how this code check multiple buttons

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}
1 Like

Thank You. I will try it

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