.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! 3.1.13 GAME– 10 Second ======================== Introduction ------------------- Next, follow me to make a game device to challenge your concentration. Tie the tilt switch to a stick to make a magic wand. Shake the wand, the 4-digit segment display will start counting, shake again will let it stop counting. If you succeed in keeping the displayed count at **10.00**, then you win. You can play the game with your friends to see who is the time wizard. Components ---------------- .. image:: img/list_GAME_10_Second.png :align: center Schematic Diagram ------------------------ ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 SPIMOSI Pin 19 12 10 GPIO18 Pin 12 1 18 GPIO23 Pin 16 4 23 GPIO24 Pin 18 5 24 GPIO26 Pin 37 25 26 ============ ======== ======== === .. image:: img/Schematic_three_one13.png :align: center Experimental Procedures --------------------------------- **Step 1**: Build the circuit. .. image:: img/image277.png :width: 800 **For C Language Users** ^^^^^^^^^^^^^^^^^^^^^^^^^ **Step 2**: Go to the folder of the code. .. raw:: html .. code-block:: cd ~/davinci-kit-for-raspberry-pi/c/3.1.13/ **Step 3**: Compile the code. .. raw:: html .. code-block:: gcc 3.1.13_GAME_10Second.c -lwiringPi **Step 4**: Run the executable file. .. raw:: html .. code-block:: sudo ./a.out Shake the wand, the 4-digit segment display will start counting, shake again will let it stop counting. If you succeed in keeping the displayed count at **10.00**, then you win. Shake it one more time to start the next round of the game. .. note:: If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi`. **Code Explanation** .. code-block:: c void stateChange(){     if (gameState == 0){         counter = 0;         delay(1000);         ualarm(10000,10000);      }else{         alarm(0);         delay(1000);     }     gameState = (gameState + 1)%2; } The game is divided into two modes: gameState=0 is the \"start\" mode, in which the time is timed and displayed on the segment display, and the tilting switch is shaken to enter the \"show\" mode. GameState =1 is the \"show\" mode, which stops the timing and displays the time on the segment display. Shaking the tilt switch again will reset the timer and restart the game. .. code-block:: c void loop(){     int currentState =0;     int lastState=0;     while(1){         display();         currentState=digitalRead(sensorPin);         if((currentState==0)&&(lastState==1)){             stateChange();         }         lastState=currentState;     } } Loop() is the main function. First, the time is displayed on the 4-bit segment display and the value of the tilt switch is read. If the state of the tilt switch has changed, stateChange() is called. **For Python Language Users** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **Step 2**: Go to the folder of the code. .. raw:: html .. code-block:: cd ~/davinci-kit-for-raspberry-pi/python/ **Step 3**: Run the executable file. .. raw:: html .. code-block:: sudo python3 3.1.13_GAME_10Second.py Shake the wand, the 4-digit segment display will start counting, shake again will let it stop counting. If you succeed in keeping the displayed count at **10.00**, then you win. Shake it one more time to start the next round of the game. **Code** .. note:: You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``davinci-kit-for-raspberry-pi/python``. .. raw:: html .. code-block:: python import RPi.GPIO as GPIO import time import threading sensorPin = 26 SDI = 24 RCLK = 23 SRCLK = 18 placePin = (10, 22, 27, 17) number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90) counter = 0 timer =0 gameState =0 def clearDisplay(): for i in range(8): GPIO.output(SDI, 1) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) def hc595_shift(data): for i in range(8): GPIO.output(SDI, 0x80 & (data << i)) GPIO.output(SRCLK, GPIO.HIGH) GPIO.output(SRCLK, GPIO.LOW) GPIO.output(RCLK, GPIO.HIGH) GPIO.output(RCLK, GPIO.LOW) def pickDigit(digit): for i in placePin: GPIO.output(i,GPIO.LOW) GPIO.output(placePin[digit], GPIO.HIGH) def display(): global counter clearDisplay() pickDigit(0) hc595_shift(number[counter % 10]) clearDisplay() pickDigit(1) hc595_shift(number[counter % 100//10]) clearDisplay() pickDigit(2) hc595_shift(number[counter % 1000//100]-0x80) clearDisplay() pickDigit(3) hc595_shift(number[counter % 10000//1000]) def stateChange(): global gameState global counter global timer1 if gameState == 0: counter = 0 time.sleep(1) timer() elif gameState ==1: timer1.cancel() time.sleep(1) gameState = (gameState+1)%2 def loop(): global counter currentState = 0 lastState = 0 while True: display() currentState=GPIO.input(sensorPin) if (currentState == 0) and (lastState == 1): stateChange() lastState=currentState def timer(): global counter global timer1 timer1 = threading.Timer(0.01, timer) timer1.start() counter += 1 def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(SDI, GPIO.OUT) GPIO.setup(RCLK, GPIO.OUT) GPIO.setup(SRCLK, GPIO.OUT) for i in placePin: GPIO.setup(i, GPIO.OUT) GPIO.setup(sensorPin, GPIO.IN) def destroy(): # When \"Ctrl+C\" is pressed, the function is executed. GPIO.cleanup() global timer1 timer1.cancel() if __name__ == '__main__': # Program starting from here setup() try: loop() except KeyboardInterrupt: destroy() **Code Explanation** .. code-block:: python def stateChange():     global gameState     global counter     global timer1     if gameState == 0:         counter = 0         time.sleep(1)         timer()      elif gameState ==1:         timer1.cancel()         time.sleep(1)     gameState = (gameState+1)%2 The game is divided into two modes: gameState=0 is the \"start\" mode, in which the time is timed and displayed on the segment display, and the tilting switch is shaken to enter the \"show\" mode. GameState =1 is the \"show\" mode, which stops the timing and displays the time on the segment display. Shaking the tilt switch again will reset the timer and restart the game. .. code-block:: python def loop():     global counter     currentState = 0     lastState = 0     while True:         display()         currentState=GPIO.input(sensorPin)         if (currentState == 0) and (lastState == 1):             stateChange()         lastState=currentState Loop() is the main function. First, the time is displayed on the 4-bit segment display and the value of the tilt switch is read. If the state of the tilt switch has changed, stateChange() is called. .. code-block:: python def timer(): global counter global timer1 timer1 = threading.Timer(0.01, timer) timer1.start() counter += 1 After the interval reaches 0.01s, the timer function is called; add 1 to counter, and the timer is used again to execute itself repeatedly every 0.01s. Phenomenon Picture ----------------------- .. image:: img/image278.jpeg :align: center