//#define RED 19 // pin 19 is always PWM capable according to LaunchPad standard
//#define GREEN 18 // may need to change this for your LaunchPad
//#define BLUE 11 // may need to change this for your LaunchPad
//I changed this to RGB LEDs on board, can change back
#define RED RED_LED
#define GREEN GREEN_LED
#define BLUE BLUE_LED
byte seven_segment_digits[10][7] = { { 0,0,0,0,0,0,1 }, // display '0'
{ 1,0,0,1,1,1,1 }, // display '1'
{ 0,0,1,0,0,1,0 }, // display '2'
{ 0,0,0,0,1,1,0 }, // display '3'
{ 1,0,0,1,1,0,0 }, // display '4'
{ 0,1,0,0,1,0,0 }, // display '5'
{ 0,1,0,0,0,0,0 }, // display '6'
{ 0,0,0,1,1,1,1 }, // display '7'
{ 0,0,0,0,0,0,0 }, // display '8'
{ 0,0,0,1,1,0,0 } // display '9'
};
byte seven_segment_celebrate[6][7] = { { 0,1,1,1,1,1,1 },
{ 1,0,1,1,1,1,1 },
{ 1,1,0,1,1,1,1 },
{ 1,1,1,0,1,1,1 },
{ 1,1,1,1,0,1,1 },
{ 1,1,1,1,1,0,1 },
};
#define timePrsd 250
#define timeBtwnSeq 500
#define splashTime 200
int btn_pin[]={11,12,13}; // change for launchpad
int led_pin[]={17,18,19}; // change for launchpad
int swch_pin = 9;
int led[]={LOW,LOW,LOW}; //current value
int bState = 0;
int swch = HIGH; // current value
int swch_new = HIGH; // value just read
int swch_changed = LOW; // turns HIGH for one cycle after each keypress
int swch_last= HIGH; // the previous reading from the input pin
long swch_lastDebounceTime = 0; // the last time the output pin was toggled
long swch_debounceDelay = 50; // the debounce time; increase if the output flickers
int btn[]={LOW,LOW,LOW}; // current value
int btn_new[]={LOW,LOW,LOW}; // value just read
int btn_changed[]={HIGH,HIGH,HIGH}; // turns HIGH for one cycle after each keypress
int btn_last[]={LOW,LOW,LOW}; // the previous reading from the input pin
long btn_lastDebounceTime[]={0,0,0}; // the last time the output pin was toggled
long btn_debounceDelay = 50; // the debounce time; increase if the output flickers
int currentNum = 2; // current highest index of the sequence
int roundNum = 0;
int sequence[15]; // maximum length of memory sequence
int timeBtwnLED = 500; // this will get smaller as the rounds progress
void debounceButtons() // debuonces buttons
{
for (int i=0; i<3; ++i)
{
btn_changed[i]=LOW;
btn_new[i]=digitalRead(btn_pin[i]);
if (btn_new[i] != btn_last[i])
{
btn_lastDebounceTime[i] = millis();
}
if ((millis() - btn_lastDebounceTime[i]) > btn_debounceDelay)
{
if (btn_new[i] != btn[i])
{
btn_changed[i]=HIGH;
btn[i] = btn_new[i];
}
}
btn_last[i]=btn_new[i];
}
}
void debounceSwitch() // debuonces switch
{
swch_changed=HIGH;
swch_new=digitalRead(swch_pin);
if (swch_new != swch_last)
{
swch_lastDebounceTime = millis();
}
if ((millis() - swch_lastDebounceTime) > swch_debounceDelay)
{
if (swch_new != swch)
{
swch_changed = LOW;
swch = swch_new;
}
}
swch_last = swch_new;
}
void makeSequence() // generates a random sequence
{
for (int i = 0; i <= currentNum; ++i)
{
sequence[i] = random(3);
}
}
void showSequence() //shows the sequence
{
for (int i=0; i<=currentNum; ++i)
{
delay(timeBtwnLED);
digitalWrite(led_pin[sequence[i]], HIGH);
delay(timeBtwnLED - 250); //maybe?
digitalWrite(led_pin[sequence[i]], LOW);
}
}
void nextRound() //prepares everything for next round
{
currentNum++;
timeBtwnLED -= 10;
roundNum++;
int pin = 2;
for (int segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_segment_digits[roundNum][segCount]);
++pin;
}
}
boolean checkSequence(int correctAnswer) // checks if answer is right or wrong
{
int answerIs=LOW;
int receivedAnswer=LOW;
while ( receivedAnswer == LOW )
{
debounceButtons();
for (int i=0; i<3; ++i)
{
if ((btn_changed[i]==HIGH) and (btn[i] == LOW))
{
if (i == correctAnswer)
{
answerIs=HIGH;
}
receivedAnswer=HIGH;
do
{
digitalWrite (led_pin[i], HIGH);
}
while (digitalRead(btn_pin[i]) != HIGH);
digitalWrite (led_pin[i], LOW);
}
}
}
return answerIs;
}
void endGame() // shows the end of the game
{
for (int x = 0; x <= currentNum; ++x)
{
sequence[x] = 0;
}
for(int i = 0; i < 3; i++ )
{
digitalWrite(led_pin[0], HIGH);
digitalWrite(led_pin[1], HIGH);
digitalWrite(led_pin[2], HIGH);
digitalWrite(led_pin[3], HIGH);
delay (500);
digitalWrite(led_pin[0], LOW);
digitalWrite(led_pin[1], LOW);
digitalWrite(led_pin[2], LOW);
digitalWrite(led_pin[3], LOW);
delay (500);
}
currentNum = 2;
roundNum = 0;
timeBtwnLED = 500;
int pin = 2;
for (int segCount = 0; segCount < 7; ++segCount)
{
digitalWrite(pin, seven_segment_digits[roundNum][segCount]);
++pin;
}
delay (500);
}
void victoryLights() //modified Fade from Energia Examples
{
int brightness = 0; // how bright the LED is
int fadeAmount = 20; // how many points to fade the LED by, variable to change speed
int color = 0; //color of current LED
int numLoops = 0;
int colors[3]= {RED, GREEN, BLUE};
while(numLoops < 5)//# of times to shine victory light sequence
{
analogWrite(colors[color], brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness >= 255)
{
fadeAmount = -fadeAmount ;
}
if (brightness == 0)
{
analogWrite(colors[color], brightness);
color++;
if (color == 3)
{
color = 0;
numLoops++;
}
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
void victoryDisplay() //Celebrate!
{
for(int cycles = 0; cycles < 10; ++cycles)
{
for(int count = 0; count< 5; ++count)
{
int pin = 2;
for (int segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_segment_celebrate[count][segCount]);
++pin;
}
delay(30);
}
}
}
void game() //this is the whole game. it is basically a loop with instructions
{
int answer = HIGH;
int pin = 2;
++roundNum;
for (int segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_segment_digits[roundNum][segCount]);
++pin;
}
do
{
//generates and displays the sequence
makeSequence();
showSequence();
for (int i = 0; i <= currentNum; ++i )
{
//checks the user input
answer = checkSequence(sequence[i]);
if (answer == LOW) //wrong answer
{
//RGB LED lights up red
digitalWrite(RED_LED, HIGH);
delay(3000);
digitalWrite(RED_LED, LOW);
delay(1000);
break;
}
}
if (answer == HIGH) //right answer
{
//RGB LED lights up green
digitalWrite(GREEN_LED, HIGH);
delay(3000);
digitalWrite(GREEN_LED, LOW);
delay(500);
//calls the update round function
nextRound();
}
if (roundNum >= 9) //Game end
{
victoryLights();
victoryDisplay();
break;
}
}
while (answer == HIGH);
}
void splashScreen() //This waits for the user to start tha game
{
int check = 0;
//stays in the loop until the check = 1, given by checkSwitch()
do
{
check = checkSwitch();
long timeStart = millis();
do
{
digitalWrite(led_pin[0],HIGH);
check = checkSwitch();
if (check == 1)
break;
}
while (millis() <= timeStart + splashTime);
check = checkSwitch();
timeStart = millis();
do
{
digitalWrite(led_pin[0],LOW);
check = checkSwitch();
if (check == 1)
break;
digitalWrite(led_pin[1],HIGH);
}
while (millis() <= timeStart + splashTime);
check = checkSwitch();
timeStart = millis();
do
{
digitalWrite(led_pin[1],LOW);
check = checkSwitch();
if (check == 1)
break;
digitalWrite(led_pin[2],HIGH);
}
while (millis() <= timeStart + splashTime);
check = checkSwitch();
timeStart = millis();
do
{
digitalWrite(led_pin[2],LOW);
check = checkSwitch();
if (check == 1)
break;
digitalWrite(led_pin[1],HIGH);
}
while (millis() <= timeStart + splashTime);
check = checkSwitch();
digitalWrite(led_pin[1],LOW);
check = checkSwitch();
if (check == 1)
break;
}
while(check == 0);
delay(1000);
}
int checkSwitch() //checks the state of switch and returns 1 or 0
{
debounceSwitch();
if ((digitalRead(swch_pin) == LOW))
{
return 1;
}
else
return 0;
}
void setup()
{
randomSeed(analogRead(0));
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
//pins for the 7 segment display
pinMode(2, OUTPUT); // set segment A as output
pinMode(3, OUTPUT); // set segment B as output
pinMode(4, OUTPUT); // set segment C as output
pinMode(5, OUTPUT); // set segment D as output
pinMode(6, OUTPUT); // set segment E as output
pinMode(7, OUTPUT); // set segment F as output
pinMode(8, OUTPUT); // set segment G as output
pinMode(9, INPUT); // set tilt switch as input
for (int i=0; i<3; ++i)
{
pinMode(btn_pin[i],INPUT_PULLUP); // set each button as pull up input
pinMode(led_pin[i],OUTPUT); // set each LED as output
}
}
void loop()
{
splashScreen();
game();
endGame();
}
Comments
Please log in or sign up to comment.