What does the future hold? Find out… (at least how random numbers work), with the Crystal Ball!

This is the version for the SainSmart Kit.

SAMSUNG

We are using:

  • Arduino Uno
  • LCD Module
  • Potentiometer
  • Tilt Switch
  • 220 Ohm Resistor (Red-Red-Black-Black)

Wire up the LCD Module as follows:

  • LCD pin (1)   / Vss      -> Gnd
  • LCD pin (2)   / Vdd    -> 5V
  • LCD pin (4)   / RS      -> 12
  • LCD pin (5)   /  R/W  -> Gnd
  • LCD pin (6)   / E         -> 11
  • LCD pin (11)  / D4      -> 5
  • LCD pin (12)  / D5      -> 4
  • LCD pin (13)  / D6      -> 3
  • LCD pin (14)  / D7      -> 2

In addition you need to wire 5V/Gnd to the A/K of the backlight LED (pins 15/16); and wire in a potentiometer to adjust the contrast (pin 3).

Without the contrast adjustment and the backlight the LCD will not Display anything visible.

The complete circuit is shown below.

CrystalBall_SainKit_bb

Please double check the =5V/Gnd Wiring. You can PERMANENTLY DAMAGE your board if this is wrong.

Once wired up, try the Hello World! Example to check everything is OK. You may need to adjust the potentiometer to see the Hello World! message.

(File -> Examples -> LiquidCrystal -> HelloWorld)

OK, so let’s proceed.

We’ve attached a tilt sensor to Arduino pin d10 so whenever you tap the sensor it activates the Crystal Ball.

Here’s the code:

/*
 Adapted from Arduino Starter Kit example
 Project 11  - Crystal Ball
 to work with SainSmart 1602 (without Keypad)

 This sketch was written to accompany Project 11 in the
 Arduino Starter Kit

 Parts required:
 Arduino UNO
 SainSmart LCD (HD44780 compatible)
 Tilt Sensor

 Created 13 September 2012
 by Scott Fitzgerald

http://arduino.cc/starterKit

 This example code is part of the public domain
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// set up a constant for the tilt switchPin
const int switchPin = 10;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

// a variable to choose which reply from the crystal ball
int reply;

void setup() {
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);
  // Set the pullup on the switch pin
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);

  // Print a message to the LCD.
  lcd.print("Ask the");
  // set the cursor to column 0, line 1
  // line 1 is the second row, since counting begins with 0
  lcd.setCursor(0, 1);
  // print to the second line
  lcd.print("Crystal Ball!");
}

void loop() {

  // loop until the switch has changed from LOW to HIGH
  while (!(switchState ==  HIGH && prevSwitchState == LOW)) {
    // the switch did not change from LOW to HIGH last time
    // Remember the previous switch state from the last iteration
    prevSwitchState = switchState;
    // Read the present state
    switchState = digitalRead(switchPin);
    // delay as a simple debounce
    delay(100);
  }
  // the while loop exited wthout updating
  // the previous switch state so do it now
  prevSwitchState = switchState;

  if (switchState == HIGH) {
    // randomly chose a reply
    reply = random(8);
    // clean up the screen before printing a new reply
    lcd.clear();
    // set the cursor to column 0, line 0
    lcd.setCursor(0, 0);
    // print some text
    lcd.print("the ball says:");
    // move the cursor to the second line
    lcd.setCursor(0, 1);
    // delay for some suspense
    delay(1000);

    // choose a saying to print baed on the value in reply
    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("Ask again");
      break;

    case 6:
      lcd.print("Doubtful");
      break;

    case 7:
      lcd.print("No");
      break;
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *