Ok almost there. I've got the capacitors set up as the input. and the game plays perfect. except that it's still random.
how do i replace the random array with a predetermined sequence?
thanks again guys
heres the code
/*Capacitor Simon Says game by Robert Spann - Edited by Daniel Waugh - 2011*/
int led1 = 7; // first LED pin
int led2 = 6; // second LED pin
int led3 = 5; // third LED pin
//int led4 = 4; // fourth LED pin
int turn = 0;
int input1 = LOW;
int input2 = LOW;
int input3 = LOW;
//int input4 = LOW;
int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// pinMode(led4, OUTPUT);
randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
for (int y=0; y<=99; y++){ //For statement to loop through the output and input functions
output();
input();
}
}
void output() { //function for generating the array to be matched by the player
for (int y=turn; y <= turn; y++){ //Limited by the turn variable
Serial.println(""); //Some serial output to follow along
Serial.print("Turn: ");
Serial.print(y);
Serial.println("");
randomArray[y] = random(1, 4); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
for (int x=0; x <= turn; x++){
Serial.print(randomArray[x]);
if (randomArray[x] == 1) { //if statements to display the stored values in the array
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(100);
}
if (randomArray[x] == 2) {
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(100);
}
if (randomArray[x] == 3) {
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
delay(100);
}
// if (randomArray[x] == 4) {
// digitalWrite(led4, HIGH);
// delay(500);
// digitalWrite(led4, LOW);
// delay(100);
// }
}
}
}
void input() { //Function for allowing user input and checking input against the generated array
char capval[6];
char pinval[6] = {
1<<PINB0,1<<PIN1,1<<PIN2};
//char pinval[6] = {1<<PINB0,1<<PINB1,1<<PINB2,1<<PINB3,1<<PINB4,1<<PINB5};
delay(100);
for(char i = 0; i < 1; i++)
{
capval[i] = getcap(pinval[i]);
for (int x=0; x <= turn;){ //Statement controlled by turn count
input1 = (getcap(pinval[0]));
input2 = (getcap(pinval[1]));
input3 = (getcap(pinval[2]));
// input4 = digitalRead(switch4);
if (getcap(pinval[0]) > 1)
{
digitalWrite(led1, HIGH); // sets the LED on
delay(200);
digitalWrite(led1, LOW); // sets the LED off
inputArray[x] = 1;
delay(50);
Serial.print(" ");
Serial.print(1);
if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
x++;
}
if (getcap(pinval[1]) > 1)
{
digitalWrite(led2, HIGH); // sets the LED on
delay(200);
digitalWrite(led2, LOW); // sets the LED off
inputArray[x] = 2;
delay(50);
Serial.print(" ");
Serial.print(2);
if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
x++;
}
if (getcap(pinval[2]) > 1)
{
digitalWrite(led3, HIGH); // sets the LED on
delay(200);
digitalWrite(led3, LOW); // sets the LED off
inputArray[x] = 3;
delay(50);
Serial.print(" ");
Serial.print(3);
if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
x++;
}
}
}
delay(500);
turn++; //Increments the turn count, also the last action before starting the output function over again
}
void fail() { //Function used if the player fails to match the sequence
for (int y=0; y<=5; y++){ //Flashes lights for failure
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
// digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
// digitalWrite(led4, LOW);
delay(200);
}
delay(5000);
turn = -1; //Resets turn value so the game starts over without need for a reset button
}
void loop() { //Unused void loop(), though for some reason it doesn't compile without this /shrug
}
char getcap(char pin)
{
char i = 0;
DDRB &= ~pin; // input
PORTB |= pin; // pullup on
for(i = 0; i < 16; i++)
if( (PINB & pin) ) break;
PORTB &= ~pin; // low level
DDRB |= pin; // discharge
return i;
}