Animal Guessing game on Arduino

It's possible to make an animal guessing game in Arduino?

Probably. Could you maybe be a little more descriptive about what you want? Animal guessing game could mean a lot of things. Is someone guessing the animal? Or is the animal doing the guessing?

I wanna do a thing like the 20q. I've tried to make a sketch, but there is an error. Here is the sketch: P.S. The code is in Italian, sorry

#define YBUTTON 8
#define NBUTTON 9

int buttonY = 0;
int buttonN = 0;

boolean noButtonMode = false;

int currentQuestion = 0;
int gameStarted = 0;
int animalGuessed = 0;

int textPrinted = 0;

void setup() {
  //if (!noButtonMode){
    pinMode(YBUTTON, INPUT);
    pinMode(NBUTTON, INPUT);
    digitalWrite(YBUTTON, HIGH);
    digitalWrite(NBUTTON, HIGH);
  //}
  Serial.begin(9600);
  Serial.println("Esempio di Intelligenza Artificiale con Arduino");
  Serial.println("Gioco tipo 20q, indovino l'animale");
  question0();
}

void loop() {
   checkButtons();

}

static void checkButtons(){
  int buttonStateY = digitalRead(YBUTTON);
  int buttonStateN = digitalRead(NBUTTON);
  if (buttonStateY == LOW){
    buttonY == HIGH;
    delay(100);
  }
  if (buttonStateN == LOW){
    buttonN == HIGH;
    delay(100);
  }
}

static void question0(){
  currentQuestion = 0;
  //if (textPrinted = 0){
    Serial.println("E un uccello?");
   // textPrinted = 1;
  //}
  if (buttonY = HIGH){
    currentQuestion = 1;
    delay(500);
    question1();
    textPrinted = 0;
  }
  if (buttonN = HIGH){
    currentQuestion = 4;
    delay(500);
    question4();
    textPrinted = 0;
  }
}
static void question1(){
  currentQuestion = 1;
  //if (textPrinted = 0){
    Serial.println("Vola?");
  //  textPrinted = 1;
  //}
  if (buttonY = 1){
    currentQuestion = 2;
    delay(500);
    question2();
    textPrinted = 0;
  }
  if (buttonN = 1){
    currentQuestion = 3;
    delay(500);
    question3();
    textPrinted = 0;
  }
}
static void question2(){
  currentQuestion = 2;
  //if (textPrinted = 0){
    Serial.println("Parla?");
  //  textPrinted = 1;
  //}
  if (buttonY = 1){
    // Indovinato Pappagallo
    animalGuessed = 1;
  }
  if (buttonN = 1){
    // Indovinato Canarino
    animalGuessed = 2;
  }
}
static void question3(){
  currentQuestion = 3;
  //if (textPrinted = 0){
    Serial.println("E di grandi dimensioni?");
  if (buttonY = 1){
    // Indovinato Struzzo
    animalGuessed = 3;
  }
  if (buttonN = 1){
    // Indovinato Pinguino
    animalGuessed = 4;
  }
}
static void question4(){
  currentQuestion = 4;
    Serial.println("E di grandi dimensioni? (Più di un uomo)");
  if (buttonY = 1){
    currentQuestion = 5;
    delay(500);
    question5();
    textPrinted = 0;
  }
  if (buttonN = 1){
    currentQuestion = 6;
    delay(500);
    question6();
    textPrinted = 0;
  }
}
static void question5(){
  currentQuestion = 5;
    Serial.println("Vive soprattutto in acqua?");
  if (buttonY = 1){
    // Indovinato Orca
    animalGuessed = 5;
  }
  if (buttonN = 1){
    // Indovinato Elefante
    animalGuessed = 6;
  }
}
static void question6(){
  currentQuestion = 6;
    Serial.println("Graffia?");
  if (buttonY = 1){
    // Indovinato Gatto
    animalGuessed = 7;
  }
  if (buttonN = 1){
    // Indovinato Cane
    animalGuessed = 8;
  }
}
static void showAnimal(){
  if (animalGuessed = 1){
    //Pappagallo
    Serial.println("Pappagallo");
  }
  if (animalGuessed = 2){
    //Canarino
    Serial.println("Canarino");
  }
  if (animalGuessed = 3){
    //Struzzo
    Serial.println("Struzzo");
  }
  if (animalGuessed = 4){
    //Pinguino
    Serial.println("Pinguino");
  }
  if (animalGuessed = 5){
    //Orca
    Serial.println("Orca");
  }
  if (animalGuessed = 6){
    //Elefante
    Serial.println("Elefante");
  }
  if (animalGuessed = 7){
    //Gatto
    Serial.println("Gatto");
  }
  if (animalGuessed = 8){
    //Cane
    Serial.println("Cane");
  }
}

But the serial result it's that: P.S. Again sorry if the text is Italian

Esempio di Intelligenza Artificiale con Arduino
Gioco tipo 20q, indovino l'animale
E un uccello?
Vola?
Parla?
E di grandi dimensioni?
E di grandi dimensioni? (Più di un uomo)
Vive soprattutto in acqua?
Graffia?

Without pressing any button How can i solve this?

if (buttonStateY == LOW){
    buttonY == HIGH;

When assigning a value use 1 equal sign.

if (buttonY = HIGH){

When comparing a value, use two equal signs.

RicoZ:
I wanna do a thing like the 20q.
I’ve tried to make a sketch, but there is an error.

I think it should be possible with some limitations.
The game should possibly only allow the answers YES or NO only, but not additional answers like UNKOWN, MAYBE, SOMETIMES or something like that. Of course you will also not get a self learning system with artificial intelligence on an Arduino. All data will have to be coded as constants in the program sketch.

But I did a bit of googling and found this source code:
http://mosaic.cnfolio.com/B142LCW2008A197

Although this is NOT an Arduino source code, it is written in C and seems to be written for small computer systems. So when taking the general concept and programming logic from that source, adding buttons for YES and NO instead of console keyboard input, adapting the logic to Arduinos setup()/loop() concept and finally do some optimizations for saving RAM and using PROGMEM for constants, I think it should be possible to create something like a 20-questions-guess-the-animal-game" from that.

For me it looks doable.
:wink:

Main problem is memory for the questions. With a strict yes/no, the total number of things you can identify is 1+the number of questions you store.

Use an SD card for storing the names, questions and decisions.

Ok, i've tried to convert this code (http://mosaic.cnfolio.com/B142LCW2008A197) to Arduino code, but it is stuck on compiling loop Here is the code: http://pastebin.com/qbBtZkbk P.S. Sorry if the code is in Italian, again :D

Don't make us go to another site to see the code. Post it here if you want help here.

Delta_G: Don't make us go to another site to see the code. Post it here if you want help here.

I cannot, if i post the code here i exceed the 9000 characters limit

I cannot

Yes, you can. Click Reply. Do not use the Quick Reply field. When you click Reply, you'll see a link below the text entry area, Additional Options. One of them lets you attach a file to your post.

Ok, here is the code:

AnimalGuess.ino (12.6 KB)

  animal = strtok_r( strdup( ANIMALS_LIST ), ANIMAL_DELIMITER );

Count how many times you make a copy of ANIMALS_LIST, versus how many times you free the memory.

Unless you have the super-secret 10 terabyte Arduino, you are running out of memory.

RicoZ:
P.S. Sorry if the code is in Italian, again :smiley:

Your “Italian C++ code” reminds me of a nasty joke which was invented by German soldiers during World War II:

Question: What’s got six reverse gears and one forward gear?
Answer: An Italian tank. The forward gear is in case they get attacked from behind.

Why does your code remind me to this?

While the original code uses the forward string parsing function “strtok()”, which needs two function parameters, I can see in your “Italian translation” that you are using the reverse string parsing function “strtok_r()”, which needs three function parameters and scans strings in reverse order.

Do you think that the string function “strtok()” translated to Italian is “strtok_r()”, or what?
:wink:

While the original code uses the forward string parsing function “strtok()”, which needs two function parameters, I can see in your “Italian translation” that you are using the reverse string parsing function “strtok_r()”, which needs three function parameters and scans strings in reverse order.

Say what? strtok_r() does not parse the string in reverse order. It is the re-entrant version of strtok().

Thank you all, I've solved the problem

PaulS: Say what? strtok_r() does not parse the string in reverse order. It is the re-entrant version of strtok().

Thank's for correction! I've mistaken "_r" for "reversed", but indeed the manual clearly tells it is for "reentrant". Mea culpa! Neverteless, strtok() and strtok_r() are different functions and strtok_r() needs one extra parameter in the function call.

RicoZ: Thank you all, I've solved the problem

And. . . ?

AWOL: And. . . ?

The "Italian code" can be compiled after - all translated "strtok_r" have been replaced with the original "strtok" functions calls - two or three syntax errors (missing brackets and semicolons) are fixed.

Arduino IDE 1.0.5 compiles with no special message.

But Arduino IDE 1.6.5 shows:

Sketch uses 6,784 bytes (21%) of program storage space. Maximum is 32,256 bytes. Global variables use 3,330 bytes (162%) of dynamic memory, leaving -1,282 bytes for local variables. Maximum is 2,048 bytes.

The sketch is using much too much RAM, so the amount of RAM required must be reduced dramatically to make the programm run on Arduino.

Perhaps by - using the F-macro with Serial.println() - using constants in PROGMEM where possible

Perhaps using an SD card.