In order to better respond to the national double reduction policy, cultivate talents who are useful to society, and reduce the burden of putting myself to sleep, I created a "night calculation trainer".
The training method is to randomly generate operational expressions and then require user input through voice broadcast. Of course, in order to save space, three sets of ship-shaped switches are used to input via 8421 codes. Each "1" of the binary code in the 8421 code represents a fixed value. Add up the binary numbers represented by each "1" to get the decimal number it represents. Because each "1" in the code represents the numbers "8", "4", "2" and "1" from left to right, it is named 8421 code. For example: input 1001 means 8+1=9. In this way, 12 switches can represent 0-999 numbers. Of course, if you go to higher grades in the future, you can consider using 10 switches to represent numbers from 0-1023. For hardware, we use the ESP32 FireBeetle produced by DFRobot, the matching Firefly OLED12864 display and the Gravity Chinese and English speech synthesis module (based on the XFS5152 chip), and then there is a boat-shaped switch for input. Next, we start the hardware design. The overall circuit diagram is as follows:
The upper left corner is a part used to consume power and ensure that it can still work normally when the power bank is powered (most power banks will automatically cut off the power supply after a period of time when the current is less than 100ma. Here you can use the timer to Pull out a current for a while to avoid this). In the upper middle is a USB Type A male connector for external power input. That is to say, this design can use Micro USB on FireBeetle or USBTypeA for power supply. Special attention needs to be paid to avoid both working at the same time to avoid the problem of current backflow. The lower left corner is a keyboard matrix design, in which diodes are used to ensure that there will be no misjudgments, so that it can be judged when multiple keys are pressed at the same time. The specific implementation is carried out by dynamic scanning, for example: first pull KEY_ROW1 high while keeping KEY_ROW2 and KEY_ROW3 low. At this time, if U1-U4 are connected, the corresponding KEY_COLx will appear high level. Next, perform similar operations on KEY_ROW2... This will enable scanning and judgment of all key inputs.
The voice module supports serial port and I2C. Here we choose I2C, which has a resistor pulled up to 3.3V.
You can see that there are three buttons on the board. Through these buttons, you can select the menu, confirm and submit the input results, return to the main menu and replay the question. Not all pins of the core ESP32 can be used as GPIO. If there is any improper use, it will cause repeated restarts. This design of IO is very tight:
Because of the existence of the boat-shaped switch, the overall PCB is larger and the wiring can be completed easily:
3D rendering
Get it and assemble it:
Next, we start to design the software part. The state transition diagram of the code is as follows:
The key code part is as follows 1. Draw the main menu in state 1. The drawing function is as follows, displaying the three items Current, Current+1...Current+3. When there is a button, the Current will be increased or decreased and then redrawn, thus realizing the menu selection function:
{
];
OLED.clear();
, MainMenu[Current]);
, Buffer);
) % MENUITEMCOUNTER]);
, Buffer );
) % MENUITEMCOUNTER]);
, Buffer);
) % MENUITEMCOUNTER]);
, Buffer);
OLED.display();
}
复制代码
2. In state 4, the function of generating calculation formulas is implemented. Use random numbers to generate numbers that participate in operations, and use enumeration types to define four arithmetic operations. In this way, random generates 0-3, which corresponds to 4 operators.
MATHOPERATION
{
, OPSUB, OPMUL, OPDIV
};
复制代码
Taking the generation of two-digit subtraction as an example, first generate two numbers, and then test the operation result. Only when the result is not a negative number is it a qualified calculation. The minuend generated afterwards is in Num[0], the subtrahend is in Num[1], the operator is stored in Op[0], and the correct result is in Result.
个数字运算
{
);
;
}
避免结果是负数
] = OPSUB;
];
break;
复制代码
3. After the calculation is generated, it is the process of broadcasting to the user. Specifically, in state 5, you can see the values and operators being read out respectively.
]);
]);
]);
)
{
]);
]);
}
复制代码
Numbers require special processing, so write a function readValue() that can output the pronunciation of 0-999:
{
(NCDEBUG)
{
);
Serial.println(Value);
}
}};
)
{
]);
]);
)
{
]);
}
}
{
)
{
]);
]);
]);
}
}
}
)
{
]);
}
)
{
]);
}
}
复制代码
4. Next wait for input in loading 6, where GetMatrix() is the matrix scan function. As mentioned earlier, the basic operation is to set KEY_ROW1 to HIGH, KEY_ROW2 and KEY_ROW3 to low, and then read the levels of KEY_COL1-3. Special attention should be paid to the fact that there must be sufficient delay after this action is completed, otherwise it will be incorrect to raise KEY_ROW2 and then read the value of KEY_COL1-3. For example, the keys are as follows:
ROW1 | ROW2 | |
COL1 | Closed (U1) | Disconnect(U1) |
COL2 | Disconnect(U1) | Disconnect(U1) |
After starting the scan, first set COL1 =HIGH, COL2=LOW, then read ROW1=HIGH, ROW2=LOW; if Delay is not added, then COL1=LOW, COL2=HIGH will be set next, and then ROW1 will be read to get HIGH. As a result, this is because the ESP32 is very fast and the charge on ROW1 has not had time to be released.
{
;
digitalWrite(KEY_ROW1, HIGH);
digitalWrite(KEY_ROW2, LOW);
digitalWrite(KEY_ROW3, LOW);
);
) +
) +
) +
digitalRead(KEY_COL4) ;
5. The last step is to wait for the submit button to be pressed.
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet