Turn Indicator Circuit + Code

http://www.vimeo.com/3248666


merry-go-round-157a


Circuit Diagram
turngame


This is a turn indicator gadget for a 4 player game:

To build a breadboard version of this gadget in class you will need:

  • Arduino + power + cable
  • Breadboard + jumper wire
  • 4 LEDS
  • 1 Buzzer
  • 1 three position switch
  • 4 pushbuttons
  • 6 pullup resistors

To build a mounted version of this you will need:

  • 22 guage hookup wire (stranded is easier)
  • soldering setup
  • shrink tubing setup
  • Something to mount to (wood / foamcore / plexi …etc)
  • A drill with the right bits

I wrote the code for this gadget once using switch statements and external function calls and once using Arrays and for loops - they both function exactly the same.

Take a look at both to see how the different approaches work and to brush up on your code syntax

Check this out first:
using switch…case and functions - ZIP file of Arduino code is here

Check this out next:
using arrays and for loops - ZIP file of Arduino code is here


CODE 1 - (SWITCH & FUNCTIONS):

/*
  1. '* INFO:
  2. *********************************************************************
  3. * WHAT: Game Turn Keeper for 4 players (Using Switch Statements and Extra Functions)
  4. * BY:  eddo3ddo for DESMA 157A UCLA W09
  5. * DATE: FEB 15, 2009
  6. *
  7. * THINGS TO LEARN HERE:
  8. *********************************************************************
  9. * SWITCH…CASE - using SWITCH STATEMENT INSTEAD OF A BUNCH OF IF..THEN..ELSE STATEMENTS
  10. * BREAK - DONT FORGET TO USE BREAK IN YOUR SWITCH STATEMENTS TO BREAK OUT OF THE SWITCH
  11. * FUNCTIONS - USING FUNCTIONS AND FUNCTION CALLS FOR REUSABLE CODE
  12. * RETURN - USING FUNCTIONS WITH AND WITHOUT RETURN VALUES
  13. *
  14. * COMPONENTS LIST:
  15. *********************************************************************
  16. * 1 X ARDUINO D
  17. * 1 X BREADBOARD
  18. * WIRE JUMPERS
  19. * 6 x PULLUP RESISTORS
  20. * 4 x PUSHBUTTONS
  21. * 4 x LEDS
  22. * 1 X THREE POSITION TOGGLE SWITCH
  23. *
  24. * COMPONENETS SETUP:
  25. *********************************************************************
  26. * LEDS on PINS 7 ,8 , 9 ,10
  27. * PUSHBUTTONS WITH PULL UP RESISTORS ON PINS 2,3,4,5
  28. * BUZZER ON PIN 11
  29. * 3 POSITION TOGGLE SWITCH ON PINS 0 ,1 WITH PULLUP RESISTORS
  30. * *******************************************************************
  31. *'
  32. */
  33.  
  34. byte CW = 0;         // the PIN of the CW toggle switch
  35. byte CCW = 1;       // the PIN of the CCW toggle switch
  36.  
  37. // YOU WOULD NORMALLY USE A LOOKUP ARRAY TO STORE THESE  -
  38. // BUT WE ARE TRYING TO KEEP THE CODE SIMPLE -
  39. //THOUGH WE HAD TO WRITE 2 EXTRA LOOKUP  FUNCTIONS SINCE WE ARENT USING ARRARYS….
  40. // THE ARRAY WOULD LOOK LIKE THIS:
  41. // int Player_BUTTON_Pins[4] = {7,8,9,10};
  42.  
  43. byte Player1_Button_PIN = 7;         // Player 1 Button PIN
  44. byte Player2_Button_PIN = 8;         // the number of the input pin
  45. byte Player3_Button_PIN = 9;         // the number of the input pin
  46. byte Player4_Button_PIN = 10;         // the number of the input pin
  47.  
  48. byte BuzzerPin = 11;       // the number of the output pin Buzzer
  49.  
  50. // YOU WOULD NORMALLY USE A LOOKUP ARRAY TO STORE THESE  -
  51. //BUT WE ARE TRYING TO KEEP THE CODE SIMPLE -
  52. //THOUGH WE HAD TO WRITE 2 EXTRA LOOKUP FUNCTIONS SINCE WE ARENT USING ARRARYS….
  53. // THE ARRAY WOULD LOOK LIKE THIS:
  54. // int Player_LED_Pins[4] = {2,3,4,5};
  55.  
  56. byte Player1_LED_PIN = 2;         // the number of the input pin
  57. byte Player2_LED_PIN = 3;         // the number of the input pin
  58. byte Player3_LED_PIN = 4;         // the number of the input pin
  59. byte Player4_LED_PIN = 5;         // the number of the input pin
  60.  
  61. byte CurrentPlayer = 1;
  62. byte turnorder;
  63.  
  64. void setup()
  65. {
  66. pinMode(Player1_Button_PIN, INPUT);
  67. pinMode(Player2_Button_PIN, INPUT);
  68. pinMode(Player3_Button_PIN, INPUT);
  69. pinMode(Player4_Button_PIN, INPUT);
  70.  
  71. // THIS IS THE THREE POSITION TOGGLE SWITCH
  72. pinMode(CW, INPUT);
  73. pinMode(CCW, INPUT);
  74.  
  75. pinMode(BuzzerPin, OUTPUT);
  76.  
  77. pinMode(Player1_LED_PIN, OUTPUT);
  78. pinMode(Player2_LED_PIN, OUTPUT);
  79. pinMode(Player3_LED_PIN, OUTPUT);
  80. pinMode(Player4_LED_PIN, OUTPUT);
  81.  
  82. // CALL THE LED ON FUNCTION TO TURN ON CURRENT PLAYERS LED  (this starts at PLAYER 1)
  83. Player_LED_ON (CurrentPlayer) ;
  84. }
  85.  
  86. void loop() {
  87. //READ MASTER TOGGLE SWITCH
  88.  if (digitalRead(CW) == LOW && digitalRead(CCW) == HIGH) {
  89.   turnorder = CCW;
  90.  }
  91.  else if (digitalRead(CW) == HIGH && digitalRead(CCW) == LOW){
  92.   turnorder = CW;
  93.  }
  94.  else if (digitalRead(CW) == HIGH && digitalRead(CCW) == HIGH){
  95.   turnorder = 99;
  96.  }
  97.  //READ THE CURRENT PLAYER'S BUTTON TO SEE IF IT IS PRESSED
  98.  if (Read_Player_Button(CurrentPlayer) == LOW) {
  99.   //BUZZ BUZZER FOR 20 ms
  100.   digitalWrite(BuzzerPin, HIGH);
  101.   delay(20);
  102.   digitalWrite(BuzzerPin, LOW);
  103.   delay(100);
  104.   //INCREMENT TO NEXT PLAYER BASED ON DIRECTION WE ARE GOING
  105.   if (turnorder == CW){
  106.     if (CurrentPlayer < 4) {
  107.       CurrentPlayer++;
  108.     }
  109.     else {
  110.       CurrentPlayer = 1;
  111.     }
  112.   }
  113.   if (turnorder == CCW){
  114.     if (CurrentPlayer > 1) {
  115.       CurrentPlayer;
  116.     }
  117.     else {
  118.       CurrentPlayer = 4;
  119.     }
  120.   }
  121.  
  122.   // UPDATE STAUTS LIGHT to INDICATE CURRENT PLAYER'S TURN
  123.   Player_LED_ON (CurrentPlayer);
  124.   }
  125.   else { // DO NOTHING - TOGGLE IS IN THE OFF POSITION
  126.     // USE CODE TO TURN OFF THE LIGHTS IF YOU WANT THE LIGHT OFF AS WELL WHEN TOGGLE IS OFF
  127.   }
  128. }
  129.  
  130. // THIS FUNCTION receives a player# (1-4) and turns thatPlayer's LED ON and all other LEDS OFF)
  131. void Player_LED_ON (int player) {
  132.   byte CurrentPlayerLEDPin;
  133.  
  134.   //FIRST TURN OFF ALL LEDS
  135.   digitalWrite(Player1_LED_PIN, LOW);
  136.   digitalWrite(Player2_LED_PIN, LOW);
  137.   digitalWrite(Player3_LED_PIN, LOW);
  138.   digitalWrite(Player4_LED_PIN, LOW);
  139.  
  140.   //Derive Player_LED_PIN from player
  141.   switch (player) {
  142.     case 1:
  143.       CurrentPlayerLEDPin = Player1_LED_PIN;
  144.       break;
  145.     case 2:
  146.       CurrentPlayerLEDPin = Player2_LED_PIN;
  147.       break;
  148.     case 3:
  149.       CurrentPlayerLEDPin = Player3_LED_PIN;
  150.       break;
  151.     case 4:
  152.       CurrentPlayerLEDPin = Player4_LED_PIN;
  153.       break;
  154.   }
  155.   //TURN ON player LED PIN
  156.   digitalWrite(CurrentPlayerLEDPin, HIGH);
  157. }
  158.  
  159. // THIS FUNCTION RECEIVES A PLAYER # (1-4) AND READS THEIR BUTTON -
  160. // IT RETURNS THE STATE OF THE BUTTON
  161. byte Read_Player_Button (byte player) {
  162.  byte CurrentPlayerBUTTONPin;
  163.  byte state;
  164.  
  165.  switch (player) {
  166.   case 1:
  167.    CurrentPlayerBUTTONPin = Player1_Button_PIN;
  168.    break;
  169.   case 2:
  170.    CurrentPlayerBUTTONPin = Player2_Button_PIN;
  171.    break;
  172.   case 3:
  173.    CurrentPlayerBUTTONPin = Player3_Button_PIN;
  174.    break;
  175.   case 4:
  176.    CurrentPlayerBUTTONPin = Player4_Button_PIN;
  177.    break;
  178.  }
  179.  
  180.  // READ THE APPROPRIATE BUTTON
  181.  state = digitalRead(CurrentPlayerBUTTONPin);
  182.  return state;
  183. }

CODE 2 - (ARRAYS & FOR LOOPS):

/*
  1. *' INFO:
  2. *********************************************************************
  3. * WHAT: Game Turn Keeper for 4 players (Using Arrays)
  4. * BY:  eddo3ddo for DESMA 157A UCLA W09
  5. * DATE: FEB 16, 2009
  6. *
  7. * THINGS TO LEARN HERE:
  8. *********************************************************************
  9. * VARIABLE SIZE - using byte instead of int to save memory
  10. * ARRAYS - using arrays to store pins and then looking them up instead of
  11. *   defining many variables
  12. * FOR LOOPS - using for loops to step through lookup arrays
  13. *
  14. * COMPONENTS LIST:
  15. *********************************************************************
  16. * 1x ARDUINO D
  17. * 1x BREADBOARD
  18. * WIRE
  19. * 6x PULLUP RESISTORS
  20. * 4x PUSHBUTTONS
  21. * 4x LEDS
  22. * 1x THREE POSITION TOGGLE SWITCH
  23. *
  24. * COMPONENTS SETUP:
  25. *********************************************************************
  26. * LEDS on PINS 7 ,8 , 9 ,10
  27. * PUSHBUTTONS WITH PULL UP RESISTORS ON PINS 2,3,4,5
  28. * BUZZER ON PIN 11
  29. * THREE POSITION TOGGLE SWITCH ON PINS 0 , 1 WITH PULLUP RESISTORS
  30. * *******************************************************************
  31. *'
  32. */
  33.  
  34. byte CW = 0;         // the PIN of the CW toggle switch
  35. byte CCW = 1;       // the PIN of the CCW toggle switch
  36.  
  37. // YOU WOULD NORMALLY USE A LOOKUP ARRAY TO STORE THESE  -
  38. // BUT WE ARE TRYING TO KEEP THE CODE SIMPLE -
  39. // THOUGH WE HAD TO WRITE 2 EXTRA LOOKUP  FUNCTIONS SINCE WE ARENT USING ARRAYS….
  40. // THE ARRAY WOULD LOOK LIKE THIS:
  41. int Player_BUTTON_Pins[4] = {7,8,9,10};
  42.  
  43. byte BuzzerPin = 11;       // the number of the output pin Buzzer
  44.  
  45. // YOU WOULD NORMALLY USE A LOOKUP ARRAY TO STORE THESE
  46. //  - BUT WE ARE TRYING TO KEEP THE CODE SIMPLE -
  47. //THOUGH WE HAD TO WRITE 2 EXTRA LOOKUP FUNCTIONS SINCE WE ARENT USING ARRAYS….
  48. // THE ARRAY WOULD LOOK LIKE THIS:
  49. int Player_LED_Pins[4] = {2,3,4,5};
  50.  
  51. byte CurrentPlayer = 0; // PLAYERS ARE ( 0 - 3 )
  52. byte turnorder;
  53.  
  54. void setup(){
  55.  // BUTTONS AND LEDS
  56.  for (int i = 0; i < 4; i++){
  57.   pinMode(Player_BUTTON_Pins[i], INPUT);
  58.   pinMode(Player_LED_Pins[i], OUTPUT);
  59.  }
  60.  // BUZZER
  61.  pinMode(BuzzerPin, OUTPUT);
  62.  
  63.  // TOGGLE SWITCH
  64.  pinMode(CW, INPUT);
  65.  pinMode(CCW, INPUT);
  66.  
  67.  // INIT THE LEDS OFF
  68.  for (int i = 0; i < 4; i++) {
  69.   digitalWrite(Player_LED_Pins[i], LOW);
  70.  }
  71.  // TURN ON CURRENT PLAYERS LED
  72.  digitalWrite(Player_LED_Pins[CurrentPlayer], HIGH);
  73.  }
  74.  
  75. void loop() {
  76.  //READ MASTER TOGGLE SWITCH
  77.  if (digitalRead(CW) == LOW && digitalRead(CCW) == HIGH) {
  78.   turnorder = CCW;
  79.  }
  80.  else if (digitalRead(CW) == HIGH && digitalRead(CCW) == LOW){
  81.   turnorder = CW;
  82.  }
  83.  else if (digitalRead(CW) == HIGH && digitalRead(CCW) == HIGH){
  84.   turnorder = 99;
  85.  }
  86.  
  87.  //READ THE CURRENT PLAYERS BUTTON TO SEE IF IT IS PRESSED
  88.  if (digitalRead(Player_BUTTON_Pins[CurrentPlayer]) == LOW) {
  89.  //BUZZ BUZZER FOR 20 ms
  90.  digitalWrite(BuzzerPin, HIGH);
  91.  delay(20);
  92.  digitalWrite(BuzzerPin, LOW);
  93.  delay(100);
  94.  
  95.  //INCREMENT TO NEXT PLAYER BASED ON DIRECTION WE ARE GOING
  96.  if (turnorder == CW){
  97.   if (CurrentPlayer < 3) {
  98.    CurrentPlayer++;
  99.   }
  100.  else {
  101.   CurrentPlayer = 0;
  102.  }
  103. }
  104.  
  105.  if (turnorder == CCW){
  106.   if (CurrentPlayer > 0) {
  107.    CurrentPlayer;
  108.   }
  109.  else {
  110.   CurrentPlayer = 3;
  111.  }
  112. }
  113.  
  114.  // UPDATE STAUTS LIGHT to INDICATE CURRENT PLAYER'S TURN
  115.  for (int i = 0; i < 4; i++) {
  116.   digitalWrite(Player_LED_Pins[i], LOW);
  117.  }
  118.  // TURN ON CURRENT PLAYERS LED
  119.  digitalWrite(Player_LED_Pins[CurrentPlayer], HIGH);
  120.  }
  121.  else { // DO NOTHING - TOGGLE IS IN THE OFF POSITION
  122.   // USE CODE TO TURN OFF THE LIGHTS IF YOU WANT THE LIGHT OFF AS WELL WHEN TOGGLE IS OFF
  123.  }
  124. }

E

One Comment

  1. Error: Unable to create directory /data/classes/web/Winter09/157A/wp-content/uploads/2019/07. Is its parent directory writable by the server? Gleb Denisov
    Posted February 16, 2009 at 11:08 pm | Permalink

    Does it stand sturdy when you press buttons?