DDU_Exammen/ModuleMath/MathSystem.h

57 lines
1.2 KiB
C

#include <M5StickCPlus.h>
// -1 if error
int a = 0;
int b = 0;
int targetResult = 0;
enum class Operator{
plus,
minus,
mult
};
void initRandomSeed(){
M5.Imu.Init();
float accX, accY, accZ;
delay(250); // Wait for imu init
M5.IMU.getAccelData(&accX, &accY, &accZ);
float accelSum = accX + accY + accZ;
unsigned long seed = accelSum * 1000000;
randomSeed(seed);
}
String generateQuestion(){
Operator oper = static_cast<Operator>(random(0, 3));
switch (oper)
{
case Operator::plus:
a = random(0, 15);
b = random(1, 15);
targetResult = a + b;
return String(a) + "+" + String(b) + " = ?";
case Operator::minus:
do
{
a = random(15, 30);
b = random(0, 15);
targetResult = a - b;
} while (targetResult <= 0);
return String(a) + "-" + String(b) + "= ?";
case Operator::mult:
a = random(0, 10);
b = random(1, 10);
targetResult = a * b;
return String(a) + "*" + String(b) + "= ?";
default:
Serial.println("Failed to create math question");
return "Error";
}
}