Charles Finley
Charles Finley

Reputation: 49

am i on the right track here with my decision tree? arduino

  #ifndef _QUEUELIST.H
  #define _QUEUELIST.H

  #include <Arduino.h>
    struct node {
      char question[16];
      node * yes;
      node * no;
    }node;

  struct node initlist(char quest[]){
    struct node head;
    strcpy(head.question,quest);
    head.yes=NULL;
    head.no=NULL; 
    return head;
  }

  void addyes (struct node n, char quest[]){
    struct node tnode;
    strcpy(tnode.question,quest);
    tnode.yes=NULL;
    tnode.no=NULL;
    n.yes=&tnode;
  }

  void addno (struct node n, char quest[]){
    struct node tnode;
    strcpy(tnode.question,quest);
    tnode.yes=NULL;
    tnode.no=NULL;
    n.no=&tnode;
  }

  #endif

  void setup() {
  struct node top;
  struct node *temp;  //using this to traverse the tree keeping top as my handle
  top = initlist ("does it turn on?");
  temp = &top;
  addyes(top, "blue screen?");
  addno(top, "power light on?");
  temp=top.yes;
  addyes(*temp, "test memory");
  }

the above code is what I'm trying to implement for my code. basically I'm trying to make a yes/no trouble shooter for laptops. its not going to do any really in depth trouble shooting but just something the average end user who knows how to live boot a CD can run. I'm just trying to set up a decision tree.

i just want to make sure I'm on the correct train of thought for this project. and to make sure I'm not messing things up. i don't have the board set up for input/outputs yet and I'm just looking into how much space I'll need for this on the chip.

just a thought, can i run this code in a c compiler to make sure the structure inside is actually programmed right? i know the arduino functions wont work but i don't see why the tree wouldn't work.

thanks for the help

Upvotes: 0

Views: 589

Answers (2)

Idipaolo
Idipaolo

Reputation: 788

In the functions

void addyes (struct node n, char quest[])
void addno (struct node n, char quest[])

You are passing the node parameter by value. This means the structure that you are modifying is not the same that is in the setup() function. To solve this problem, try to pass, instead of the entire structure, the pointer to that structure.

void addyes (struct node * n, char quest[]){
  struct node* tnode = new node;
  strcpy(tnode->question,quest);
  tnode->yes=NULL;
  tnode->no=NULL;
  n->yes=tnode;
}

void addno (struct node* n, char quest[]){
  struct node* tnode = new node;
  strcpy(tnode->question,quest);
  tnode->yes=NULL;
  tnode->no=NULL;
  n->no=tnode;
}

And the setup will change in this:

  void setup() {
    struct node top;
    struct node *temp;  //using this to traverse the tree keeping top as my handle
    top = initlist ("does it turn on?");
    temp = &top;
    addyes(&top, "blue screen?");
    addno(&top, "power light on?");
    temp=top.yes;
    addyes(temp, "test memory");
  }

Upvotes: 1

hithwen
hithwen

Reputation: 2193

you can take a look at this little state machine library I wrote may be helpful for decision trees too.

The pros I see in that library is that I think it could lead to a clearer code and does not use as much memory as it does not store the whole yes/no path walked by the user.

Here's a mini decision tree example using the library:

#include "Arduino.h"
#include "hithwen/statemachine/statemachine.h"

char input;

StateMachine machine(3, 2);
bool char_is_y() {
    return input == 'y';
}

bool char_is_n() {
    return input == 'n';
}

void status_0() {
    Serial.println(F("Do you like chocolate?"));
}

void status_1() {
    Serial.println(F("Eat chocolate!"));
}

void status_2() {
    Serial.println(F("Don't eat chocolate!"));
}

void setup() {
    Serial.begin(9600);
    Serial.println(F("Welcome to my decision tree!"));


    //machine.reset();
    machine.add_transition(0, 1, &char_is_y);
    machine.add_transition(0, 2, &char_is_n);

    machine.add_state_function(0, &status_0);
    machine.add_state_function(1, &status_1);
    machine.add_state_function(2, &status_2);
    machine.loop();
}

void loop() {
    //checking data has been sent
    if (Serial.available() > 0) {
        char msg = Serial.read(); //read a message, it's not ignoring \0 chars
        input = msg;
        int nustate = machine.loop();
        input = '';
    }
}

Upvotes: 0

Related Questions