Go Down

Topic: Custom Simon Game (Read 1 time) previous topic - next topic

whattheshock

Aug 15, 2011, 05:44 am Last Edit: Aug 15, 2011, 05:52 am by whattheshock Reason: 1
Hey guys, new to arduino and electronics in general so im having a bit of trouble wrapping my head around this one.
I want to make a Simon game using my arduino and 3 LEDS and three buttons, but I dont want the LED's to light up in a random sequence as the game progresses. I want it to be a predetermined light up sequence.

Could any one help with a simple version of what I'm asking and then hopefully I will be able to cut and past code to create my own sequence?

Thanks!

robtillaart

Made a Simon game for the ArduCap shield some time ago, which could serve as a base. You need to strip/modify  all ArduCap-shields specifics and rewrite the readKey() but it could be a basis.  See - http://arduino.cc/forum/index.php/topic,54242.0.html -

hope this helps,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

whattheshock

Oh that's unbelievably perfect.The next stage of my project was to have capacitor sensors replace the buttons. But this ArduCap - shield stuff confuses me. I was hoping to have the input be stripped wires connected to tin foil as I have found in this Playground example.

http://www.arduino.cc/playground/Code/CapacitiveSensor

The code at the bottom is the one that works on my Arduino UNO. Depending on if I'm touching the wire, the serial monitor displays 1 (not touching) or 4-5 (touching).

I'm not entirely sure how to add these two codes together.

How do I tell it to read 1-2 as LOW and 3-5 as HIGH. as in button pushed / button not pushed?

This is great. I love this forum.
Thanks again!

whattheshock

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

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

}

whattheshock

ok but where do i put that?
should i be declaring them? or are they part of the void output?

and also what does "= 0x8F;" parts mean?

could you please give me an example of lighting in this sequence :
led1 -> delay(1000) -> led3 -> delay(1000) -> led2.

whattheshock

thank you for that. I have been told before to be more direct with my language when trying to program computers.

I'm trying to make a simon game that has a set sequence of levels. by this I mean I would like level one of the simon game to be (and to always be):

levelOne {
digitalWrite (led1, HIGH);
delay(1000);
digitalWrite (led3, HIGH);
delay(1000);
digitalWrite (led2, HIGH);
}

//and then level two to be:

levelTwo {
digitalWrite (led1, HIGH);
delay(1000);
digitalWrite (led1, HIGH);
delay(1000);
digitalWrite (led3, HIGH);
}


Would it be a matter of replacing:

int randomArray[100];

//replace with

int levelOne;
int levelTwo;
etc... etc.   ?

how do i tell it to always play in sequence of level one then level two?
and how do i tell it that level one is always "this sequence" and level two is always " this different sequence"

thanks again.

whattheshock

ok so I've got it doing predetermined sequences. but it's only...umm...how do i say this... the progression is still plus an extra move per level. so each turn it adds a extra move. so pretty close to what I want. but not quite.

Code: [Select]
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[0] = (1);
    randomArray[1] = (3);
    randomArray[2] = (2);
    randomArray[3] = (2);


this means the game will ALWAYS progress in that sequence of led1, led3, led2, led2. (as the output and "expected" input)

HOWEVER what i would like it to do is get randomArray[0] to be

Code: [Select]

randomArray[0] =

digitalWrite (led1, HIGH);
delay(500);
digitalWrite (led1, LOW);
delay(1000);
digitalWrite (led3, HIGH);
delay(500);
digitalWrite(led3,LOW);
delay(1000);
digitalWrite (led2, HIGH);
delay(500);
digitalWrite (led2, LOW);
delay(1000);
digitalWrite (led2, HIGH);
delay(500);
digitalWrite(led2,LOW);
digitalWrite (led2, HIGH);


i know that the code i wrote would not work, but hopefully gives an idea of what im trying to achieve.
thanks again for your patience

Go Up