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!!