I'm trying to make a random answer generating machine but when I click the button nothing happens, do you know what is wrong?

#include <LiquidCrystal.h>

char* ANESWER {"yes", "no", "maybe", "why?", "____ you!"};

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int button = 8;

void setup() {
  lcd.begin(2, 16);
  randomSeed(ANESWER[0, 4]);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  int read = digitalRead(button);

  if (read == HIGH) {
    lcd.setCursor(0, 3);
    lcd.print(ANESWER[0, 4]);
    delay(5000);

    lcd.clear();
  }else{
  lcd.clear();
  }

}

Uses a push button connected to pin 8. Also I used a 1602 16x2 LCD.

Your code has many problems- it does not compile.

First, when you post code USE CODE TAGS. When you paste your code highlight it and click on </>.

Next, Before you copy the code, use CTRL T in the IDE to properly format it.

Do not use a word processor like MS Word. It completely screws up the punctuation of the code.

Right off the bat - you have not properly formatted the char array “ANESWER”.

You have not provided a schematic, but the line:
pinMode(button, INPUT);
Should probably be
pinMode(button, INPUT_PULLUP);

Are you trying to simulate a politician?

(sorry I couldn’t help myself)

:+1: :+1: :+1:

Hello
try this sketch. You have to merge the LCD , change the input pin and than random answer generating machine shall works.

#define ProjectName "I’m trying to make a  but when I click the button nothing happ"
struct ANSWERBUTTON {
  byte pin;
  bool state;
  String answer[5];
} answerButton {A0, 0, {"yes", "no", "maybe", "why", "___you!"}};
const int heartBeat {
  LED_BUILTIN
};
void setup() { // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);
  Serial.print("Project: "), Serial.println(ProjectName);
  pinMode (heartBeat, OUTPUT);
  pinMode (answerButton.pin, INPUT_PULLUP);
  Serial.println(F(".\nlet´s go! "));
  delay(1000);
}
void loop() { // put your main code here, to run repeatedly:
  digitalWrite(heartBeat, millis() / 500 % 2);
  static unsigned long debounceMillis;
  if (millis() - debounceMillis >= 50) {
    debounceMillis = millis();
    bool state = !digitalRead(answerButton.pin);
    if (answerButton.state != state) {
      answerButton.state = state;
      if (answerButton.state) Serial.println(answerButton.answer[random(0,5)]);
    }
  }
}

thanks!

is this better?

thanks!!!

Thank you

IN 1775 English inventor Alexander Cumming was granted the first patent for a fluid toilet

Not to be confused with his much later successor, Thomas Crapper.

? That's not a random answer

No, it's a very regular answer.

I guess in the end, this thread was not a random answer generator.

@kv2rules sometimes it is easier to have more variables.
For example, make first a variable to get a random number, then use it for the array.

int randomIndex = random( 0, NUM_ANSWERS);
lcd.print( answerTable[randomIndex]);

This is an example with a single button.

#include <LiquidCrystal.h>

LiquidCrystal lcd( 12, 11, 5, 4, 3, 2);

const int buttonPin = 8;
int lastButtonState;

#define NUM_ANSWERS 7
const char * answerTable[NUM_ANSWERS] = 
{
  "yes", 
  "no", 
  "maybe", 
  "why?", 
  "tomorrow", 
  "I don't know",
  "ask new question",
};

void setup() 
{
  lcd.begin( 2, 16);
  lcd.setCursor( 0, 0);
  lcd.print( "Ask a question");
  lcd.setCursor( 0, 1);
  lcd.print( "and press button");
  delay( 2000);
  lcd.clear();

  pinMode( buttonPin, INPUT_PULLUP);

  // Try to get a random start by using the noise of the analog input(s)
  long seed = 12345678L + analogRead(0) + analogRead(1) + analogRead(2);
  randomSeed( seed);

  lastButtonState = digitalRead( buttonPin);
}

void loop() 
{
  int read = digitalRead( buttonPin);

  if( read != lastButtonState)
  {
    lastButtonState = read;

    if( read == LOW)               // low when button is pressed
    {
      lcd.setCursor( 0,0);

      int randomIndex = random( 0, NUM_ANSWERS);
      lcd.print( answerTable[randomIndex]);

      delay( 4000);
      lcd.clear();
    }
  }
}

The sketch in Wokwi:

This project is of course, called the "Magic Eight Ball". :grin:

1 Like