UKHeliBob:
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.
Sorry for the last ans deleted, ignore if you didn't read
I didn't change the wiring, I have a button Module
so
- goes to GND
S goes to pin 7
other goes to +5V
I added the INPUT_PULLUP and the project is working PERFECTLY..
I changed the code to make this project a Majic 8 ball
You've to ask a YES or NO question
You will get a random answer from 25 list of answers ![]()
Here's the code
const int buttonPin = 7;
int buttonState = 0;
int last_buttonState = LOW;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char * phrases[] = {"It is certain", "Probably Yes", "Decidedly so", "Most likely", "No Doubt", "Outlook Good", "Yes, Definitely", "YES", "You may rely!", "Signs say YES", "Stars Say yes", "Absolutley", "Try again Later", "Ask again later", "not telling now ", "CantPredict now", "Please ask again", "Stars say no", "My reply is no", "It seems, NO!", "Dosen't seem so", "Hmm, Doubtful", "ChancesArent good", };
void setup()
{
Serial.begin(9600);
// pinMode(buttonPin, INPUT);
pinMode(buttonPin,INPUT_PULLUP);
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.setCursor(0, 0);
lcd.print("Ask a yes or no");
lcd.setCursor(0, 2);
lcd.print("Question");
}
else if (buttonState == LOW) // is current button state low
{
byte index = random(0, 24); //get a number between 0 and 24
lcd.setCursor(0, 0);
lcd.clear();
lcd.print(phrases[index]); //use it as the index to the array of phrases
}
}
}
Thanks so Much UK Heli Bob and ADW systems...
i will quote you also wait ![]()
adwsystems:
Does it only display Phrase (?) while the button is pressed?
The project is working awesome!
Thanks again
Hope I get as knowledge as you have and help other people too, I really love helping too.
Thank you.
Arduino Community is really awesome !!