SunFounder ESP32 Starter Kit
on the back to increase the contrast.
How it works?
1. In the setup() function, the I2C LCD screen and IR receiver are initialized. Then call the initNewValue()
function to generate a new random lucky number, and a welcome message is displayed on the LCD screen.
void setup() {
// Initialize the LCD screen
lcd.init();
lcd.backlight();
// Start the serial communication
Serial.begin(9600);
// Enable the IR receiver
irrecv.enableIRIn();
// Initialize a new lucky point value
initNewValue();
}
2. In the loop function, the code waits for a signal from the IR receiver. When a signal is received, the
decodeKeyValue function is called to decode the signal and get the corresponding button value.
void loop() {
// If a signal is received from the IR receiver
if (irrecv.decode(&results)) {
bool result = 0;
String num = decodeKeyValue(results.value);
// If the POWER button is pressed
if (num == "POWER") {
initNewValue(); // Initialize a new lucky point value
}
// If the CYCLE button is pressed
else if (num == "CYCLE") {
result = detectPoint(); // Detect the input number
lcdShowInput(result); // Show the result on the LCD screen
}
// If a number button (0-9) is pressed,
//add the digit to the input number
//and detect the number if it is greater than or equal to 10
else if (num >= "0" && num <= "9") {
count = count * 10;
count += num.toInt();
if (count >= 10) {
result = detectPoint();
}
lcdShowInput(result);
}
irrecv.resume();
(continues on next page)
1.37. 6.7 Guess Number 131