07 Feb 2014 Magic 8-Ball
 |  Category: Electronics

Whipped up a small circuit for my niece’s school project – a Magic 8-Ball.

Instructions:

Think of a question (with a Yes or No answer)

Press Button

And see the Magic Answer.

Very Simple circuit:

  1. 8 LEDs with their anodes connected to pins 10, 11, 12, 13, A0, A1, A2, A3 – these are just for adding a fun element to the project
  2. 16×2 LCD Display connected to pins 8, 7, 5, 4, 3, 2 – this shows the “Magic Answer”
  3. +ve terminal of the LCD backlight connected to pin 9 – Uses PWM to fade in and fade out
  4. Push-button connected to pin 6

Code:

#include "LiquidCrystalFast.h"
#include "OneButton.h"

String magicBall[] = {
  "It is certain",
  "It is decidedly so",
  "Without a doubt",
  "Yes definitely",
  "You may rely on it",
  "As I see it,    yes",
  "Most likely",
  "Outlook good",
  "Yes",
  "Signs point to  yes",
  "Reply hazy try  again",
  "Ask again later",
  "Better not tell you now",
  "Cannot predict  now",
  "Concentrate and ask again",
  "Don\'t count on it",
  "My reply is no",
  "My sources say  no",
  "Outlook not so  good",
  "Very doubtful"
};

LiquidCrystalFast lcd(8, 7, 5, 4, 3, 2);
int led[]={10,11,12,13,A0,A1,A2,A3};
OneButton button1(6, true);

void setup() {
  // put your setup code here, to run once:
  for(int i=0; i<8; i++){
    pinMode(led[i],OUTPUT);
  }
  lcd.begin(16, 2);
  button1.attachClick(magic);
  randomSeed(analogRead(0));
}

void loop() {
  button1.tick();
}

void magic()
{
  for(int i=0; i<8; i++){
     digitalWrite(led[i],HIGH);
     delay(100);
     digitalWrite(led[i],LOW);
     delay(100);
  }
  for(int i=7; i>=0; i--){
    digitalWrite(led[i],HIGH);
    delay(100);
    digitalWrite(led[i],LOW);
    delay(100);
  }
  lcd.clear();
  for(int i=0; i <255;i++){
    analogWrite(9,i);
    delay(10);
  }
  lcd.print(magicBall[ random(20) ]);
  delay(5000);
  lcd.clear();
  for(int i=254; i==0;i--){
    analogWrite(9,i);
    delay(5);
  }
  digitalWrite(9,LOW);
  for(int i=0; i<8; i++){     
     digitalWrite(led[i],HIGH);
     delay(100);
     digitalWrite(led[i],LOW);
     delay(100);
  }
  for(int i=7; i>=0; i--){
    digitalWrite(led[i],HIGH);
    delay(100);
    digitalWrite(led[i],LOW);
    delay(100);
  }
}

As always, comments welcome.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply