#include <LiquidCrystal.h>
LiquidCrystal lcd (12,11,5,4,3,2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup(){
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print(" Ask the");
lcd.setCursor(0,1);
lcd.print("Crystal ball");
}
void loop(){
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState ){
if (switchState == LOW){
reply = random(8);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("The ball says :");
lcd.setCursor(0,1);
switch(reply){
case 0:
lcd.print("Yes");
break;
case 1:
lcd.print("Most likely");
break;
case 2:
lcd.print("Certainly");
break;
case 3:
lcd.print("Outlook good");
break;
case 4:
lcd.print("Unsure");
break;
case 5:
lcd.print("Negative");
break;
case 6:
lcd.print("Doubtful");
break;
case 7:
lcd.print("No");
break;
}
}
}
prevSwitchState = switchState;
}
Nice sketch. Be sure to format your code and post it inside <CODE> tags (using the <CODE> button in the reply).
Here is an example of your sketch using arrays to lessen repetition (and some extra flair).
// magic8ball Uno/Nano, LCD1602, pushbutton
#include <LiquidCrystal.h> // LCD SPI library
byte rs = 12, en = 10, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // create object
uint8_t COLS = 16, ROWS = 2;
const byte switchPin = 6;
bool switchState = 0;
bool prevSwitchState = 0;
uint8_t reply;
uint8_t readtime = 5; // seconds for reading answer
const char *title[] = { "Ask the", "Crystal Ball" };
const char *answer[] = { "Yes", "Most likely", "Certainly", "Outlook good", "Unsure", "Negative", "Doubtful", "No" };
uint8_t smaldig[][8] = {
// each custom char has 8 elements
{ 0, 0, 0, 2, 6, 2, 2, 7 }, // small "1"
{ 0, 0, 0, 6, 1, 2, 4, 7 }, // small "2"
{ 0, 0, 0, 6, 1, 2, 1, 6 }, // small "3"
{ 0, 0, 0, 1, 3, 7, 1, 1 }, // small "4"
{ 0, 0, 0, 7, 4, 6, 1, 6 }, // small "5"
};
void setup() {
for (uint8_t i = 0; i < 5; i++) // count to three
lcd.createChar(i + 1, smaldig[i]); // create custom characters
lcd.begin(COLS, ROWS); // start the LCD
pinMode(switchPin, INPUT_PULLUP); // PRESSED = LOW, RELEASED = HIGH
randomSeed(analogRead(A0)); // add pseudo randomness
ask(); // show the title
}
void loop() {
switchState = digitalRead(switchPin); // read button state
if (switchState != prevSwitchState) { // if button state changes
if (switchState == LOW) { // if button was pressed
reply = random(8); // find the answer
center("The ball says:", 0);
scramble(); // flip random answers
center(answer[random(8)], 1); // display the answer
for (byte i = 0; i < readtime; i++) { // delay to read answer
lcd.setCursor(15, 1); // place countdown
lcd.write(readtime - i); // show countdown
delay(1000);
}
ask(); // restart it
}
}
prevSwitchState = switchState; // store button state
}
void ask() {
lcd.clear();
for (byte i = 0; i < 2; i++)
center(title[i], i);
}
void center(const char *text, byte row) {
lcd.setCursor((COLS - strlen(text)) / 2, row); // center text on LCD
lcd.print(text);
}
void scramble() { // show random answers
for (uint8_t i = 0; i < 20; i++) {
center(answer[random((sizeof(answer) / sizeof(answer[0])))], 1);
delay(900 / (sizeof(answer) / sizeof(answer[0]))); // half second of words
center(" ", 1);
}
}
Click here for diagram.json info for wokwi
{
"version": 1,
"author": "x",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
{ "type": "wokwi-lcd1602", "id": "lcd1", "top": -150.17, "left": -41.6, "attrs": {} },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 15.8,
"left": 172.8,
"attrs": { "color": "yellow", "xray": "1" }
},
{
"type": "wokwi-text",
"id": "text1",
"top": -9.6,
"left": 182.4,
"attrs": { "text": "PRESS" }
}
],
"connections": [
[ "nano:12", "lcd1:RS", "green", [ "v0" ] ],
[ "nano:10", "lcd1:E", "green", [ "v0" ] ],
[ "nano:2", "lcd1:D7", "green", [ "v0" ] ],
[ "nano:3", "lcd1:D6", "green", [ "v0" ] ],
[ "nano:4", "lcd1:D5", "green", [ "v0" ] ],
[ "nano:5", "lcd1:D4", "green", [ "v0" ] ],
[ "nano:GND.3", "btn1:1.l", "black", [ "v0" ] ],
[ "btn1:2.l", "nano:6", "yellow", [ "h0" ] ],
[ "nano:GND.3", "lcd1:K", "black", [ "v0", "h-28.8" ] ],
[ "nano:5V.2", "lcd1:A", "red", [ "v0", "h-28.8" ] ]
],
"dependencies": {}
}
