Coding - Random Questions

Hello, I'm trying to create a project that consists of a quiz with 6 questions. However, he liked to store about 15 different questions, and whenever a person started the quiz, different questions would appear.

However, there is a small detail, which is, the 6 question, must always be the same, from 1 to 5 question it can be different, but the 6 question (the last one in the case) must always be the same question .

Can they help? I am still a beginner in Arduino.

Thanks

//-------Library-------//

#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD

#include <stdlib.h>
#include <time.h>

//-------Variables-------//

//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.


const int buttonY = 11;
const int buttonN = 12;

const int ledPin1 =  13;
const int ledPin2 =  2;
const int ledPin3 =  9;
const int ledPin4 =  8;

unsigned long Tempo = millis(); // Inicia variáveis de tempo

int lastLed, indexLed;

int buttonStateY = 0;
int buttonStateN = 0;
int perguntas = 0;

bool escuta_botao = 0;
bool escuta_botao2 = 0;
bool firstpress = 0;



//------- Code -------//

void setup() {

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  
  pinMode(buttonY, INPUT_PULLUP);
  pinMode(buttonN, INPUT_PULLUP);

  Serial.begin(9600);

  lcd.init();
  lcd.backlight();
}

void loop() {

  if (escuta_botao == 1) {
    buttonStateY = digitalRead(buttonY);         // check if the pushbutton is pressed.

    // ButtonY
    if (buttonStateY  == LOW && firstpress == 0) {
      if (perguntas == 6) {
        //digitalWrite(ledPin1, HIGH);
        //acenderled();
        lcd.clear();
        perguntas = -1;
        RandomLed();
      }
      firstpress = 1;
      perguntas++;                                    //  preguntasrand=rand(1,15);
    }
    if (buttonStateY == HIGH && firstpress == 1) {
      firstpress = 0;
      escuta_botao = 0;
    }
  }
  if (escuta_botao2 == 1) {
    buttonStateN = digitalRead(buttonN);         // check if the pushbutton is pressed.
    
    // ButtonN
    if (buttonStateN  == LOW && firstpress == 0) {
      if (perguntas == 6) {
        lcd.print("Não aceitou os termos");                    // Print a message to the LCD.
        delay(500);
        Serial.println(perguntas);
      }
      firstpress = 1;
      perguntas++; //  preguntasrand=rand(1,15);
    }
    if (buttonStateN == HIGH && firstpress == 1) {
      firstpress = 0;
      escuta_botao2 = 0;
    }
    
    Serial.println(perguntas);
  }

  if (perguntas == 0) {
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("Bem-Vindo!");                             // Print a message to the LCD.
    lcd.setCursor(0, 1);
    lcd.print("Clique para iniciar");                    // Print a message to the LCD.
    //delay(2000);
    //scrool();
    escuta_botao = 1;
    escuta_botao2 = 1;
  }

  if (perguntas == 1) {
    lcd.clear();
    lcd.print("Sou sociavel?");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    delay(2000);
    escuta_botao = 1;
    escuta_botao2 = 1;
  }

  if (perguntas == 2) {
    lcd.clear();
    lcd.print("Lido bem com o stress?");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    delay(500);
    //acenderled();
    escuta_botao = 1;
  }
  if (perguntas == 3) {
    lcd.clear();
    lcd.print("Sou descuidado?");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    delay(2000);
    escuta_botao = 1;
    escuta_botao2 = 1;
  }
  if (perguntas == 4) {
    lcd.clear();
    lcd.print("Tenho pouca criatividade?");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    delay(2000);
    escuta_botao = 1;
  }

  if (perguntas == 5) {
    lcd.clear();
    lcd.print("Acredito em Deus?");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    delay(2000);
    escuta_botao = 1;
    escuta_botao2 = 1;
  }
  if (perguntas == 6) {
    lcd.clear();
    lcd.print("Aceite os termos para acender a vela");
    lcd.setCursor(0, 1);
    lcd.print("Sim          Nao");   // Print a message to the LCD.
    //delay(2000);
    escuta_botao = 1;
    escuta_botao2 = 1;
  }
}

//---------------------------------------------------- Funções -----------------------------------------------------//


void scrool() {
  for (int positionCounter = 0; positionCounter < 40; positionCounter++) {   // scroll 13 positions to the left
    lcd.scrollDisplayLeft();                                                 // scroll one position left
    delay(250);                                                              // wait a bit
  }
}

void acenderled(){            
  if((millis() - Tempo) < 20000){  
        digitalWrite(ledPin1, LOW); 
  }
  if((millis() - Tempo) > 20000){
    Tempo = millis();
  }
}


void RandomLed(){
    srand (time(NULL));

    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);

    indexLed = rand() % 2;
    do{
        indexLed = rand() % 2;
        if(indexLed != lastLed){
            digitalWrite(indexLed, HIGH);
        }
    }while (lastLed == indexLed);
}

Create an array with the 14 questions, save the special one separately.

Shuffle the array (see Fisher–Yates shuffle) and use the first 5 entries as questions. Do that in a for loop to avoid duplicating code

Once those 5 questions have been asked, have a special code asking the very last question that you set aside.

Another way is to store all the questions in the array with the special one at the end and shuffle all entries but the last one (treat the array as if it was one element shorter in the Fisher–Yates shuffle code) and then ask questions starting at 6 entries from the end of the array. This way the last question is always the same.

(And the answer is 42)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.