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.