laurenkoi
Published

Arduino Project 11: Crystal Ball

Working with an LCD display and random()

IntermediateWork in progress1 hour362
Arduino Project 11: Crystal Ball

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Through Hole Resistor, 10 kohm
Through Hole Resistor, 10 kohm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Tilt Sensor, SPST-NC
Tilt Sensor, SPST-NC
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1

Story

Read more

Schematics

Video

Fritzing

Schematics

Code

Crystal Ball Code

Arduino
Uses random() to choose from a list of responses. Works like a magic 8-ball.
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2); // generates an instance 
in the lcd

const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup() {
  lcd.begin(16,2);
  
  pinMode(switchPin, INPUT);
  lcd.print("Preguntame");
  lcd.setCursor(0,1); // changes the Cursor to continue 
writing in the second row
  lcd.print("Bola de Cristal");
}
void loop() {
  switchState=digitalRead(switchPin);

  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      reply = random(8);
      lcd.clear(); 
      lcd.setCursor(0,0);
      lcd.print("La bola dice:");
      lcd.setCursor(0,1);

      switch(reply){ 
        case 0:
        lcd.print("Si");
        break;
        case 1:
        lcd.print("Es probable");
        break;
        case 2:
        lcd.print("Ciertamente");
        break;
        case 3:
        lcd.print("Buenas perspectivas");
        break;
        case 4:
        lcd.print("No es seguro");
        break;
        case 5:
        lcd.print("Pregunta de nuevo");
        break;
        case 6:
        lcd.print("Ni idea");
        break;
        case 7:
        lcd.print("No");
        break;
      }
    }
  }
  

}

Credits

laurenkoi
13 projects • 1 follower

Comments