Tiebreaker - Jeopardy game
Moderators: adafruit_support_bill, adafruit

Tiebreaker - Jeopardy game

by isonno on Wed Apr 13, 2011 11:33 pm

My son has recently become obsessed with "Jeopardy", and as part of his set-up, wanted a set of switches to determine who has the answer first.

So, we broke out his ARDX kit (we'd had it since Christmas, but just got around to opening it now) and got to work. We had it up and running in half an hour - pretty neat. It's my first time using the Arduino, though I've been programming MCUs for years.

Here's the sketch:
Code: Select all | TOGGLE FULL SIZE
//
// This sketch implements a "Jeopardy" style contestant response tiebreaker.
//
// Each contestant has a toggle switch. When they know the answer,
// they flip it on.  This turns on the corresponding light.   
// The light for the first person to flip their switch on blinks.
//
// Turning all of the switches off resets it.
//
// J. Peterson, Apr '11
//


// The LEDs, cathodes connected to ground
int Lights[3] = {13, 12, 11};

// The toggle switches connect the input to ground when closed.
int Switches[3] = { 10, 9, 8 };

// Set to -1 when no winner set.
int winner;

void setup()
{
  int i;
  for (i = 0; i < 3; i++)
  {
    pinMode(Lights[i], OUTPUT);
   
    pinMode(Switches[i], INPUT);
    digitalWrite(Switches[i], HIGH);  // Enable pullup
  }
  winner = -1;
  LightsOff();
}

void LightsOff()
{
  int i;
  for (i = 0; i < 3; i++)
    digitalWrite( Lights[i], LOW);
}

int invert( int value )
{
  return value == HIGH ? LOW : HIGH;
}

void loop()
{
  int i;
  if (winner == -1)
  {
    // Scan the switches looking for a winner
    for (i = 0; (i < 3) && (winner == -1); i++)
      if (digitalRead( Switches[i] ) == LOW)
        winner = i;
  }
  else
  {

     for (i = 0; i < 3; i++)
     {
       if (i == winner)
       {
         // Blink the winner's light every 100ms
         if (((millis() /100) % 10) & 1)
           digitalWrite( Lights[winner], HIGH );
         else
           digitalWrite( Lights[winner], LOW );
       }
       else   // Turn loser's lights on if switches are on
         digitalWrite( Lights[i], invert( digitalRead( Switches[i] )));
     }
  }

  // Check to see if all the switches are off,
  // and reset the winner & lights if so.
  if ((digitalRead( Switches[0] ) == HIGH)
     && (digitalRead( Switches[1] ) == HIGH)
     && (digitalRead( Switches[2] ) == HIGH))
   {
     winner = -1;
     LightsOff();
   }
}
Last edited by isonno on Thu Apr 14, 2011 9:15 am, edited 1 time in total.
isonno
 
Posts: 21
Joined: Sat May 10, 2008 9:30 pm
Location: California

Re: Tiebreaker - Jeopardy game

by adafruit_support_bill on Thu Apr 14, 2011 6:33 am

Nice project! A good way to get your son started programming micros. :D
User avatar
adafruit_support_bill
 
Posts: 31055
Joined: Sat Feb 07, 2009 10:11 am

Re: Tiebreaker - Jeopardy game - Pushbutton version

by isonno on Mon Apr 02, 2012 4:55 am

We revisited the sketch again this year, modifying it to use pushbuttons instead of toggle switches. This requires adding a "reset" switch for the host to restart the game.

Here's the updated sketch:

Code: Select all | TOGGLE FULL SIZE
//
// This sketch implements a "Jeopardy" style contestant response tiebreaker.
//
// Each contestant has a pushbutton switch. When they know the answer,
// they push it.  This turns on the corresponding light.
// The light for the first person to push their button blinks.
//
// Pressing the reset switch restarts the game.
//
// J. Peterson, Apr '12
//

// The LEDs, cathodes connected to ground
int Lights[3] = {13, 12, 11};

// The pushbutton switches connect the input to ground when closed.
int Switches[3] = { 10, 9, 8 };

// Connect to ground to reset the game
int Reset = 7;

// Set to -1 when no winner set.
int winner;

boolean SwitchPushed[3] = { false, false, false };

void setup()
{
  int i;
  for (i = 0; i < 3; i++)
  {
    pinMode(Lights[i], OUTPUT);

    pinMode(Switches[i], INPUT);
    digitalWrite(Switches[i], HIGH);  // Enable pullup

    pinMode( Reset, INPUT );
    digitalWrite( Reset, HIGH );    // Enable pullup
  }

  LightsOff();
}

// Reset to the initial state.
void LightsOff()
{
  int i;
  winner = -1;
  for (i = 0; i < 3; i++)
  {
    digitalWrite( Lights[i], LOW);
    SwitchPushed[i] = false;
  }
}

void loop()
{
  int i;

  // If no winner selected, first button selects the winner.
  if (winner == -1)
  {
    for (i = 0; (i < 3) && (winner == -1); i++)
      if (digitalRead( Switches[i] ) == LOW)
        winner = i;
  }
  else
  {
     for (i = 0; i < 3; i++)
     {
       if (i == winner)  // Blink winner's light
       {
         if (((millis() /100) % 10) & 1)
           digitalWrite( Lights[winner], HIGH );
          else
            digitalWrite( Lights[winner], LOW );
       }
       else
       {
         // When switch is pushed, record it.
         if (digitalRead( Switches[i] ) == LOW)
           SwitchPushed[i] = true;

         // Display non-winner lights
         digitalWrite( Lights[i], SwitchPushed[i] ? HIGH : LOW );
       }
     }
  }

  // Reset button
  if (digitalRead( Reset ) == LOW)
    LightsOff();
}
isonno
 
Posts: 21
Joined: Sat May 10, 2008 9:30 pm
Location: California