Вы находитесь на странице: 1из 21

// //****************************************// //

//*********************************************************//
//********************** Whack A Mole Game ****************//
//*************************** Ver 004 *********************//
// //*****************************************// //

//By Darren Farrar

/*
**********************************************
****** Improvements since last Version *******
**********************************************
Ver 002 Added sound
Ver 003 Added intro tune
Ver 004

ToDo:-
add interrupt button + sub routine
fix Demo mode
fix gameover

**********************************************
**********************************************
*/

/*
**********************************************
**** WIRE COLOURS FOR THE MOLES **************
**********************************************
* *
* Push Button :- Red & Black *
* *
* LED's :- Brown to 9v Rail and Blue to *
* transistor outputs (after the resistors *
* see circuit diagram) *
* *
**********************************************
**********************************************
**********************************************
*/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

//***********************************************//
//*** Setup LCD Display ***//
//***********************************************//
#include <LiquidCrystal.h>; // Loads LCD Library
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); // alocates pins for the LCD display

//********************************************************//
//*** alocate variables to digital output pins ***//
//********************************************************//
int mole_1 = 10;
int mole_2 = 11;
int mole_3 = 12;
int mole_4 = 13;
int mole_5 = A2;
int mole_6 = A3;
int mole_7 = A4;
int mole_8 = A5;
int sound = 3;

//****************************************************//
//*** declare variables for mole on mole off beeps ***//
//****************************************************//

int on_beep = 400;


int off_beep = 1000;
int on_off_beep_duration = 200;
//***************************************************//
//*** declare variables for moles state high/low ***//
//***************************************************//
boolean mole_state_1 = LOW;
boolean mole_state_2 = LOW;
boolean mole_state_3 = LOW;
boolean mole_state_4 = LOW;
boolean mole_state_5 = LOW;
boolean mole_state_6 = LOW;
boolean mole_state_7 = LOW;
boolean mole_state_8 = LOW;

//***************************************************//
//*** declare variables for recording ***//
//*** if the LEDS are on or off ***//
//*** (used to count the number of ***//
//*** moles on at one time) ***//
//***************************************************//
int led_on_1;
int led_on_2;
int led_on_3;
int led_on_4;
int led_on_5;
int led_on_6;
int led_on_7;
int led_on_8;
int led_total;

//***********************************************//
//*** declare variables for input buttons ***//
//*** 5 buttons per analogue input ***//
//*** 10k, 22k,33k,47k & 68K Resistors are ***//
//*** used to create a potentail divider with ***//
//*** a 22k resistor which produces a ***//
//*** voltage on analog i/p's (A0, A1) ***//
//***********************************************//
int sw_1_to_5 = A0; //pin alocation
int sw_6_to_10 = A1; //pin alocation
int button_1_to_5; //var for the value of the pin
int button_6_to_10; //var for the value of the pin

//***********************************************//
//*** declare variables for scores ***//
//***********************************************//
int hi_score;
int score;

//***********************************************//
//*** declare timing variables ***//
//*** used to activate parts of the program ***//
//*** that run only once per number of cycles ***//
//***********************************************//

int mole_timer = 0; // increases until = mole_timer_cycle_time


int mole_timer_cycle_time = 300; // decreasing this reduces the time between
moles lighting up
int mole_timer_reduce_by = 2; // reduces the time between moles lighting up
each time a mole lights

int Demo_timer = 0;
int Demo_timer_cycle_time = 1000;
int Demo_timer_reduce_by = 3;

int random_mole = 0; // variable to store a randmon number used for choosing


which mole to light next
//*************************************************************//
//*** variable to indicate which part of the program to run ***//
//*** 1 = demo, 2 = playing, 3 = game over ***//
//*************************************************************//
int program_state = 2;

//***********************************************//
//*** Setup part of the program, runs once ***//
//***********************************************//
void setup()
{
lcd.begin(16, 2); // sets LCD rows and collums

pinMode(mole_1, OUTPUT);
pinMode(mole_2, OUTPUT);
pinMode(mole_3, OUTPUT);
pinMode(mole_4, OUTPUT);
pinMode(mole_5, OUTPUT);
pinMode(mole_6, OUTPUT);
pinMode(mole_7, OUTPUT);
pinMode(mole_8, OUTPUT);
// sets pins connected to the moles as outputs

int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:


int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second


// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(sound, melody[thisNote],noteDuration);

// to distinguish the notes, set a minimum time between them.


// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(sound);
}

pinMode(sw_1_to_5, INPUT);
pinMode(sw_6_to_10, INPUT);
// sets analog pins connected to the switches as inputs
}

//*************************************************//
//*** Main part of the program, runs in a loop ***//
//*************************************************//

void loop()
{
//*** parts of the program that need to run every cycle ***//

random_mole = random(1, 9);


//produces a random number from 1 to 8

mole_timer = mole_timer + 1; // increases the count by one


Demo_timer = Demo_timer + 1; // increases the count by one

digitalWrite(mole_1, mole_state_1);
digitalWrite(mole_2, mole_state_2);
digitalWrite(mole_3, mole_state_3);
digitalWrite(mole_4, mole_state_4);
digitalWrite(mole_5, mole_state_5);
digitalWrite(mole_6, mole_state_6);
digitalWrite(mole_7, mole_state_7);
digitalWrite(mole_8, mole_state_8);
// set each mole either on or off

button_1_to_5 = analogRead(sw_1_to_5);
button_6_to_10 = analogRead(sw_6_to_10);
// reads the value of the 2 button inputs

//***********************************************//
//*** Demo program ***//
//***********************************************//

if (program_state == 1)
{
if (Demo_timer == Demo_timer_cycle_time)
{
Demo_timer_cycle_time = Demo_timer_cycle_time - Demo_timer_reduce_by;

if (random_mole == 1)
{
mole_state_1 = HIGH;
}

if (random_mole == 2)
{
mole_state_2 = HIGH;
led_on_2 = 1;
}

if (random_mole == 3)
{
mole_state_3 = HIGH;
}

if (random_mole == 4)
{
mole_state_4 = HIGH;
}

if (random_mole == 5)
{
mole_state_5 = HIGH;
}
if (random_mole == 6)
{
mole_state_6 = HIGH;

}
if (random_mole == 7)
{
mole_state_7 = HIGH;
}

if (random_mole == 8)
{
mole_state_8 = HIGH;
}

if (random_mole == 0)
{
mole_state_1 = LOW;
mole_state_2 = LOW;
mole_state_3 = LOW;
mole_state_4 = LOW;
mole_state_5 = LOW;
mole_state_6 = LOW;
mole_state_7 = LOW;
mole_state_8 = LOW;

lcd.setCursor(0, 0);
lcd.print("**Whack-A-Mole**");
lcd.setCursor(0, 1);
lcd.print("Hi Score = ");
lcd.setCursor(11, 1);
lcd.print(random_mole);
}

Demo_timer = 0;
}

if (button_6_to_10 > 200


&& button_6_to_10 < 289)
{
program_state = 2;
lcd.clear();
}

//***********************************************//
//*** Game Playing ***//
//***********************************************//

if (program_state == 2)
{

if (mole_timer == mole_timer_cycle_time)
{
mole_timer_cycle_time = mole_timer_cycle_time - mole_timer_reduce_by;

if (random_mole == 1)
{
mole_state_1 = HIGH;
led_on_1 = 1;
}

if (random_mole == 2)
{
mole_state_2 = HIGH;
led_on_2 = 1;
}
if (random_mole == 3)
{
mole_state_3 = HIGH;
led_on_3 = 1;
}
if (random_mole == 4)
{
mole_state_4 = HIGH;
led_on_4 = 1;
}
if (random_mole == 5)
{
mole_state_5 = HIGH;
led_on_5 = 1;
}
if (random_mole == 6)
{
mole_state_6 = HIGH;
led_on_6 = 1;
}
if (random_mole == 7)
{
mole_state_7 = HIGH;
led_on_7 = 1;
}
if (random_mole == 8)
{
mole_state_8 = HIGH;
led_on_8 = 1;
}
mole_timer = 0;
tone(sound, 1000, 200);
}

//*********button 1**********//
if (button_1_to_5 > 606
&& mole_state_1 == HIGH)
{
score = score + 10;
mole_state_1 = LOW;
led_on_1 = 0;
tone(sound, 400, 200);
}

//*********button 2**********//
if (button_1_to_5 > 416
&& button_1_to_5 < 606
&& mole_state_2 == HIGH)
{
score = score + 10;
mole_state_2 = LOW;
led_on_2 = 0;
tone(sound, 400, 200);
}

//*********button 3**********//
if (button_1_to_5 > 368
&& button_1_to_5 < 461
&& mole_state_3 == HIGH)
{
score = score + 10;
mole_state_3 = LOW;
led_on_3 = 0;
tone(sound, 400, 200);
}

//*********button 4**********//
if (button_1_to_5 > 289
&& button_1_to_5 < 368
&& mole_state_4 == HIGH)
{
score = score + 10;
mole_state_4 = LOW;
led_on_4 = 0;
tone(sound, 400, 200);
}

//*********button 5**********//
if (button_1_to_5 > 200
&& button_1_to_5 < 289
&& mole_state_5 == HIGH)
{
score = score + 10;
mole_state_5 = LOW;
led_on_5 = 0;
tone(sound, 400, 200);
}

//*********button 6**********//
if (button_6_to_10 > 606
&& mole_state_6 == HIGH)
{
score = score + 10;
mole_state_6 = LOW;
led_on_6 = 0;
tone(sound, 400, 200);
}
//*********button 7**********//
if (button_6_to_10 > 416
&& button_6_to_10 < 606
&& mole_state_7 == HIGH)
{
score = score + 10;
mole_state_7 = LOW;
led_on_7 = 0;
tone(sound, 400, 200);
}

//*********button 8**********//
if (button_6_to_10 > 368
&& button_6_to_10 < 461
&& mole_state_8 == HIGH)
{
score = score + 10;
mole_state_8 = LOW;
led_on_8 = 0;
tone(sound, 400, 200);
}
led_total = led_on_1 + led_on_2 + led_on_3 + led_on_4 + led_on_5 + led_on_6
+ led_on_7 + led_on_8;

lcd.setCursor(0, 0);
lcd.print("Score");
lcd.setCursor(0, 1);
lcd.print(score);

if(led_total > 4)
{
program_state = 3;
lcd.clear();

}
}
//***********************************************//
//*** Game Over ***//
//***********************************************//

if (program_state == 3)
{

lcd.setCursor(0, 0);
lcd.print("Gameover");
lcd.setCursor(0, 1);
lcd.print("Your Score");
lcd.setCursor(11, 1);
lcd.print(score);

/*if ( hi_score < score )


{
hi_score = score;

lcd.setCursor(0, 0);
lcd.print("Gameover");
lcd.setCursor(0, 1);
lcd.print("New Hi Score");
lcd.setCursor(14, 1);
lcd.print(score);
}

if ( hi_score > score )


{

lcd.setCursor(1, 1);
lcd.print("Gameover");
lcd.setCursor(0, 1);
lcd.print("Your Score");
lcd.setCursor(11, 1);
lcd.print(score);
}

if (button_1_to_5 > 606


&& mole_state_1 == HIGH)

{
mole_state_1 = LOW;
mole_state_2 = LOW;
mole_state_3 = LOW;
mole_state_4 = LOW;
mole_state_5 = LOW;
mole_state_6 = LOW;
mole_state_7 = LOW;
mole_state_8 = LOW;

lcd.clear();

program_state = 2;
score = 0;
}
*/
}

Вам также может понравиться