code part 1
/*
HANGMAN PUZZLE BOX - By: Dan Ford.
A lot of code here has been borrowed from Dan Wagoner's "hangmanduino" sketch,
therefore I have included his origional notes from the aformentioned sketch
at the bottom of the notes section.
==================================================================================
=
Project aimed at creating a locking box, that operates on a game of hangman to
open the box. Components inside the box include a servo to operate a very
primitive "locking mechanism", and a button to open the lock from the inside due
to the fact that once the lock is opened it will close after 5 seconds. Outside
components include a standard 16x2 Character LCD (adafruit is the best deal right now),
a 10k potentiometer used to scroll through the letters A-Z, and a button to select
the highlighted letter.
Known issues:
Random function - selected words are not very random.
Servo jitter - servo moves slightly every 2-3 seconds, but not enough to open the box.
BUG FIX:
------------------------------------------------------------------------------------------
PART1
TRUERANDOM library added to code to ensure real random selection of a word from the list,
the native random function, (should be called psuedorandom), does not generate truly random
selections.
NOTE: ANALOG PIN 0 MAY NOT BE USED WHEN TRUERANDOM LIBRARY IS INCLUDED IN A SCRIPT
truerandom library generates static on analog pin 0 to draw random numbers from.
PART 2
servo jitter was generated by navite randomseed assigned to digital pin 4, the same pin
the servo was attatched to. Servo enable and disable commands are still included in code, but
will be commented out, servo is powered directly from +5V and GND pins.
pin 4 is no longer used for randomseed because TRUERANDOM library uses analog pin 0,
2 birds with one stone on that one!
--------------------------------------------------------------------------------------------
MAP:
--------------------------------------------------
ARDUINO PINS: |
analog 0 - DO NOT USE!!! |
digital 3 - button inside |
analog 3 - 10k pot outside |
digital 2 - LCD E6 |
(digital 3 - power to servo transistor)! |
digital 4 - servo |
digital 5 - speaker |
digital 6 - button outside |
digital 7 - LCD D4 11 |
digital 8 - LCD D5 12 |
digital 9 - LCD D6 13 |
digital 10 - LCD D7 14 |
digital 11 - LCD RW 5 |
digital 12 - LCD RS 4 |
|
OTHER: |
LCD 1 - GND |
LCD 2 - V+ |
LCD 3 - Contrast, 10k pot |
LCD 15 - V+ |
LCD 16 - GND |
|
|
! - code for this has been commented out, |
connect servo directly to 5V, |
or enable code for this fuction, |
see notes for details. |
|
---------------------------------------------------
*/
//-------------------ORIGIONAL NOTES PER DAN WAGONER FOR HANGMANDUINO SKETCH
SOURCECODE--------------------
// --Hangmanduino
// --
==================================================================================
=====
// --This sketch allows you to play hangman on your Arduino. There is a small list of words
// --defined in a variable, which the program randomly selects from. A potentiometer is used
// --for scrolling through the alphabet and a tactile switch for making selections. When you
// --select the letter, the program displays a * instead of the letter and you aren't allowed
// --to make that selection again. If you guess the word, you win the board is reset. If
// --you guess too many wrong, game over and the board is reset. If you hold the select button
// --for 2 seconds, the board is reset.
// --
// --For more information, please visit my website: www.nerdybynature.com
// --
// --Written By: Dan Wagoner
// --------------------------------------------------------------------------------------------------------
#include <Servo.h> //Needed to control servo
#include <LiquidCrystal.h>
#include <WString.h>
#include <Print.h>
#define POTPIN 3
#define SPEAKERPIN 5
#define BUTTONPIN 6
//#define servon 3 //power to servo
#define LASTBUTTONSTATE HIGH
#define DEBOUNCEDELAY 50
#define NUMWORDS 42
#define button2 3 //button inside box
//define notes for buzzer
#define LOWNOTE 100
#define ALOW 440
#define CLOW 261
#define ELOW 329
#define FLOW 349
#define CHIGH 523
#define EHIGH 659
#define GHIGH 784
#define FSHIGH 740
#define AHIGH 880
void(* resetFunc) (void) = 0; //declare reset function @ address 0
const char* words[] = {"jodhpurs", "czarevna", "higgler", "endenization", "quadrifid", "pyaemia",
"dagon", "quidam", "fanon", "shintiyan", "bodkin", "ambiloquy", "nawab",
"febrifuge", "feaze", "frizzly", "fylfot", "gamic", "bedizen", "behoof",
"hygeia", "azygous", "bavin", "huffy", "hurlyburly", "bayadere",
"assegai", "homoousian", "pawky", "ligneous", "copper", "jemidar",
"hamose", "japanning", "canakin", "choky", "mazard", "electrolyze",
"gulph", "methodize", "hadji", "acequia"};
const char letterVal[] = "abcdefghijklmnopqrstuvwxyz";
char guessLetter;
char guessLast;
char guessed[25];
char* secretWord;
int guessedCount = 1;
int wordSize;
int gotOne = 0;
int alreadyGuessed = 0;
int showAsterisk = 0;
int buttonState;
int hangman = 0;
int totalRight = 0;
long lastDebounceTime = 0;
Servo lock; //create a servo object called lock
int button2on; //integer for button inside box
LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10);
String guessWord = String(42);
// hangman graphic characters
byte topleft[] = { 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 };
byte topright[] = { 0x1C, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte bottomleft[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F, 0x1F, 0x1F };
byte bottomright[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte head[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x00, 0x00, 0x00 };
byte topbody[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x04, 0x04, 0x04 };
byte bottombody[] = { 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte rightarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x05, 0x06, 0x04 };
byte leftarm[] = { 0x1C, 0x04, 0x04, 0x0E, 0x0E, 0x15, 0x0E, 0x04 };
byte rightleg[] = { 0x04, 0x04, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00 };
byte leftleg[] = { 0x04, 0x04, 0x0A, 0x0A, 0x11, 0x00, 0x00, 0x00 };
byte leftarrow[] = { 0x10, 0x18, 0x1C, 0x1E, 0x1E, 0x1C, 0x18, 0x10 };
byte rightarrow[] = { 0x01, 0x03, 0x07, 0x0F, 0x0F, 0x07, 0x03, 0x01 };
void setup()
{
lock.write(160); //set lock servo to locked position before it is attatched to prevent servo
from moving to 90 degrees on startup
lock.attach(4); // Connected to servo to control lock
Serial.begin(9600);
pinMode (POTPIN, INPUT);
pinMode (BUTTONPIN, INPUT);
pinMode (SPEAKERPIN, OUTPUT);
// pinMode (servon, OUTPUT);
pinMode (button2, INPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Guess the word");
lcd.setCursor(1,1);
lcd.print("to get inside!");
delay(2000);
// pick a random word using analog 5 for random data
randomSeed(analogRead(5));
//newWord();
//draw the board
draw_board();
// digitalWrite(servon, HIGH);
lock.write(160);
delay(1000);
// digitalWrite(servon, LOW);
}
//void newWord(){
//pick a random word from the list
//int pick = TrueRandom.random(NUMWORDS);
//const char* pickWord = words[pick];
//guessWord = pickWord;
//secretWord = guessWord.getChars();
// wordSize = guessWord.length();
// Serial.println(guessWord); // print the word to serial for cheaters like me ;) ...or for testing
purposes.
//}
void draw_board(){
// define the custom characters
lcd.createChar(0, topleft);
lcd.createChar(1, topright);
lcd.createChar(2, bottomleft);
lcd.createChar(3, bottomright);
lcd.createChar(4, leftarrow);
lcd.createChar(5, rightarrow);
// draw blank hangman table
lcd.clear();
lcd.home();
lcd.write(2);
lcd.write(1);
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(3);
// print underlines
lcd.setCursor(3,1);
for (int x=0; x < wordSize; x++){
lcd.print("_");
}
}
void loop(){
//enable button to open lock from inside to allow the lid to be closed when the box is open
button2on = digitalRead(button2);
if (button2on == HIGH)
{
// digitalWrite(servon, HIGH); //apply power to servo
lock.write(10); //move servo to position 10 degrees
delay(5000); //Hold door lock open for 5 seconds
lock.write(160); //move servo to position 160 degrees
delay(1000); //wait for servo to catch up
// digitalWrite(servon, LOW); //shut off power to servo
}