Defining a case as a node and moving to it (with yes/no push button)

Howdy Y'all,
So I am trying to make a simple decision tree build (in which users move through a series of questions). The print code below is faux (but hopefully a little fun). I am having trouble moving from case to case. With a lot of help already I come back here again to humbly seek guidance. I am trying to get the questions to move based on if the user presses "no" or "yes" push button. The trick being I want the questions to go in a very specific way depending upon their location in the tree. One glorious soul here helped to create the following struc:

struct node {   // set up for an array
   char* questionStr;  //stores character value
   int yesIndex;       //integer for number storage (yes index)
   int noIndex;       //integer for number storage (no index)
};
struct node nodeList[8];  //array of 8 nodes

And here is the access for the node:

nodeList[3]. yesIndex = 2;   // Example of access node

question 1: Can I define a "case" as a node in the pressedYes/pressedNo paradigm? If so how?
Here is the full code:

#include <LiquidCrystal.h> // Import LCD

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Allocate ports for LCD

const int yesPin = 13; // Defining digital pin 13 as linking to 'yes' push button switch.
const int noPin = 6; // 6 as no pin.

int receivedAnswer = 0; // help manage display
int askedQuestion = 0;
int pressedNo = 0;
int pressedYes = 0;
int lastButtonState = 0;
int question;
int CurrentQuestion = 0;

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
//below used to create question nodes to move the question depending on the answer.

struct node {   // set up for an array
   char* questionStr;  //stores character value
   int yesIndex;       //integer for number storage (yes index)
   int noIndex;       //integer for number storage (no index)
};
struct node nodeList[8];  //array of 8 nodes

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // Initialise Serial at 9600.
  
  pinMode(yesPin, INPUT); // Digital Pin 13
  pinMode(noPin, INPUT); // Digital Pin 6. Both input mode.

  lcd.begin(16, 2); // Initalise LCD Display.
  lcd.print("greetingln1"); // Inital strings to display on LCD including delays in between, resetting cursor (to following line, etc) and clearing LCD.
  delay(2500);
  lcd.setCursor(0, 1);
  lcd.print("greetingln2?.");
  delay(2500);
  lcd.clear();
  lcd.print("Answer yes");
  delay(2500);
  lcd.setCursor(0, 1);
  lcd.print("Or answer no.");
  delay(2500);
  lcd.clear();
}

void loop() {
  int reading = digitalRead(yesPin);  // read the state of the switch into a local variable:

  // check to see if you just pressed the button since the last press to ignore any noise based on time constraints

  // If the switch changed, due to noise or pressing:
  if (reading != askedQuestion) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != pressedYes, pressedNo) {
      pressedYes = reading;

      // only toggle the LED if the new button state is HIGH
      if (yesPin == HIGH) {
        pressedYes = ! pressedYes;
      }
    }
  }
  int reading2 = digitalRead(noPin);  // read the state of the no switch into a local variable:

  // check to see if you just pressed the button since the last press to ignore any noise based on time constraints

  // If the switch changed, due to noise or pressing:
  if (reading != askedQuestion) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != pressedNo) {
      pressedNo = reading;

      // only toggle the noPin if the new button state is HIGH
      if (noPin == HIGH) {
        pressedNo = ! pressedNo;
      }
    }
  }
  //start of main code after debouncing-->


  if (digitalRead(yesPin) == HIGH && receivedAnswer == 0){ // Checking if we have NOT received an answer yet and the digitalPin marked yesPin (8) is reading HIGH.
    // Yes!
    Serial.println("Pressed Yes!"); // Output to be read.
    pressedYes = 1; // Re defining vars to assist with LCD management.
    receivedAnswer = 1;
    lcd.clear(); // Clear LCD.
  }  //code to print yes answer after pushing button

  if (digitalRead(noPin) == HIGH && receivedAnswer == 0) { // The same thing as before (with the yes).
    // No!
    pressedNo = 1;
    Serial.println("Pressed No!");
    receivedAnswer = 1;
    lcd.clear();  //code to print no answer after pushing button
  }

  while (receivedAnswer == 0 && askedQuestion == 0) { // While we have not received answer nor asked question.
     
    switch (question) { // Switch statement from 0 - 7
      case 0:
        lcd.print("Are you ");
        lcd.setCursor(0, 1);
        delay(2500);
        lcd.print("human?");
        delay(2500);
 //question 1 aka Start
        if (pressedYes == 1) { // If Yes switch was pressed.
            lcd.clear();
              lcd.print("Verifying"); // Print stuff to LCD.
                lcd.setCursor(0, 1);
                    delay(2500);
                      lcd.print("Verified"); //print to lcd
                           delay(2500);
                              lcd.clear();
                                pressedYes = 0; // We have printed what we need to the LCD. Don't need to do it again.
                                  receivedAnswer = 0;
                                      askedQuestion = 0;
                                       nodeList[3]. yesIndex = 1; // mover to yesIndex?
        }
        if (pressedNo == 1) { // If no switch was pressed.
            lcd.clear();
              lcd.print("Verifying"); // Print stuff to LCD.
                lcd.setCursor(0, 1);
                    delay(2500);
                      lcd.print("Verified"); //print to lcd
                           delay(2500);
                              lcd.clear();
                                pressedNo = 0; // We have printed what we need to the LCD. Don't need to do it again.
                                  receivedAnswer = 0;
                                      askedQuestion = 0;
                                       nodeList[3]. noIndex = 1; // mover to noIndex?
        }
                                        break;
//here is where I am stuck. How can I label the following cases in line with their respective node lists? I.E
// for example if Pressedno -- move to --> noIndex 1 (<--how can I set this case?)
      case 1:
      case = yesIndex = 1; // This was my guess. but it reads "expected primary-expression before '=' token
        lcd.print("Do you like ");
        lcd.setCursor(0, 1);
        delay(2500);
        lcd.print("humans");
        delay(2500);
        nodeList[3].yesIndex = 2;  //mover to
        nodeList[3].noIndex = 2;
        break;

        
      case 2:
        lcd.print("Do you like ");
        lcd.setCursor(0, 1);
        delay(2500);
        lcd.print("brains");
        node = nodeList [3]yesIndex = 2; 
        delay(2500);
        
        break;
      case 3:
        lcd.print("Are you a ");
        lcd.setCursor(0, 1);
        delay(2500);
        lcd.print("cat?");
        delay(2500);
        break;
     //deleted the remaining case here in order to be within the 9000 character limit
    }
    askedQuestion = 1; // We have asked the question, don't need to do it again until we have a response.
  }

  if (pressedYes == 1) { // If Yes switch was pressed.
    lcd.clear();
    lcd.print("You answered "); // Print stuff to LCD.
    lcd.setCursor(0, 1);
    delay(2500);
    lcd.print("Yes!");
    delay(2500);
    lcd.clear();
    pressedYes = 0; // We have printed what we need to the LCD. Don't need to do it again.
    receivedAnswer = 0;
    askedQuestion = 0;
    }

  if (pressedNo == 1) { //  Exactly the same as above but for No
    lcd.clear();
    lcd.print("You answered ");
    lcd.setCursor(0, 1);
    delay(2500);
    lcd.print("No!");
    delay(2500);
    lcd.clear();
    pressedNo = 0;
    receivedAnswer = 0;
    askedQuestion = 0;
  }
 }

This looks like a game similar to "Twenty Questions". I think you want to put your questions in a list with pointers to what the next question would be for YES and NO answers. You have the data structure but have not filled it in.

struct node     // set up for an array
{
  const char * firstPart;
  const char * secondPart;
  int yesIndex;       //integer for number storage (yes index)
  int noIndex;       //integer for number storage (no index)
} questions[] =
{
{"Are you ", "human?", 1, 1},  // Question 0
{"Do you like ", "humans", 2, 2}, // Question 1
{"Do you like ", "brains", 2, 1}, // Question 2
{"Are you a ", "cat?", 3, 2}, // Question 3
};

You use this code MANY times so you should put it in a function:

void show(const char * first, const char * second)
{
          lcd.clear();
          lcd.print(first); // Print stuff to LCD.
          lcd.setCursor(0, 1);
          delay(2500);
          lcd.print(second); //print to lcd
          delay(2500);
}

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