이 프로젝트에서는 아두이노로 1에서 6 사이의 숫자를 제공하는 전자 주사위를 만드는 방법을 살펴볼 것입니다. 예를 들어 보드 게임을 할 때 매우 유용할 수 있습니다. 이 프로젝트에는 숫자를 표시하는 LCD 화면과 새 숫자를 얻기 위한 푸시 버튼이 포함되어 있습니다.
이제 프로젝트에 필요한 장비를 살펴보겠습니다:
푸시 버튼을 사용할 때 0V와 5V 사이의 전압이 존재하여 푸시 버튼의 값을 읽을 때 오류가 발생할 수 있습니다.
푸시 버튼의 저항기는 푸시 버튼을 더 이상 사용하지 않을 때 전압을 0V 또는 5V로 되돌림으로써 푸시 버튼의 알 수 없는 전압 상태를 제거합니다. 이것은 풀다운 저항입니다. 자세한 내용은 이에 대한 강좌를 참조하세요.
전위차계는 화면 밝기를 조정하는 데 사용됩니다. 전위차계 없이도 5V를 갈색 선에 직접 연결하고 전위차계에서 화면으로 연결되는 GND를 아두이노 보드에 직접 연결할 수 있습니다.
/* Program for Electronic Dice on Arduino */
#include <LiquidCrystal.h> // LCD Screen Library
const int button_pin = 7; // The push button is on pin 7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the pins used by the LCD screen
void setup() {
lcd.begin(16, 2); // Initialize the LCD screen
int random_number = random(1, 7); // Generate a random number between 1 and 6 for the dice
lcd.print("Dice Number: "); // Display the text
lcd.setCursor(13, 0); // Position the cursor for the dice number
lcd.print(random_number); // Display the random number
}
void loop() {
lcd.setCursor(0, 1); // Reset cursor to the beginning of the second line
int button_state = digitalRead(button_pin); // Read the push button value
if (button_state == 0) { // If the push button is pressed, change the number
lcd.clear(); // Clear the LCD screen
int random_number = random(1, 7); // Calculate the new random number
lcd.print("Dice Number: "); // Display the text
lcd.setCursor(13, 0);
lcd.print(random_number);
}
}
아두이노 보드에 프로그램을 업로드하려면 아두이노 아이디어 소프트웨어가 필요합니다. 소프트웨어가 설치되면 Arduino 보드에 적합한 포트를 선택하고 프로그램을 보드에 업로드하기만 하면 됩니다.
랜덤 함수는 1에서 6까지의 난수를 생성하는 데 사용됩니다. 그러나 숫자 생성에 숫자 6을 포함하려면 마지막 숫자가 포함되지 않으므로([1-7[) 7까지 올라가야 합니다.