How to use random function? Please Help

How can I use a random function to generate a random message from given specific messages and display in an lcd , after every 2 seconds?

I'm using Arduino UNO

Thanks.

have a look at the arduino random documentation

horace:
have a look at the arduino random documentation

Thanks for the quick reply,

I've already saw that, but it's with numbers, I don't know how to do it with text,
Here's the code I've tried out, this one is just to print in Serial Monitor, then I will do with LCD..

long randNumber;



void setup() {

 Serial.begin(9600);
   randomSeed("A","B");
}

void loop() {
   randNumber = random();
  Serial.println(randNumber);

  delay(50);
  

}

I get this error :

exit status 1
too many arguments to function 'void randomSeed(long unsigned int)'

Thanks..

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

randomSeed()takes one paramter - the seed to the pseudo random sequence
on a PC I tend to use time the arduino documentation recommends

randomSeed(analogRead(0));

Are you attempting to generate a sequence of random characters?
if so use the random() function to randomly access an array of characters 'a' to 'z'?
or generate a random number between 0 and 25 and add it to 'a'

Use the number returned by random() as the index to an array of phrases.

Something like

char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  Serial.begin(115200);
  randomSeed(analogRead(A0)); //attempt to make the sequence more random
}

void loop()
{
  byte index = random(0, 3);  //get a number between 0 and 3
  Serial.println(phrases[index]); //use it as the index to the array of phrases
  delay(1000);
}

UKHeliBob:
Use the number returned by random() as the index to an array of phrases.

Something like

char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  Serial.begin(115200);
  randomSeed(analogRead(A0)); //attempt to make the sequence more random
}

void loop()
{
  byte index = random(0, 3);  //get a number between 0 and 3
  Serial.println(phrases[index]); //use it as the index to the array of phrases
  delay(1000);
}

Hi, thanks for your reply, This is the exact code as I wanted and is working well in serial monitor, thank you so much,

but as I moved forward to transform into an led and button, its like as soon
as I will press the Push button, a random phrase should come to the LCD and stay until I leave the Button..

You can view the code below but the problem is , as soon as I press the button , a random phrase comes, but it keeps changing very fast, if I add a delay, little slow, I want it to just select one and then choose another one next time..

If you want I can shoot a video and upload to youtube,

the code is here....

const int buttonPin = 7;
int buttonState = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  ///Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  analogWrite(9, 50);
  randomSeed(analogRead(A0));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    lcd.clear();
    lcd.print("Press button to get a random phrase.");
    delay(500);
  }
  else if (buttonState == LOW) {

    byte index = random(0, 4);  //get a number between 0 and 4 :D
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print(phrases[index]); //use it as the index to the array of phrases


  }

}

Thanks!!

The continuous call to the lcd.clear() is causing the issue.

What you need to do is only check if the button is high or low when the button changes.

if (last_buttonState != buttonState)
{
 last_buttonState = buttonState;
 
 if (buttonState == HIGH)
 {
   lcd.clear();
   lcd.print("Press button to get a random phrase.");
 }
 else if (buttonState == LOW) {

   byte index = random(0, 4);  //get a number between 0 and 4 :D
   lcd.setCursor(0, 0);
   lcd.clear();
   lcd.print(phrases[index]); //use it as the index to the array of phrases
 }

}

get it?

adwsystems:
The continuous call to the lcd.clear() is causing the issue.

What you need to do is only check if the button is high or low when the button changes.

if (last_buttonState != buttonState)

{
last_buttonState = buttonState;

if (buttonState == HIGH)
{
  lcd.clear();
  lcd.print("Press button to get a random phrase.");
}
else if (buttonState == LOW) {

byte index = random(0, 4);  //get a number between 0 and 4
  lcd.setCursor(0, 0);
  lcd.clear();
  lcd.print(phrases[index]); //use it as the index to the array of phrases
}

}




get it?

Hi , thanks for your reply, I get the concept clearly, but I didn't get the code.

I replaced the void loop and I get this follwing error...

'last_buttonState' was not declared in this scope

What am I doing wrong ?

Thanks..

nityoday:
What am I doing wrong ?

You added a variable to the program without declaring it. I provided a code snippet to fix your problem. You need to provide the rest. If you want someone else to do all the work, that costs money.

adwsystems:
You added a variable to the program without declaring it. I provided a code snippet to fix your problem. You need to provide the rest. If you want someone else to do all the work, that costs money.

I am sorry, but I am new to coding and arduino and I am unable to understand.

Thank you.

@nityoday You sent me a PM saying, amongst other things

People are asking money

There is no need to pay anyone to write the program. You can do it yourself and people here will help, but only if you help yourself too.

Post the latest version of your program that you have written and the full text of any error messages. We can then give advice as to how you can fix it and you will learn something.

Alright,

UKHeliBob:
@nityoday You sent me a PM saying, amongst other things
There is no need to pay anyone to write the program. You can do it yourself and people here will help, but only if you help yourself too.

Post the latest version of your program that you have written and the full text of any error messages. We can then give advice as to how you can fix it and you will learn something.

Here is the code that I've been trying :

const int buttonPin = 7;
int buttonState = 0;       
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  analogWrite(9,50);
  randomSeed(analogRead(A0)); 
}

void loop()
{
   buttonState = digitalRead(buttonPin);
   if (last_buttonState != buttonState)
{
 last_buttonState = buttonState;
 
 if (buttonState == HIGH)
 {
   lcd.clear();
   lcd.print("Press button to get a random phrase.");
 }
 else if (buttonState == LOW) {

   byte index = random(0, 4);  //get a number between 0 and 4 
   lcd.setCursor(0, 0);
   lcd.clear();
   lcd.print(phrases[index]); //use it as the index to the array of phrases
 }
}
}

Here's the error :

Arduino: 1.8.3 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\SONY\Documents\Arduino\sketch_may17b\sketch_may17b.ino: In function 'void loop()':

sketch_may17b:20: error: 'last_buttonState' was not declared in this scope

    if (last_buttonState != buttonState)

        ^

exit status 1
'last_buttonState' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

That "Snippet" provided by adwsystems is replaced in my original code after void loop's second line...

I understood the concept that it should be the last button state , high / low.. however, I don't understand the code, basically, last_buttonState and that Exclamation mark! in his/her code..

The modified code of yours, that I modified and added lcd, push button, and IF/ELSE statements is here (I've already posted too )

const int buttonPin = 7;
int buttonState = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  ///Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  analogWrite(9, 50);
  randomSeed(analogRead(A0));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    lcd.clear();
    lcd.print("Press button to get a random phrase.");
    delay(500);
  }
  else if (buttonState == LOW) {

    byte index = random(0, 4);  //get a number between 0 and 4 :D
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print(phrases[index]); //use it as the index to the array of phrases


  }

}

Thank you.

This is one of those rare times when the error is quite literal, the variable last_buttonState is not delcared.

First, you need to declare last_buttonState just like you did buttonState.

Then you need to read on if...else statements and comparison operators, including not equal to

You have some comments, but should have more. I will add some for you.

if (last_buttonState != buttonState) // is this button state not equal to the last button state
{
 last_buttonState = buttonState; // record current button state as last
 
 if (buttonState == HIGH) // is current button state HIGH
 {
   lcd.clear();
   lcd.print("Press button to get a random phrase.");
 }
 else if (buttonState == LOW) // is current button state low
 {

   byte index = random(0, 4);  //get a number between 0 and 4 :D
   lcd.setCursor(0, 0);
   lcd.clear();
   lcd.print(phrases[index]); //use it as the index to the array of phrases
 }

}

As I mentioned, you need to stop clearing the lcd if you want to stop the flickering. The information on the display only need to be updated when the button get pressed. Before you determine the state of the button, you need to determine if the state has changed (this is what I added).

adwsystems:
This is one of those rare times when the error is quite literal, the variable last_buttonState is not delcared.

First, you need to declare last_buttonState just like you did buttonState.

Then you need to read on if...else statements and comparison operators, including not equal to

You have some comments, but should have more. I will add some for you.

if (last_buttonState != buttonState) // is this button state not equal to the last button state

{
last_buttonState = buttonState; // record current button state as last

if (buttonState == HIGH) // is current button state HIGH
{
  lcd.clear();
  lcd.print("Press button to get a random phrase.");
}
else if (buttonState == LOW) // is current button state low
{

byte index = random(0, 4);  //get a number between 0 and 4 :smiley:
  lcd.setCursor(0, 0);
  lcd.clear();
  lcd.print(phrases[index]); //use it as the index to the array of phrases
}

}




As I mentioned, you need to stop clearing the lcd if you want to stop the flickering. The information on the display only need to be updated when the button get pressed. Before you determine the state of the button, you need to determine if the state has changed (this is what I added).

Okay so what CURRENT code is,

const int buttonPin = 7;
int buttonState = 0;  
int last_buttonState = LOW;     
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char * phrases[] = {"Phrase 1", "Phrase 2", "Phrase 3", "Phrase 4"};

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  analogWrite(9,50);
  randomSeed(analogRead(A0)); 
}

void loop()
{
   buttonState = digitalRead(buttonPin);
  if (last_buttonState != buttonState) // is this button state not equal to the last button state
{
 last_buttonState = buttonState; // record current button state as last
 
 if (buttonState == HIGH) // is current button state HIGH
 {
   lcd.clear();
   lcd.print("Press button to get a random phrase.");
 }
 else if (buttonState == LOW) // is current button state low
 {

   byte index = random(0, 4);  //get a number between 0 and 4 :D
   lcd.setCursor(0, 0);
   lcd.clear();
   lcd.print(phrases[index]); //use it as the index to the array of phrases
 }

}}

The project is working fine expect what I can see is when Button is not pressed, I can see "Random Phrase (?) " and the "Press button" too both swapping very very fast...

When button is pressed, I succesfully see a normal Phrase selected randomly..

But

sometimes LCD goes crazy

with thousands of characters scrolling !

Sorry for deleting last answer as there were some illogical errors which were reapired now.

Thank you.

A switch does not go from 1 to 0 and 0 to 1, it bounces causing the Arduino to see 01010001010011111.

Between the ill placed }} at the end of the sketch, add a delay (50 to 200 should do fine). This will cause the Arduino to wait for the switch to settle on a value.

adwsystems:
A switch does not go from 1 to 0 and 0 to 1, it bounces causing the Arduino to see 01010001010011111.

Between the ill placed }} at the end of the sketch, add a delay (50 to 200 should do fine). This will cause the Arduino to wait for the switch to settle on a value.

I've added a delay, LCD is showing Phrase (?) for some 5 seconds, then it shows Press button and then Phrase (?) real fast, like [SOS] Mode.

Last part of the code

}
delay(200);
}

And what is Karma ? I mean what happens if I add Karma to you ?

And what shall I do now for that LCD ?

also i can send a video, but how to send ?

Does it only display Phrase (?) while the button is pressed?

   byte index = random(0, 4);  //get a number between 0 and 4 :D

Not actually a problem, but the comment (or the code) is wrong. index will get a value between 0 and 3. In fact it better NOT get a value of 4 because your phrases are numbered 0, 1, 2 and 3 and there is no number 4

adwsystems:
Does it only display Phrase (?) while the button is pressed?

Yes, only while button is pressed. There must be a problem of code because when button isnt pressed, We can see both prashe number and Press Button to... swapping very very fast.. We may have to add some delays or INTERRUPTS maybe ?

UKHeliBob:

   byte index = random(0, 4);  //get a number between 0 and 4 :D

Not actually a problem, but the comment (or the code) is wrong. index will get a value between 0 and 3. In fact it better NOT get a value of 4 because your phrases are numbered 0, 1, 2 and 3 and there is no number 4

Alright, thanks for improving, I thought they are numbered from 1 to 4.. These small errors can really help improving.

when button isnt pressed, We can see both prashe number and Press Button to... swapping very very fast.. We may have to add some delays or INTERRUPTS maybe ?

There should be no need to add delay()s and certainly no need to use an interrupt.

How exactly is the button wired ?

My advice would be to use INPUT_PULLUP in the pinMode() for the button to activate the built in pullup resistor. This will ensure that the input is held HIGH. Wire the pushbutton to take the input to GND when the button is pressed. That way the input will not be floating and possibly picking up stray inputs.