/*
Arduino | coding-help
Breakbeam sensors
priyanka.c — 3/26/24 at 4:59 PM
It's only returning questions from size and not any other category.
- fixed
Below fails sometimes, "String"s should go to PROGMEM
*/
const int BEAM_PINS[] = {9, 8, 7}; // sensor pins
const int BTN_TRUE_PIN = 12; // green button
const int BTN_FALSE_PIN = 11; // blue button
const int LED_PIN = 10; // red LED
String size[5] = { /* Example category for sensor 1 */
"Jupiter is the smallest planet in our solar system",
"Jupiter is made mostly of gasses like hydrogen and helium",
"Jupiter has a solid surface beneath its clouds",
"Jupiters nickname is “The Gas Giant”",
"Jupiter cannot be seen with just the human eye"
};
bool answers1[] = {false, true, false, true, false}; // Example answers for sensor 1
String moons[5] = { /* Example category for sensor 2 */
"Jupiter has no moons orbiting around it",
"Jupiter has rings similar to Saturn",
"Jupiter has 20 moons",
"Jupiter has more moons than any other planet in our solar system",
"Jupiters rings are easy to see"
};
bool answers2[] = {false, true, false, true, false}; // Example answers for sensor 2
String features[5] = { /* Example category for sensor 3 */
"Jupiter's Great Red Spot is a massive storm that has been raging for centuries ",
"The Great Red Spot is not as big as Earth",
"Jupiter's gravity pulls in asteroids and comets, protecting Earth from potential impacts",
"The composition of Jupiters core is still unknown",
"Jupiter's atmosphere is composed primarily of carbon dioxide"
};
bool answers3[] = {true, false, true, true, false};
int beamState[3];
int oldBeamState[3];
int oldFalseState = HIGH; // pullup
int oldTrueState = HIGH; // pullup
bool correctAnswer = false;
void checkBeams(int number) {
beamState[number] = digitalRead(BEAM_PINS[number]);
if (oldBeamState[number] != beamState[number]) {
oldBeamState[number] = beamState[number];
if (beamState[number] == HIGH) {
Serial.print(F("From category "));
Serial.print(number + 1);
Serial.println(F(", true or false?"));
digitalWrite(LED_PIN, HIGH);
switch (number) {
case 0:
displayQuestion(size, answers1);
break;
case 1:
displayQuestion(moons, answers2);
break;
case 2:
displayQuestion(features, answers3);
break;
default:
break;
}
} else {
digitalWrite(LED_PIN, LOW);
}
delay(20); // debounce
}
}
void checkFalse() {
int falseState = digitalRead(BTN_FALSE_PIN);
if (oldFalseState != falseState) {
oldFalseState = falseState;
if (falseState == LOW) {
checkAnswer(false);
}
delay(20); // debounce
}
}
void checkTrue() {
int trueState = digitalRead(BTN_TRUE_PIN);
if (oldTrueState != trueState) {
oldTrueState = trueState;
if (trueState == LOW) {
checkAnswer(true);
}
delay(20); // debounce
}
}
void displayQuestion(String category[5], bool answers[]) {
// Generate a random index within the size of the category array
int randomIndex = random(5);
// Display the question at the random index
Serial.println(category[randomIndex]);
// Store the correct answer
correctAnswer = answers[randomIndex];
}
void checkAnswer(bool userAnswer) {
static int points = 0;
// Check if the user's answer matches the correct answer
if (userAnswer == correctAnswer) {
points++; // Increment points if the answer is correct
Serial.println(F("Correct!"));
Serial.print(F("Your score is "));
Serial.println(points);
Serial.println();
if (points >= 3) {
Serial.println(F("You pass!\n"));
points = 0; // Reset points
}
} else {
Serial.println(F("Incorrect!\n"));
}
}
void setup() {
Serial.begin(9600);
pinMode(BTN_TRUE_PIN, INPUT_PULLUP);
pinMode(BTN_FALSE_PIN, INPUT_PULLUP);
for (int i = 0; i < 3; i++) {
pinMode(BEAM_PINS[i], INPUT);
}
pinMode(LED_PIN, OUTPUT);
randomSeed(analogRead(A0));
// splash screen
Serial.println(F("Test your knowledge of Jupiter!"));
Serial.println(F("Answer 3 true or false questions to pass."));
Serial.println(F("Choose a category with a sensor.\n"));
}
void loop() {
for (int i = 0; i < 3; i++) {
checkBeams(i);
}
// add if question asked
checkTrue();
checkFalse();
}
True
False