creating a random loop in a lcd

i have created a little program where if a button is pressed a lcd will display text. it will then display different text when it is released. i used a simple if statement which works fine

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0;  // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to

// read the value of the button to see if it is high or low
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // output value to the LCD and Servo
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  // tell arduino how many rows and collums lcd has
   lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Press The Button!");
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue); 
if (outputValue == 0)
//  if statement saying if the button is not pressed (0) then print one value 
 { lcd.print ("Thankyou") ;
 }else
 // when button is pressed say this value
 {lcd.print ("HELP ME PLEASE!");
 }
 

  //set the cursor to the first point on the lcd to display values
   lcd.setCursor(0, 1);


                   
}

i wish to expand it now to include multiple variations. i have set up an array of strings and a random loop but it doesnt like it. can someone help me

long randNumber;
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//string array for when button is not pressed (Low)
char* stringLow[]={"Thankyou", "Thanks very much", "YEAH BABAY"
"About Time!" "Get In There"};
//string array for when button is not pressed (High)
char* stringHigh[]={"HELP ME!", "Cmon Please", "Its cold Help!",
"Cmon do something", "just push the button"};
const int analogInPin = A0;  // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to

// read the value of the button to see if it is high or low
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // output value to the LCD and Servo
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  // tell arduino how many rows and collums lcd has
   lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Press The Button!");
    randomSeed(analogRead(0));

}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);
 
 for (int i = 0; i < 5; i++){
   Serial.println(stringHigh[i]);
   delay(500);
   }
   for (int i = 0; i < 5; i++){
   Serial.println(stringLow[i]);
   delay(500);
   } 
  
  if (outputValue == 0)
   //  if statement saying if the button is not pressed (0) then print one value 
 { randNumber = random (stringHigh)
   lcd.print (randNumber);
 }else
 // when button is pressed say this value
 
 {randNumber = random (stringHigh)
   lcd.print (randNumber);
 }


  //set the cursor to the first point on the lcd to display values
   lcd.setCursor(0, 1);


                   
}

it is telling me invalid conversation from char*** to long int

Once again you seem to be tossing a lot of code on the wall and seeing what sticks. Let me suggest a whole 'nuther way of thinking. Start small, get that code working, and then add to it and expand it functionality. As an example what does this snippet of code supposed to do ? What was you intention in having it in your code ?

for (int i = 0; i < 5; i++){
   Serial.println(stringHigh[i]);
   delay(500);
   }
   for (int i = 0; i < 5; i++){
   Serial.println(stringLow[i]);
   delay(500);
   }

So start with a basic loop() that reads the button (? why are you using an analog input ?) and picks the 1’st entry in your character arrays and “prints” that on the LCD. That way you’ll have the whole use of arrays figured out. Then try to do the random function. Trying to do everything all at once is guaranteed to be a frustrating fail when you don’t understand the basics.

i was given the links to the string, array and random on the arduino site to look up so i have been trying to understand and manipualte the code there. the idea is to create a string array of text for the lcd. set up the if statement so that the lcd takes a random one of the texts and shows it and decides on another one when the button is pressed/released again.

as for the code shown i added it so that i could set up the array that would go into my if statement for the lcd

i have until friday to finish this all. everything works now it is just a case of getting the random done. im sure there must be a simple way of doing it without restarting all my code

tyson642:
i was given the links to the string, array and random on the arduino site to look up so i have been trying to understand and manipualte the code there. the idea is to create a string array of text for the lcd. set up the if statement so that the lcd takes a random one of the texts and shows it and decides on another one when the button is pressed/released again.

I think you've got the character arrays about right. The use of the random function is all screwed up, so I say get the button push to display just the 1'st message out of your arrays. It will be easy to add the random code later.

tyson642:
as for the code shown i added it so that i could set up the array that would go into my if statement for the lcd

I don’t understand what you’re saying above. The arrays were already “set up” by :

char* stringLow[]={"Thankyou", "Thanks very much", "YEAH BABAY"
"About Time!" "Get In There"};
//string array for when button is not pressed (High)
char* stringHigh[]={"HELP ME!", "Cmon Please", "Its cold Help!",
"Cmon do something", "just push the button"};

So, IIRC, a

lcd.print(stringHigh[0]);

should get the LCD to show a "HELP ME! . But you should start there and get the basic accessing and printing of characters out of the arrays to work first.

The for loops I showed should do nothing but print out all the characters over and over and over again. That might be useful just to show that it works but frankly my way is simpler and wont be tossed away when you add in the random functioning.

The big picture I’m trying to convey is code that does the following (to start);

if button pushed
  -lcd.print(stringLow[0]);
else
  -lcd.print(stringHigh[0]);

(I might have the “high” and "low’ reversed but the idea is the same. Start simple)

ive been speaking to a freind who has helped me so far but i think im missing the last part

long randNumber;
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to

// read the value of the button to see if it is high or low
int sensorValue = 0; // value read from the pot
int outputValue = 0; // output value to the LCD and Servo
void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // tell arduino how many rows and collums lcd has
  lcd.begin(16, 2);
  // Print a message to the LCD.

  lcd.print("Press The Button!");
  randomSeed(analogRead(0));

  char stringLow[ ] = {
    'Thankyou', 'Thanks very much', 'YEAH BABY'
      'About Time!' 'Get In There'  };
  char stringHigh[ ] = {
    'HELP ME!', 'Cmon Please', 'Its cold Help!',
    'Cmon do something', 'just push the button'  };



}

void loop() {

  randNumber = random(0, 4);

  // read the analog in value:
  sensorValue = analogRead(analogInPin); 
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  if (outputValue == 0)
    // if statement saying if the button is not pressed (0) then print one value 
  { 
    lcd.print (stringHigh[randNumber]);
  }
  else
    // when button is pressed say this value

  {
    lcd.print (stringLow[randNumber]);
  }

  //set the cursor to the first point on the lcd to display values
  lcd.setCursor(0, 1);


}

im getting the error though expected } before /x6521

error now gone it runs but nothing displays the section where it should display is blinking as if it is refreshing constatly

long randNumber;
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to

// read the value of the button to see if it is high or low
int sensorValue = 0; // value read from the pot
int outputValue = 0; // output value to the LCD and Servo
void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // tell arduino how many rows and collums lcd has
  lcd.begin(16, 2);
  // Print a message to the LCD.

  lcd.print("Press The Button!");
  randomSeed(analogRead(0));

 



}
void loop() {
  
   char stringLow[ ] = {
    'Thankyou', 'Thanks very much', 'YEAH BABY',
      'About Time!', 'Get In There'  };
  char stringHigh[ ] = {
    'HELP ME!', 'Cmon Please', 'Its cold Help!',
    'Cmon do something', 'just push the button'  };

  randNumber = random(0, 4);

  // read the analog in value:
  sensorValue = analogRead(analogInPin); 
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  if (outputValue == 0)
    // if statement saying if the button is not pressed (0) then print one value 
  { 
    lcd.print (stringHigh[randNumber]);
  }
  else
    // when button is pressed say this value

  {
    lcd.print (stringLow[randNumber]);
  }

  //set the cursor to the first point on the lcd to display values
  lcd.setCursor(0, 1);


}

You’re getting that error because you forgot some commas, inbetween the characters in the arrays. You also inexplicably changed the " to ’ and changed the char* stringXYZ to char stringXYZ. Those are both errors. Lastly you moved the declaration of the stringXYZ from the top of your code to being inside the setup() function. That’s also an error.

When you correct all those, it will compile.

tyson642:
error now gone it runs but nothing displays the section where it should display is blinking as if it is refreshing constatly

That's because it is refreshing constantly. How long does it take to execute all the code in the loop() ? Didn't you have this problem before ? Also haven't you forgotten the lcd.clear problem you fixed ?

it is going through the loop quite quickly i cant see any of the text appearing. i have re added the lcd.clear again

long randNumber;
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A0; // Analog input pin that the Button is attached to
const int analogOutPin = 9; // Analog output pin that the Servo is attached to

// read the value of the button to see if it is high or low
int sensorValue = 0; // value read from the pot
int outputValue = 0; // output value to the LCD and Servo
void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // tell arduino how many rows and collums lcd has
  lcd.begin(16, 2);
  // Print a message to the LCD.

  lcd.print("Press The Button!");
  randomSeed(analogRead(0));

 



}
void loop() {
  
   char stringLow[ ] = {
    'Thankyou', 'Thanks very much', 'YEAH BABY',
      'About Time!', 'Get In There'  };
  char stringHigh[ ] = {
    'HELP ME!', 'Cmon Please', 'Its cold Help!',
    'Cmon do something', 'just push the button'  };

  randNumber = random(0, 4);

  // read the analog in value:
  sensorValue = analogRead(analogInPin); 
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);
  //set the cursor to the first point on the lcd to display values
  lcd.setCursor(0, 1);
  lcd.clear();
  if (outputValue  < 20 )
    // if statement saying if the button is not pressed (0) then print one value 
  { 
    lcd.print (stringHigh[randNumber]);
  }
  else
    // when button is pressed say this value

  {
    lcd.print (stringLow[randNumber]);
  }

  
}

do i need to initialise the loop in the void setup window as i havent set anything to say about the loop or random

tyson642:
it is going through the loop quite quickly i cant see any of the text appearing.

Imagine this situation. You not pressing the button. How often do you want the random message to change ? Once a second ? Every 2 secs ? 5 secs ? Add a delay in the loop or if it's a long delay you'll need to find another way to stop the LCD from being updated every single time the loop() executes.

i want it to change then i press the button that is what i need for what im doing. it has to change when the button value changes

tyson642:
i want it to change then i press the button …

Huh ?

tyson642:
it has to change when the button value changes

OK, when the button goes from unpressed to pressed ... you want the message to change. It then stays the same for as long as the button is held ? And when the button is released, going from pressed to unpressed ... the LCD shows a new message but then stays the same until the button is pressed again ?

I once again would point you towards reading this tutorial and noting how the button/switch is read and how the LED on/off is done.

http://arduino.cc/en/Tutorial/InputPullupSerial

It’s surely more sensible than doing what you’re doing.

BTW why does this have to be done by Friday ? Is the servo stuff you had before supposed to be in here too ?

It has to be done w by Friday as it is coursework it currently does everything I want but I wish to try to expand it as I feel that the if statement is too easy/boring

The servo code is in there just a new way of doing it which seems to work well enough

Mee_n_Mac:
I once again would point you towards reading this tutorial and noting how the button/switch is read and how the LED on/off is done.

http://arduino.cc/en/Tutorial/InputPullupSerial

It’s surely more sensible than doing what you’re doing.

Since it’s now past your assignments due date I’ll post the code the way I would have written it (errr, did write it :roll: ).

You can note that it’s built off the code in the tutorial above. Since I don’t have your LCD I can’t guarantee it works but the parts I can test tell me it should. I have left out the servo code but if you take a little time to understand it as written, you should be able to plug the servo stuff back in fairly simply. I added some print statements and code to turn on/off the onboard LED so I could “see” what the code was doing.

//tell the compiler what other libraries are being used
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//declare all the constants used
const int inPin = 6;              //the input switch will be connected to this pin
const int outPin = 9;             //the servo will be connected to this pin
const int LEDPin = 13;            //the onboard LED is connected to this pin
const unsigned long LCDelay = 250; //delay in msec to allow the LCD to update
//declare the variables used
long randNumber = 0;
int switchState = 0;
int prior_switchState = 0;
char* stringLow[] = {"Thankyou", "Thanks very much", "YEAH BABY",
      "About Time!", "Get In There"};   //array to pick from when button is pushed
char* stringHigh[] = {"HELP ME!", "Cmon Please", "Its cold Help!",
    "Cmon do something", "just push the button"}; //array to pick from when button is not pushed

void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure input pin as an input and enable the internal pull-up resistor
  pinMode(inPin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
  pinMode(outPin, OUTPUT);
  //ensure the outputs are in their off states
  digitalWrite(LEDPin, LOW);
  digitalWrite(outPin, LOW);
  // tell arduino how many rows and collums lcd has
  lcd.begin(16, 2);
  // prompt the user to push the button, also show the LCD is working
  lcd.print("Press The Button!");
  delay(2000);      //delay long enough for user to read the above message on LCD
  // randomize the seed from reset to reset
  randomSeed(analogRead(0));
}

void loop(){
  //read the pushbutton value into a variable
  switchState = digitalRead(inPin);
  //print out the value of the pushbutton to help in debugging
  Serial.print("The switch state is ");
  Serial.println(switchState);
  Serial.print("The random number for this loop is ");
  Serial.println(randNumber);
  // now pick a random number between 0 and 4 to use to index
  // the character arrays
  randNumber = random(0, 4);
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on the LED when the
  // button's pressed, and off when it's not:
  if (switchState == HIGH) {
    digitalWrite(LEDPin, LOW);       //the button is not pressed, turn off LED
    if (prior_switchState == LOW) {  //test if button has just been released
      // clear the LCD and set the cursor to the starting position
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print(stringHigh[randNumber]);  //since the switch state has just changed, send new message to LCD
      prior_switchState = switchState;    //update prior state so next pass the test will fail unless button pushed
      Serial.println(stringHigh[randNumber]); //also print to monitor for debug
     //add write to servo when button is not pushed code below this line
    }
  }
  else {
    digitalWrite(LEDPin, HIGH);      // the button is pushed, turn on LED
    if (prior_switchState == HIGH) { // the switchstate is LOW so now test if the state has just changed
      // clear the LCD and set the cursor to the starting position
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print(stringLow[randNumber]);  //since the switch state has changed, send new message to LCD
      prior_switchState = switchState;   //update prior state so next pass the test will fail unless button released
      Serial.println(stringLow[randNumber]);  //also print to monitor for debug
     //add write to servo when button pushed code below this line
    }
  }
  delay(LCDelay);      //wait for the switch and LCD to settle before looping again
}

I would add that you should note the wiring for the above is basically what’s shown in the tutorial but, because you’re using pin 2 (used in tutorial) for the LCD, the button/switch is assumed to be wired to pin 6 in my code.