New to coding, will this work?

Hi everyone,

My first time posting so please bear with me. I was tasked with a project for my church, I just want to make sure I got this right before I start ordering parts and piecing everything together.

**Background: Every few months several churches (5 or more) gather together, one of the main events is the Battle of the Kids… basically. The children are grouped into several age groups and pit against one another in a bible trivia challenge. The only way we have as of now to determine who is chosen to answer a question is by who raises their hand first. This is not very precise. A lot of guessing when you have 15 six year olds all raising their hands.

**Project: Jeopardy style buzz in with hand held buttons+LED’s. Person reading the questions(PRTQ) will enable the kids buttons when he presses his button.

1: PRTQ reads a question…
2: PRTQ presses his master button – LED illuminates on “junction box” all the buttons are connected to (TRRS audio cable).
3: Kids can press their buttons.
4: Kid that presses first will have their built in LED light up. All other kids are locked out.
5: Loop times out and waits for another press from PRTQ.

/* jeopardy style game-- master button (masClick) will enable 20+ other buttons (chiClick) to "buzz" in.  
  Once a player button is pressed, the others are locked out and the player's corresponding LED is lit indicating which player
buzzed in first.  
The loop resets after 5 seconds and waits for the master to be pressed again.*/



const int masClick = 53;// iteration should commence once masClick pin 53 goes HIGH
  const int masClickLED = 52; // will illuminate when master button is pressed
    const int chiClick[] = {2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23};                   
                        //  0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 
 const int chiClickLED[] = {24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45};
    const int pinCount = 22;

unsigned long debounceTime = 0;
unsigned long debounceDelay = 55;
int masClickState ;
  int preMasClickState     = LOW ;
    int masLedState        = LOW ;
      
      int chiState ;
        int preChiState    = LOW ;
          int chiLedState  = LOW ;


void setup() {
  

 pinMode (masClick, INPUT);
  pinMode (masClickLED, OUTPUT);
    pinMode (chiClick, INPUT);
      pinMode (chiClickLED, OUTPUT);


 digitalWrite (masClickLED, LOW);
 digitalWrite (chiClickLED, LOW);
}



void loop() {
 int reading = digitalRead(masClick);
  if(reading != preMasClickState){          // debouncing masClick button
  debounceTime = millis();
  }
    if ((millis() - debounceTime) > debounceDelay) {
      masClickState = reading;
  }
  if ( masClickState == masClick) {
     digitalWrite (masClickLED, HIGH);
   
   
  int i=0;
  
  
  for (i=0; i<pinCount; i++){                       
  
    int chiReading = digitalRead(chiClick[i]);         // start debouncing chiClick here?
      if(chiReading != preChiState){
        debounceTime = millis();
        }
        if ((millis() - debounceTime) > debounceDelay) {
             chiState = chiReading;
      }
  if (chiState == chiClick[i]){
    digitalWrite (chiClickLED[i], HIGH) ;
  
  delay (5000);
  
   break ;
 
  }  
   }
    }
     }

ChurchJeopardy.ino (2.08 KB)

Your premise is okay.

Haven't looked at your code, what doesn't work.

Show us a good schematic of your circuit.

.

Use a for loop to go through the arrays and set pinMode inputs and outputs.
Use a loop to read each input pin one at a time, takes only microseconds.
Don't worry about bouncing just read the switches every ~10 milliseconds.

.

/* jeopardy style game-- master button (masClick) will enable 20+ other buttons (chiClick) to "buzz" in.
  Once a player button is pressed, the others are locked out and the player's corresponding LED is lit indicating which player
  buzzed in first.
  The loop resets after 5 seconds and waits for the master to be pressed again.*/

const int masClick = 53;// iteration should commence once masClick pin 53 goes HIGH
const int masClickLED = 52; // will illuminate when master button is pressed

const int chiClick[]    = { 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
//                          0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
const int chiClickLED[] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
const int pinCount      = 22;

const unsigned int onTime   = 5000; //after you have a winner leave LED on for this time

const unsigned long debounceDelay = 25;  //scans switches every 25 milliseconds
unsigned long debounceTime;
unsigned long FiveSecMillis;

bool FiveSecFlag   = false;        //false = not in 5 second timing mode
bool polingFlag    = false;        //flase = not scanning the contestant switches

//*********************************************************

void setup()
{
  pinMode(masClick, INPUT_PULLUP);
  pinMode(masClickLED, OUTPUT);
  digitalWrite(masClickLED, LOW);

  for (int i = 0; i < pinCount; i++)
  {
    pinMode(chiClick[i], INPUT_PULLUP);
    pinMode(chiClickLED[i], OUTPUT);
    digitalWrite(chiClickLED[i], LOW);
  }

  //Lamp test, flash LEDs 10 times at reset, even # leaves the LEDs off
  for (int x = 0; x < 20; x++)
  {
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite(chiClickLED[i], !digitalRead(chiClickLED[i]));
    }
    digitalWrite(masClickLED, !digitalRead(masClickLED));
    delay(500);
  }
  
} //END of               s e t u p ( )

//*********************************************************

void loop()
{

  //NON blocking code goes here


  //**************************
  //Are we in a 5 second timeout?
  if (FiveSecFlag == true && millis() - FiveSecMillis >= onTime)
  {
    FiveSecFlag = false;

    //turn off contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LOW) ;
    }
  }

  //**************************
  //Time to scan the switches?
  if (millis() - debounceTime >= debounceDelay)
  {
    debounceTime = millis();
    readSwitches();
  }

} // END of              l o o p ( )


//*********************************************************

void readSwitches()
{
  //has the master switch been pressed?
  if (polingFlag == false && digitalRead(masClick) == LOW)
  {
    digitalWrite (masClickLED, HIGH);
    //turn off contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LOW) ;
    }

    //allow scanning
    polingFlag = true;
  }

  //should we scan for a winner?
  if (polingFlag == true)
  {
    //scan for the first LOW switch
    for (int i = 0; i < pinCount; i++)
    {
      if (digitalRead(chiClick[i]) == LOW)
      {
        //turn on the winner LED
        digitalWrite (chiClickLED[i], HIGH);

        //disable further scanning, allow master to ask next question
        polingFlag = false;
        digitalWrite (masClickLED, LOW);
        //Enable the 5 second countdown timer
        FiveSecFlag = true;
        FiveSecMillis = millis();
        //we found the winner so stop looking
        break;
      }
    }
  }

} // END of            r e a d S w i t c h e s ( )

//*********************************************************

You will have to add SWC filtering on the inputs.
Also, suggest you use 470R-1K external pull-ups.

.

gr8_1_day:
one of the main events is the Battle of the Kids...

How about finding a less aggressive sounding name. Religion should be preaching LOVE not BATTLE.

...R

Robin2:
How about finding a less aggressive sounding name. Religion should be preaching LOVE not BATTLE.

...R

Does seem odd.

.

LarryD, awesome job, I can tell you really enjoy this. I really appreciate it. The changes are perfect. You have my word that I will study until I fully understand the train of thought you implemented there. As for the "battle of the kids", don't know it's just what I typed. Obviously we don't actually battle lol.

Filter -- I'm thinking a snubber circuit would do the trick. During your projects, what is an acceptable R + C rating? I've no way to measure the oscillations.

The design for your contestant switch pads can be simple to very complex.
Use certified and approved power supplies with all your electronics projects.
Let’s say the room you are staging has next to no static discharge.
For up to 30-50 feet, you could get away with something as simple as shown below.
This would probably be good for 80% of the situations in a church.
Test out a few ‘cables switch units’ before you build 22.
Note: you should always make two controller units in case the first dies :wink:

I have used these in the past:
2017-04-12_8-10-55.jpg

2017-04-12_8-11-22.jpg

Note: if you had just installed new carpet, then static could be a problem (could spray floor with laundry anti-static solution).

Ultimate design might include: optocouplers or transistor receivers, transorbs, MOVs.

Edit
Personally, I would design around ‘normally closed’ PB switches and use shielded cable.
http://www.jameco.com/z/R18-29B-Switch-Push-Button-Single-Pole-On-Momentary-Off-Round-Plunger-3-Amp-125-VAC-Solder-Lug-Panel-Mount-Chrome-threads_26649.html

http://www.jameco.com/Jameco/Products/ProdDS/26649.pdf

.

Added cheat checking.

/* jeopardy style game-- master button (masClick) will enable 20+ other buttons (chiClick) to "buzz" in.
  Once a player button is pressed, the others are locked out and the player's corresponding LED is lit indicating which player
  buzzed in first.
  The loop resets after 5 seconds and waits for the master to be pressed again.*/

//LarryD
#define pushed LOW   //pushing gives a LOW
//#define pushed HIGH  //pushing gives a HIGH

#define LEDon   HIGH
#define LEDoff  LOW

const byte masClick      = 53; // iteration should commence once masClick pin 53 goes HIGH
const byte masClickLED   = 52; // will illuminate when master button is pressed

const byte chiClick[]    = { 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
//                           0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
const byte chiClickLED[] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
const byte pinCount = 22;

const unsigned int onTime         = 5000; //after you have a winner leave LED on for this time
const unsigned int debounceDelay  = 25;   //scans switches every 25 milliseconds

unsigned long debounceMillis; //used in timing switch checking
unsigned long FiveSecMillis;  //used in timing after first person has been detected
unsigned long masterMillis;   //used for flashing master LED after cheating is detected
unsigned long delayMillis;    //used after cheating has been acknowledgment by master push
unsigned long winnerMillis;   //used in timing to flash the winners LED

byte winnerIndex;          //the array index number of the winner

bool FiveSecFlag = false;  //false = not in 5 second timing mode
bool polingFlag  = false;  //flase = not scanning the contestant switches
bool cheatFlag   = true;   //true  = check to see if contestant button is pressed before it should be
bool flashFlag   = false;  //false = flash the master LED

//*********************************************************

void setup()
{
  pinMode(masClick, INPUT_PULLUP);
  pinMode(masClickLED, OUTPUT);
  digitalWrite(masClickLED, LEDoff);

  for (int i = 0; i < pinCount; i++)
  {
    pinMode(chiClick[i], INPUT_PULLUP);
    pinMode(chiClickLED[i], OUTPUT);
    digitalWrite(chiClickLED[i], LEDoff);
  }

  //Lamp test, flash LEDs 10 times at reset, an even number here leaves the LEDs off
  for (int x = 0; x < 20; x++)
  {
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite(chiClickLED[i], !digitalRead(chiClickLED[i]));
    }
    digitalWrite(masClickLED, !digitalRead(masClickLED));
    delay(500);
  }

} //END of             s e t u p ( )

//*********************************************************

void loop()
{

  //NON blocking code goes here

  //**************************
  //if there is any cheating flash master LED
  if (flashFlag == true)
  {
    if (millis() - masterMillis >= 250)
    {
      masterMillis = millis();
      //toggle master LED
      digitalWrite(masClickLED, !digitalRead(masClickLED));
    }
  }

  //**************************
  //are we in the 5 second timeout period?
  if (FiveSecFlag == true && millis() - winnerMillis > 100ul)
  {
     //flash winners LED
     winnerMillis = millis();
     //toggle LED
     digitalWrite(chiClickLED[winnerIndex], !digitalRead(chiClickLED[winnerIndex]));
  }

  else if (FiveSecFlag == true && millis() - FiveSecMillis >= onTime)
  {  
    FiveSecFlag = false;

    //turn off contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LEDoff) ;
    }

    //turn off master LED
    digitalWrite (masClickLED, LEDoff);

    //allow cheat checking
    cheatFlag = true;
  }



  //**************************
  //time to scan the switches?
  if (millis() - debounceMillis >= debounceDelay)
  {
    debounceMillis = millis();
    readSwitches();
  }

} // END of             l o o p ( )


//*********************************************************

void readSwitches()
{
  //**************************
  //should we check for cheating?
  if (cheatFlag == true)
  {
    //check to see if any number of contestants are holding their switch down
    for (int i = 0; i < pinCount; i++)
    {
      if (digitalRead(chiClick[i]) == pushed)
      {
        //turn on the cheaters LED
        digitalWrite (chiClickLED[i], LEDon);

        //notify master that a switch is being pressed too early
        flashFlag = true;
      }
    }
  }

  //**************************
  //if there is cheating, has the master acknowledged it
  if (flashFlag == true && digitalRead(masClick) == pushed)
  {
    //Stop master LED flashing
    flashFlag = false;
    //turn off Master LED
    digitalWrite (masClickLED, LEDoff);

    //turn off all contestant LEDs
    for (int i = 0; i < pinCount; i++)
    {
      digitalWrite (chiClickLED[i], LEDoff) ;
    }

    //must give some time after cheat acknowledgment
    delayMillis = millis();
  }

  //**************************
  //Start the game?
  //not in 5 second mode, no cheating, we are not currently poling, 2 seconds has gone by since
  //the last cheat acknowledgment and the master switch is now pressed
  if (FiveSecFlag == false && flashFlag == false && polingFlag == false  && millis() - delayMillis > 2000
      && digitalRead(masClick) == pushed)
  {
    //turn on master LED
    digitalWrite (masClickLED, LEDon);

    //allow scanning
    polingFlag = true;
    //disable cheat checking
    cheatFlag = false;
  }

  //**************************
  //should we scan for a winner?
  if (polingFlag == true)
  {
    //scan for the first LOW switch
    for (int i = 0; i < pinCount; i++)
    {
      if (digitalRead(chiClick[i]) == pushed)
      {
        //save the winners array index
        winnerIndex = i;
        winnerMillis = millis();
        
        //turn on the winner LED
        digitalWrite (chiClickLED[i], LEDon);

        //disable further scanning
        polingFlag = false;
        
        //Enable the 5 second countdown timer
        FiveSecFlag = true;
        FiveSecMillis = millis();
        
        //we found the winner, stop looking any further
        break;
      }
    }
  }

} // END of         r e a d S w i t c h e s ( )

//*********************************************************

You will probably want to add a piezo speaker to beep when the first person has been detected.

You may want to add a disable ‘cheat checking’ switch if there is a lot of static discharge happening.

A state machine sketch may have been a better choice for this, but that’s another topic for later.

Edit:
Added winner LED flashing

.