Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active December 9, 2025 11:11
Show Gist options
  • Select an option

  • Save psychemist/a1894ce65d2839be4dd68bb732ab4f13 to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/a1894ce65d2839be4dd68bb732ab4f13 to your computer and use it in GitHub Desktop.
Arduino Starter 11
// set up LiquidCrystal library
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int response;
void setup() {
// initialize LCD display ( arduino LCD has 16 columnns and 2 rows )
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
// print first line of welcome message on LCD screen
lcd.print("Ask the ");
// move the cursor to ( columnn index, row index ) and print second line
lcd.setCursor(0, 1);
lcd.print("Crystal Ball!");
}
void loop() {
// read and store switch state value
switchState = digitalRead(switchPin);
// respond if tilt switch state has changed since previous loop
if ( switchState != prevSwitchState ) {
// choose response based on random value
if ( switchState == LOW ) {
response = random(8);
// clear screen and print prelude line
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("The ball says:");
lcd.setCursor(0, 1);
// print line based on response
switch(response) {
case 0:
lcd.print("Yes");
break;
case 1:
lcd.print("Honestly, tea");
break;
case 2:
lcd.print("Outlook good");
break;
case 3:
lcd.print("Most likely");
break;
case 4:
lcd.print("Ask me again");
break;
case 5:
lcd.print("Thats CRAZY talk");
break;
case 6:
lcd.print("God forbid");
break;
case 7:
lcd.print("NO!!!");
break;
}
}
}
// track current/next switch state
prevSwitchState = switchState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment