Codigo - Perguntas Random

Olá, eu estou a tentar criar um projeto que consiste num quiz com 6 perguntas. No entanto, gostava de armazenar cerca de 15 perguntas diferentes, e sempre que uma pessoa fosse começar o quiz, aparecia perguntas diferentes.

No entanto, existe um pequeno pormenor, que é, a 6 pergunta, tem que ser sempre a mesma, da 1 à 5 pergunta é que pode ser diferente, mas a 6 pergunta (a ultima no caso) tem que ser sempre a mesma questão.

Será que podem ajudar? Eu ainda sou um iniciante no arduino.

Obrigado

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

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

//-------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;

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


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

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() {

  acenderled();

  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;
      }
      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 sociável?");
    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);
  }
}

//-------------- 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) > 6000){  
        digitalWrite(ledPin1, LOW); 
  }
  if((millis() - Tempo) < 6000){
    Tempo = millis();
  }
}

A pergunta que tem que ser a mesma é a última ou a sexta? Porque você falou que pretende aumentar para quinze perguntas, neste caso, qual seria a que não pode mudar?

Em qualquer dos casos, não resolveria com um condicional? Colocar um IF para verificar se a quantidade de perguntas realizadas até o momento é uma menor que a desejada.

if(total_perguntas==5){
    perguntas=6; // Aqui você atribui o valor fixo, para não ser aleatório.
}

Isso funcionaria caso sempre fosse a ultima pergunta.

Se for sempre a seis, independente da quantidade de perguntas, você pode fazer um array com int com cada pergunta sua menos a seis e o rand pega uma posição (index) aleatória de dentro dele e depois você move os valores para sobrepo-lo.

int perguntasIndex[] = {1,2,3,4,5,7,8,9};
int maxi = sizeof(perguntasIndex);
int total_perguntas = 0;
int pergunta;

void setup() {
  // put your setup code here, to run once:
  
}

void loop() {

  if(total_perguntas == 5)
  {
    pergunta == 6;
  }else{
    int rndIndx = random(0, maxi); // gera valor aleatorio de 0 ao maximo
    pergunta == perguntasIndex[rndIndx]; // escolhe a pergunta
    for (int i = rndIndx; i < maxi; ++i) // a partir do escolhido, desloca os valores
      perguntasIndex[i] = perguntasIndex[i + 1]; // move o valor da direita para a esquerda
    maxi--;
  }
}

Acho que alguma coisa assim resolveria, mas não testei.

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