/* #include <LiquidCrystal_I2C.h>
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600); // For Serial Monitor
lcd.begin();
lcd.backlight();
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
lcd.setCursor(0,0);
lcd.print("BPM: "); // Print phrase "BPM: "
lcd.print(myBPM);
}
delay(50); // considered best practice in a simple sketch.
}```
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int BUTTON1_PIN = 2; // 按钮1的引脚
const int BUTTON2_PIN = 3; // 按钮2的引脚
const int BUTTON3_PIN = 4; // 按钮3的引脚
LiquidCrystal_I2C lcd(0x27, 16, 2); // 定义LCD对象,设置I2C地址为0x27,列数为16,行数为2
void setup() {
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.backlight();
}
void display_symbol(int num) {
if (num == 1) {
lcd.print("Paper");
} else if (num == 2) {
lcd.print("Scissors");
} else {
lcd.print("Stone");
}
}
void display_result(int user, int robot) {
lcd.setCursor(0, 1);
if (user == robot) {
lcd.println("Draw");
} else if ((user == 1 && robot == 3) || (user == 2 && robot == 1) || (user == 3 && robot == 2)) {
lcd.println("Win");
} else {
lcd.println("Loss");
}
}
void loop() {
int button1State = digitalRead(BUTTON1_PIN);
int button2State = digitalRead(BUTTON2_PIN);
int button3State = digitalRead(BUTTON3_PIN);
if (!(!button1State || !button2State || !button3State)) return;
int user_input = 1;
int robot_input = random(1, 4); // 随机生成1到3的数字
lcd.clear(); // 清除LCD屏幕
lcd.setCursor(0, 0);
if (button1State == LOW) { // 如果按钮1被按下
user_input = 1;
} else if (button2State == LOW) { // 如果按钮2被按下
user_input = 2;
} else if (button3State == LOW) { // 如果按钮3被按下
user_input = 3;
}
display_symbol(user_input);
lcd.print(" VS ");
delay(1000);
display_symbol(robot_input);
display_result(user_input, robot_input);
delay(1000);
while (!digitalRead(BUTTON1_PIN) || !digitalRead(BUTTON2_PIN) || !digitalRead(BUTTON3_PIN)) delay(10);
}