กลับไปหน้ารวมไฟล์
choice-maker-16fddb-en.md

I made this Arduino-Breadboard based circuit, designed to answer any Yes/No questions randomly by the press of a button. It answers with yes, probably yes, maybe/I don't know, probably not, no. The according LED lights up. This project is a great way to explore how to blink LEDs randomly and get user input through a button.

Hardware Components and Operation Details

The core of this project comprises the following key electronic components:

  1. Arduino Board: Acts as the Microcontroller Unit (MCU) to receive Input signals from the push button and control the Output to the LED.
  2. LEDs (5 pieces): Used to display the answer status. Typically, different colors are used to indicate confidence levels, such as green for "Yes" and red for "No." Each LED must be connected in series with a Resistor of approximately 220Ω for Current Limiting to prevent excessive current flow through the LED, which could cause damage.
  3. Push Button: Functions as a Momentary Switch to send Digital High/Low signals to the Arduino. In practice, a Pull-down Resistor connection is often used to ensure that when the button is not pressed, the voltage at the Input pin is always Zero Volt (Low), preventing a Floating State.

Code Logic Analysis

Code Explanation:

We start by setting our pins and initializing our ButtonState and answer variables. We then need to set the pinMode of our LEDs to outputs and our button to input.

The code's operational process is designed to respond to the user in a sequential manner, as follows:

1. Initialization: We begin by declaring variables to define the Pins of the Arduino connected to various devices, as well as variables to store the button's state (buttonState) and the randomized result (answer). In the setup() function, we configure the pinMode for all LED pins as OUTPUT and the pin connected to the push button as INPUT.

2. Input Detection and Delay for User Experience: In the loop() function, the program continuously checks the button's state using digitalRead(). When buttonState is detected as HIGH (button pressed), the program clears any LEDs that might be on from the previous run. It then pauses the system for approximately 100 ms. This delay is important for two reasons: it helps with Debouncing the signal and creates a brief waiting period to improve the User Experience, making it feel like the device is "thinking" for an answer.

3. Randomization & Decision Making: We then constantly check if the buttonState is high (button is pressed). If the button gets pressed, we first clear our LEDs and wait 100 ms to avoid an immediate response. This improves user experience. Then we generate a random number from 0 - 4.

The decision-making process utilizes the random(0, 5) function, which returns an integer value from 0 to 4. This value is then fed into a switch-case control structure, which is a cleaner and more efficient method than using multiple if-else statements. We then use a switch to act accordingly:

  • Case 0 (No): Turns on the red LED.
  • Case 1 (Probably Not): Turns on the yellow or orange LED indicating a negative status.
  • Case 2 (Maybe): Turns on the middle LED to signify uncertainty.
  • Case 3 (Probably Yes): Turns on the LED indicating a positive status.
  • Case 4 (Yes): Turns on the green LED.

Engineering Conclusion

This project is not merely a fun gadget, but also a crucial foundation for understanding Stochastic Processes, or randomization in computer systems, which in Arduino uses the principle of Pseudo-random numbers. Furthermore, it helps beginners grasp Digital I/O state management and fundamental User Interface design through LED lighting, which can be applied to more complex projects, such as signal level meters or various status notification systems in industrial applications.

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x Arduino IDE"
author: "petbump10"
category: "Lights & LEDs"
components:
  - "1x Arduino Uno R3"
  - "1x Yellow LEDs"
  - "1x Green LEDs"
  - "2x 10k ohm resistors"
  - "1x 3mm white LED"
  - "1x Jumper wires (generic)"
  - "5x Resistor 220 ohm"
  - "1x Push Button"
  - "1x Red LEDs"
  - "1x Breadboard (generic)"
  - "1x Blue LED"
  - "1x Computer"
description: "An Arduino-Breadboard project designed to make random choices."
difficulty: "Easy"
documentationLinks: []
downloadableFiles:
  - "https://projects.arduinocontent.cc/f45fa46e-60ed-4444-882c-c17c8e5fbba4.ino"
encryptedPayload: "U2FsdGVkX1/5hM87ckF2OpozXHvraUef3PxNxLKNpUQ/6NrNWxnGW9vaJCdApcDXJGvDMiWkqQTg+3DogMoJKkUGBnEfIi3E6UF+45aANA8="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/choice-maker-16fddb_cover.jpg"
lang: "en"
likes: 2
passwordHash: "1abd5d70d69787c3e8d6a7b0a7d5891d7bbb7bd58f972530818520f62f59fd02"
price: 450
seoDescription: "Build an Arduino-Breadboard project to make random choices. Easy electronics and programming tutorial for beginners."
tags:
  - "Games"
  - "Lights"
title: "Choice Maker"
tools: []
videoLinks: []
views: 1400