Go Down

Topic: Help Tech Newbie (Read 1 time) previous topic - next topic

cghove

Aug 12, 2015, 07:23 pm Last Edit: Aug 14, 2015, 10:33 am by cghove
updated posts further down with propper schematic and code Aug 14,2015

Hello im starting with saying sorry for my bad English.
Im new to Arduino, but usualy quite good at copying things from blue prints, and i wanted to try and make a geocache based on Arduino, and fell in love with the hangmanduino design tried to copy it, but the servo keeps moving, and when i try to connect the push buttons, the game automaticly wins after just one random letter, i have connected everything like this (exept for push buttons)
And the code im trying to use is attached in pdf:
(by the way what do i have to do to change the code from the servo, to a solenoid lock like this: solenoid lock that is once someone wins the game, this lock gets a 5 second power signal to open)

PaulRB

Hi, and welcome to the forum.

Please remake your schematic properly. It is unreadable. Take a look at other schematics and you will understand better what is needed.

Also please post your sketch/code into your forum post, using code tags (the </> icon) so it looks like
Code: [Select]
this.

Paul

cghove

#2
Aug 12, 2015, 09:25 pm Last Edit: Aug 12, 2015, 09:26 pm by cghove Reason: hmm... the fzz file werent approwd
Hmmm... let me try and add the sketch from Fritzing and see if thats better.... but on the code i got an error that i used to many words to post??

cghove

code part 1
Code: [Select]
/*
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
 }


cghove

code part 2:
Code: [Select]

 
// letter selection via potentiometer
 int potVal = analogRead(POTPIN) / 40; // 1024 / 26 ~= 39
 guessLetter = letterVal[potVal];

 // if letter is different from last, print to lcd
 // this prevents from printing the same char over and over
 if (guessLetter != guessLast){
 guessLast = guessLetter;
 showAsterisk = 0;

 // cycle through all guessed letters and determine whether to show * or the letter
 for (int x=0; x < guessedCount; x++){
 if (guessLetter == guessed[x]){
 showAsterisk = 1;
 }
 }
 // print letters to the left of selected letter
 lcd.setCursor(3,0);
 for (int x=5; x >= 1 ; x--){
 if (potVal - x >= 0){
 lcd.print(letterVal[potVal - x]);
 }
 else{
 lcd.print("|");
 }
 }

 // print left arrow
 lcd.write(4);

 // print the letter
 if (showAsterisk == 0){
 lcd.setCursor(9,0);
 lcd.print(guessLetter);
 alreadyGuessed = 0;
 }
 // print a *
 else{
 lcd.setCursor(9,0);
 lcd.print("*");
 alreadyGuessed = 1;
 }

 // print right arrow
 lcd.write(5);

 // print letters to the right of selected letter
 lcd.setCursor(11,0);
 for (int x=1; x <= 5 ; x++){
 if (potVal + x <= 25){
 lcd.print(letterVal[potVal + x]);
 }
 else{
 lcd.print("|");
 }
 }
 }


 // select button...debounced
 int reading = digitalRead(BUTTONPIN);
 
 // filter out noise and debounce
 if (reading != LASTBUTTONSTATE) {
 lastDebounceTime = millis();
 }

 // if select button is pressed, check word for selected letter
 if ((millis() - lastDebounceTime) > DEBOUNCEDELAY) {
 gotOne = 0;
 if (alreadyGuessed == 0){
 alreadyGuessed = 1;
 lcd.setCursor(9,0);
 lcd.print("*");
 for (int i = 0; i < wordSize; i++) {
 if (secretWord[i] == guessLetter) {
 lcd.setCursor(i+3,1);
 lcd.print(guessLetter);
 gotOne = 1;
 totalRight = totalRight + 1;
 }
 }
 // add letter to guessed letter array
 guessed[guessedCount] = guessLetter;
 guessedCount++;
 lastDebounceTime = millis();
 // none of the letters match, draw the next body part on the hangman
 if (gotOne == 0){
 buzz(LOWNOTE, 500);
 hangman++;
 draw_hangman(hangman);
 }
 else{
 // letter is in word, sound buzzer
 buzz(FSHIGH, 30);
 buzz(AHIGH, 50);
 }

 //all letters have been guessed...WIN!
 if (totalRight == wordSize){
 gameOver(1);
 }
 }
 // this letter has already been guessed, sound buzzer
 }
}
void draw_hangman(int var){

 switch (var){
 case 1:
 lcd.createChar(1, head); // head
 break;
 case 2:
 lcd.createChar(1, topbody); // body
 lcd.createChar(3, bottombody);
 break;
 case 3:
 lcd.createChar(1, rightarm); // right arm
 break;
 case 4:
 lcd.createChar(1, leftarm); // left arm
 break;
 case 5:
 lcd.createChar(3, rightleg); // right leg
 break;
 case 6:
 lcd.createChar(3, leftleg); // left leg
 break;
 case 7:
 gameOver(0);
 default:
 break;
 }
}
void gameOver(int whatToDo){

 // decide whether win, lose or restart game
 switch (whatToDo){
 case 0: // GAME OVER
 lcd.clear();
 lcd.setCursor(6,0);
 lcd.print("ACCESS");
 lcd.setCursor(6,1);
 lcd.print("DENIED");

 //buzzer sound
 buzz(ELOW, 500); // GAME OVER!
 buzz(CLOW, 1000); // sound buzzer
 break;
 case 1: // WINNER
 
 // buzzer sound
 buzz(ALOW, 150);
 buzz(CHIGH, 150);
 buzz(EHIGH, 150);
 buzz(AHIGH, 150);
 delay(150);
 buzz(GHIGH, 150);
 buzz(AHIGH, 500);

 lcd.clear();
 lcd.setCursor(4,0);
 lcd.print("ACCESS");
 lcd.setCursor(4,1);
 lcd.print("GRANTED");
 // 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


 }
 delay(2000);
 resetFunc(); // reset arduino for a new game
}
void buzz (int frequencyInHertz, long timeInMilliseconds){
 Serial.println(frequencyInHertz);
 long delayAmount = (long)(1000000/frequencyInHertz);
 long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
 for (int x=0; x < loopTime; x++){
 digitalWrite(SPEAKERPIN, HIGH);
 delayMicroseconds(delayAmount);
 digitalWrite(SPEAKERPIN, LOW);
 delayMicroseconds(delayAmount);
 }
 delay(20);
}

cghove

#5
Aug 14, 2015, 10:27 am Last Edit: Aug 14, 2015, 12:29 pm by cghove Reason: Added .ino
Ok, think this is what u want from my schematic.
What i'm having trouble with now is that it seems that the script wont pick random word, because once i don't see anny lines indicating number of letters and once i push the select button (to select a letter) the game tells me i have won, and the servo opens.....
code 2 follow in 2 posts..
Please help....  :)
Edit: Added code as .ino attachment in this post

cghove

Code: [Select]
/*
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 4  //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);
 
}

cghove

Code: [Select]
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((uint8_t)0);
  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
  }
 
 
   

 // letter selection via potentiometer
  int potVal = analogRead(POTPIN) / 40;      // 1024 / 26 ~= 39
  guessLetter = letterVal[potVal];
 
  // if letter is different from last, print to lcd
  // this prevents from printing the same char over and over
  if (guessLetter != guessLast){
    guessLast = guessLetter;
    showAsterisk = 0;
   
    // cycle through all guessed letters and determine whether to show * or the letter
    for (int x=0; x < guessedCount; x++){
      if (guessLetter == guessed[x]){
        showAsterisk = 1;
      }
    }
    // print letters to the left of selected letter
    lcd.setCursor(3,0);
    for (int x=5; x >= 1 ; x--){
      if (potVal - x >= 0){
        lcd.print(letterVal[potVal - x]);
      }
      else{
        lcd.print("|");
      }
    }
   
    // print left arrow
    lcd.write(4);
   
    // print the letter
    if (showAsterisk == 0){
      lcd.setCursor(9,0);
      lcd.print(guessLetter);
      alreadyGuessed = 0;
    }
    // print a *
    else{
      lcd.setCursor(9,0);
      lcd.print("*");
      alreadyGuessed = 1;
    }
       
    // print right arrow
    lcd.write(5);
   
    // print letters to the right of selected letter
    lcd.setCursor(11,0);
    for (int x=1; x <= 5 ; x++){
      if (potVal + x <= 25){
        lcd.print(letterVal[potVal + x]);
      }
      else{
        lcd.print("|");
      }
    }
  }
 
 
  // select button...debounced
  int reading = digitalRead(BUTTONPIN);
 
  // filter out noise and debounce
  if (reading != LASTBUTTONSTATE) {
    lastDebounceTime = millis();
  }
 
  // if select button is pressed, check word for selected letter
  if ((millis() - lastDebounceTime) > DEBOUNCEDELAY) {
    gotOne = 0;
    if (alreadyGuessed == 0){
      alreadyGuessed = 1;
      lcd.setCursor(9,0);
      lcd.print("*");
      for (int i = 0; i < wordSize; i++) {
        if (secretWord[i] == guessLetter) {
          lcd.setCursor(i+3,1);
          lcd.print(guessLetter);
          gotOne = 1;
          totalRight = totalRight + 1;
        }
      }
 
      // add letter to guessed letter array
      guessed[guessedCount] = guessLetter;
      guessedCount++;
      lastDebounceTime = millis();

      // none of the letters match, draw the next body part on the hangman
      if (gotOne == 0){
        buzz(LOWNOTE, 500);
        hangman++;
        draw_hangman(hangman);
      }
      else{
        // letter is in word, sound buzzer
        buzz(FSHIGH, 30);
        buzz(AHIGH, 50);
      }
     
      //all letters have been guessed...WIN!
      if (totalRight == wordSize){
        gameOver(1);
      }
    }
    // this letter has already been guessed, sound buzzer
  }
}

void draw_hangman(int var){
 
  switch (var){
    case 1: 
      lcd.createChar(1, head);           // head
      break;
    case 2:
      lcd.createChar(1, topbody);        // body
      lcd.createChar(3, bottombody);
      break;
    case 3:
      lcd.createChar(1, rightarm);       // right arm
      break;
    case 4:
      lcd.createChar(1, leftarm);        // left arm
      break;
    case 5:
      lcd.createChar(3, rightleg);       // right leg
      break;
    case 6:
      lcd.createChar(3, leftleg);        // left leg
      break;
    case 7:
      gameOver(0);
    default:
      break;
  }
}

void gameOver(int whatToDo){
 
  // decide whether win, lose or restart game 
  switch (whatToDo){
    case 0:  // GAME OVER
      lcd.clear();
      lcd.setCursor(6,0);
      lcd.print("ACCESS");
      lcd.setCursor(6,1);
      lcd.print("DENIED");
   
      //buzzer sound
      buzz(ELOW, 500);                  // GAME OVER!
      buzz(CLOW, 1000);                 // sound buzzer
      break;
    case 1:  // WINNER
   
          // buzzer sound
      buzz(ALOW, 150);
      buzz(CHIGH, 150);
      buzz(EHIGH, 150);
      buzz(AHIGH, 150);
      delay(150);
      buzz(GHIGH, 150);
      buzz(AHIGH, 500);
     
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print("ACCESS");
      lcd.setCursor(4,1);
      lcd.print("GRANTED");
  //    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
       
   

  }
  delay(2000);
  resetFunc();      // reset arduino for a new game
}

void buzz (int frequencyInHertz, long timeInMilliseconds){ 
  Serial.println(frequencyInHertz);
    long delayAmount = (long)(1000000/frequencyInHertz);
    long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
    for (int x=0; x < loopTime; x++){
      digitalWrite(SPEAKERPIN, HIGH);
      delayMicroseconds(delayAmount);
      digitalWrite(SPEAKERPIN, LOW);
      delayMicroseconds(delayAmount);
    }
    delay(20);
}

TomGeorge

Hi,
Attach your entire code as an ino code, that way you do not have to split it.

Tom..... :)
Everything runs on smoke, let the smoke out, it stops running....

cghove

Entire code is attached as .ino....

mathers

I think you swapped Vin and 5V pins on the Arduino

Vin should be connedted to your power supply, and the servo should be connected to either the arduino 5V, or directly to the power supply.

What are you using as a power supply ?

cghove

Servo is connected directly to 5v on arduino and the Vin port gives power to my breadbord, for the moment im powering via usb, but i'm waiting on a 9v batery holder to connect to the power soccet on the arduino board....

cghove

#12
Aug 14, 2015, 02:29 pm Last Edit: Aug 14, 2015, 02:38 pm by cghove
I have now figured out almost everything xept, that when ever i press the pin button to choose a letter the game lets me win automaticly.....  :smiley-confuse:
That is i don't tink the code 2 pick a random word works...

cghove

#13
Aug 14, 2015, 03:38 pm Last Edit: Aug 14, 2015, 03:40 pm by cghove
Update the push button and win thing was just me beeing stupid and kept pressing the wrong button, but i just can't figure out how to choose a random word from the defined list in the script..... :smiley-confuse: + my potentiometer won't scroll past the letter V so i can't choose WXYZ

cghove

The problem i'm having must be with me not being able 2 have this piece og code "open" error: no member named 'getChars' in 'String'
 secretWord = guessWord.getChars(); once i putt // in front of secretWord = guessWord.getChars(); i'm able to transfer the program onto my arduino, but i can't get the game to pick a word from the "word bank"
Anyone got am idea on how i can fix it?

Go Up