#include <stdio.h>
#include <stdlib.h>
// set up the combinations of segments to display in a 2-D array
byte seven_segment_digits[5][7] = { { 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,1,0,0,0,1,1 }, // display 'u'
{ 1,0,0,0,0,1,0 } // display 'd'
};
// Stores the states of each of the five inputs.
// 0 means the input is active.
int inputStates[] = {0, 0, 0, 0, 0};
// Set the pins of the LEDs, buttons, tilt switch,
// and the first segment of the 7-segment display.
// These can be modified depending on circuit layout.
int redPin = 17;
int greenPin = 18;
int buttonPins[] = {19, 14, 15, 13};
int segmentPin = 3;
// Reads the inputs from the pins.
void read() {
for (int i = 0; i < 4; i++) {
inputStates[i] = digitalRead(buttonPins[i]);
}
inputStates[4] = !inputStates[3];
}
// Runs once at the beginning of the program
void setup() {
// Sets the pins to input or output
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
for (int i = 0; i < 7; i++) {
pinMode(segmentPin + i, OUTPUT);
}
// Turn the segments off to start.
for(int i = 0; i < 7; i++) {
digitalWrite(segmentPin + i, HIGH);
}
// Read the input
read();
// Loop until a button is pressed.
int seed = 0;
while (inputStates[0] != 0 && inputStates[1] != 0 && inputStates[2] != 0) {
read();
if (seed / 100000 % 2 == 0) {
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
} else {
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
}
int lastOffset = (seed - 1) / 10000 % 6;
int offset = seed / 10000 % 6;
int offset2 = (seed / 10000 + 1) % 6;
digitalWrite(segmentPin + lastOffset, HIGH);
digitalWrite(segmentPin + offset, LOW);
digitalWrite(segmentPin + offset2, LOW);
seed++;
}
// Seed the RNG.
srand(seed);
//Turn everything off to start.
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
for(int i = 0; i < 7; i++) {
digitalWrite(segmentPin + i, HIGH);
}
}
// A function that returns which input has been set to 0 since the last read().
int getChange() {
int oldStates[5];
for (int i = 0; i < 5; i++) {
oldStates[i] = inputStates[i];
}
read();
for (int i = 0; i < 5; i++) {
if (oldStates[i] != inputStates[i]) {
if (inputStates[i] == 0) {
return i;
}
}
}
return -1;
}
// The length of the pattern
int length = 1;
// This function should be repeatedly run after setup.
void loop() {
// Read the current state.
read();
// Generate a pattern
int pattern[length];
int tilt = inputStates[3];
for (int i = 0; i < length; i++) {
pattern[i] = rand() % 4;
// Only have an 'u' input if the device is tilted down,
// and vice versa.
if (pattern[i] == 3) {
if (tilt == 0) {
pattern[i] = 4;
}
tilt = !tilt;
}
}
// Show the pattern.
delay(1000);
for (int button = 0; button < length; button++) {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPin + i, seven_segment_digits[pattern[button]][i]);
}
delay(200);
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPin + i, 1);
}
delay(100);
}
// Read the current state.
read();
// Check each input state change against the pattern.
int button = 0;
while (button < length) {
int change = getChange();
if (change == pattern[button]) {
// Input was correct.
button++;
} else if (change != -1) {
// Input was incorrect.
break;
}
delay(50);
}
// Blink the correct LED, and change the pattern length for difficulty.
if (button == length) {
digitalWrite(greenPin, HIGH);
delay(200);
digitalWrite(greenPin, LOW);
length++;
} else {
digitalWrite(redPin, HIGH);
delay(200);
digitalWrite(redPin, LOW);
if (length > 1) {
length--;
}
}
}
Comments
Please log in or sign up to comment.