Landscape Artwork/Game in need of some code help!

Hey everyone,

Im working on an art project that involves landscape and the creation of conversation around landscape. You can see more of my work or more about the project here:

www.earlprocko.wix.com/mtns

Im using an arduino uno and some LCD screens to generate questions to the players while they play so they can create conversation. So far i have connected 2 16x4 lcd screens up and have gotten them to project different questions. But i have 2 problems i am trying to work out.

problem 1: I want to create a push button that when pressed will refresh a random question from a bank of questions i can create? I know this should be doable, just not sure where to start?

problem 2: Is there a way for me to input the questions so that i can just type it in and not have to separate it out? As of now it seems i have to change the beginning of each line?

I found this and think its part of my problem?

Anyway thanks for all or any help you professionals of wizardry can provide me, deeply in debt.

-earl-

oh man, looks like some of those images are not working!?

lets try again?

First order of business is to update the Arduino IDE. The most current version is 1.6.6 and you appear to be using ver. 0023.

problem 1: I want to create a push button that when pressed will refresh a random question from a bank of questions i can create? I know this should be doable, just not sure where to start?

The best place to start is with the example programs included with the IDE. Open the IDE/programming window and take the path File > Examples >02.Digital. Study "Button", "Debounce", "Digital Input Pullup", and "state Change Detection". This will teach you how to detect when a button has been pressed.

Wire the switch you have between a digital pin set as INPUT_PULLUP and ground.

When you can detect a button press you will use the function random() to select your question

problem 2: Is there a way for me to input the questions so that i can just type it in and not have to separate it out? As of now it seems i have to change the beginning of each line?

No. Cursor management is an important part us using these types of LCD displays. Get used to it.

hmm... was wrong i have 20, 4 lcd screens but nonetheless:

ok so i got the button up and running and figured out how to engage it with my screens. then i got the button to when pushed send info and change info the screens!

however, now im trying to randomize a bunch of questions to put on the screens every time you push the button...

i can change a sentence with an if/else. but as soon as i put random in i get a number that keeps changing on the screen! i was able to get it to stay put on a number with one button push but then the next button push seems to create a continuos number change? then back again to a stable number then back to a constantly changing number?

im hoping in the end i can assign the numbers to the questions and when it randomly picks a number it displays the question that goes with that number? ive looked at arrays and strings but am unsure if this is the right road to go down? since its not just an integer or character i want displayed but a whole sentence that has to be formatted to display correctly on the lcd?

Stop posting screenshots of your code. First do a Tools > Auto Format every time before posting code. Next, copy and paste it as text using code tags. Usually it is best to post your entire sketch instead of code snippets.

sorry pert. appologies for not reading the "read before posting" thread. :confused:

okay here is what i am playing around with at the moment..

// this constant won't change:
const int  buttonPin = 6;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("what?");

  // set up the LCD's number of rows and columns:
  lcd.begin(20, 4);
  //  Print a message to the LCD.
  lcd.print("Player 1");

  lcd2.begin(20, 4);
  //  Print a message to the LCD.
  lcd2.print("PLayer 2");
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;




  if (buttonPushCounter % 2 == 0) {
    lcd.setCursor(0, 0);
    lcd.print("question");

    lcd2.setCursor(0, 0);
    lcd2.print("questionz");
  }


  else {

    lcd.setCursor(0, 0);
    lcd.print("hello    ");
    lcd2.setCursor(0, 0);
    lcd2.print("hello    ");
  }
}

so now when i push the button it changes back and forth between either saying hello or questions on the lcd's. Im happy with this but what i need is for a random question to be brought up.

one problem im having is in order to format the question to the lcd screen it looks like this:

lcd.setCursor(0,0);
lcd.print("When was the last");
lcd.setCursor(1,0);
lcd.print("time you described");
lcd.setCursor(2,0);
lcd.print("a landscape?");

can this and many more questions be embedded in an array or string and the selected randomly?

when i try and use random[] the screen continuously displays random numbers that never stop?

anyways... very appreciative for any help.

Here's some feedback on what you have done, and I've given you some code which will help refine your thinking.

First, I would change the way that your button is wired, so that pinMode is INPUT_PULLUP, and the switch is from the digital pin to ground. This will reverse the logic so that LOW is when the button is pressed. This is a safe, reliable way to use a push button and does not require an external pull down or pull up resistor.

I suggest that the way to cycle through your questions is with a switch/case tree. See https://www.arduino.cc/en/Reference/SwitchCase
Your lcd management will just be written within each case.

I have set the random selection to cycle through 10 questions without immediate repeats, but still a question can repeat. If you don't want any repeating questions in your cycle, you will need to replace the current approach with a "random shuffle".

Take a look at this. Refine your thinking on the presentation of the random questions and come back with another sketch for review.

// this constant won't change:
const int  buttonPin = 6;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

byte questionNumber; //variables for switch case
byte lastQuestionNumber;



// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);

void setup() {
  // initialize the button pin as a input:
  //pinMode(buttonPin, INPUT);
  pinMode (buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("what?");

  randomSeed(analogRead(A0));//randomize random number sequence

  // set up the LCD's number of rows and columns:
  lcd.begin(20, 4);
  //  Print a message to the LCD.
  lcd.print("Player 1");

  lcd2.begin(20, 4);
  //  Print a message to the LCD.
  lcd2.print("PLayer 2");
}
void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {   //reverse the logic
      // if the current state is LOW then the button
      // went from off to on:
      //buttonPushCounter++;
      //Serial.println("on");
      //Serial.print("number of button pushes:  ");
      //Serial.print(buttonPushCounter);
      Serial.println("Call Question Number");
      //Serial.println(random(1,11));
      questionNumber = random(1, 11);
       if (questionNumber == lastQuestionNumber)
        return;

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

  if (questionNumber != lastQuestionNumber)
  {
    lastQuestionNumber = questionNumber;//prevent constant looping before another question called

    switch (questionNumber) {

      case 1:
        Serial.println("This is Question 1");
        break;
      case 2:
        Serial.println("This is Question 2");
        break;
      case 3:
        Serial.println("This is Question 3");
        break;
      case 4:
        Serial.println("This is Question 4");
        break;
      case 5:
        Serial.println("This is Question 5");
        break;
      case 6:
        Serial.println("This is Question 6");
        break;
      case 7:
        Serial.println("This is Question 7");
        break;
      case 8:
        Serial.println("This is Question 8");
        break;
      case 9:
        Serial.println("This is Question 9");
        break;
      case 10:
        Serial.println("This is Question 10");
        break;
    }
  }
}

prockoe:
one problem im having is in order to format the question to the lcd screen it looks like this:

So you want to have two LCDs and you want to show questions across several lines with just one function call?

For doing so you can create a function that takes two parameters:

  • the LCD
  • the formatted text of the question
    and the function displays the text on the display you want.

Instead of a "line break" (which is not supported by the LiquidCrystal library) I'd suggest to use a special character as a "line break character" which normally does not occur in your question. Let's say you use the '#' chacter for 'line break' formatting in your questions.

Here is a small programming example (untested code as I do not have the hardware):

#include <LiquidCrystal.h>
LiquidCrystal lcd1(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);

void PrintQuestion(LiquidCrystal *lcd, char* question)
{
  int line=0;
  lcd->clear();
  for (unsigned int i=0;i<strlen(question);i++)
  {
    if (question[i]=='#')
    {
      line++;
      lcd->setCursor(0,line);
    }
    else lcd->write(question[i]);
  }
}

void setup() 
{
  lcd1.begin(20, 4);
  lcd2.begin(20, 4);
  PrintQuestion(&lcd1, (char*)"When was the last#time you described#a landscape?");
  PrintQuestion(&lcd2, (char*)"Was that#the correct answer?");
}

void loop() {
  // put your main code here, to run repeatedly:
}

If everything works as expected each function call of the PrintQuestion() function will print one text across multiple lines on the LCD.

What do you think about such a "PrintQuestion()" function?