Adding buttons to my Simon Says game & general questions.

So, I'm making this project as a school project, and I've ran into trouble.
I'm almost done with my coding part, but I am not sure how to move onwards from here.

I am well aware that I have to replace the delay in my loop with the button-sequence, but how I have no idea.
Also, it's a problem that everytime I reset my Arduino, it generates the same sequence of LED's, making it "non-random", since every game is the same then.

Here's my MMI:
http://i.imgur.com/UGRJLRp.jpg (picture too big to be linked directly)

And here's my code:

int led_1 = 13; //The five LED-pins
int led_2 = 12;
int led_3 = 11;
int led_4 = 10;
int led_5 = 9;

int button_1 = 8;//The four button input pins
int button_2 = 7;
int button_3 = 6;
int button_4 = 5;
int button_5 = 4;

int niveau = 0;
int ledList[32];
void setup()
{
  pinMode(led_1,OUTPUT);//Initializing LED-pins as output
  pinMode(led_2,OUTPUT);
  pinMode(led_3,OUTPUT);
  pinMode(led_4,OUTPUT);
  pinMode(led_5,OUTPUT);

  pinMode(button_1,INPUT);//Initializing buttons as input
  digitalWrite(button_1,HIGH);//Enabling the internal Arduino pullup-resistor
  pinMode(button_2,INPUT);
  digitalWrite(button_2,HIGH);
  pinMode(button_3,INPUT);
  digitalWrite(button_3,HIGH);
  pinMode(button_4,INPUT);
  digitalWrite(button_4,HIGH);
  pinMode(button_5,INPUT);
  digitalWrite(button_5,HIGH);
}

void ledSequence()
{ 
  int i;
  int pin;

  for(i=0; i<=niveau; i++)
  {
    switch(ledList[i])
    {
    case 1: 
      pin = led_1; 
      break;
    case 2: 
      pin = led_2; 
      break;
    case 3: 
      pin = led_3; 
      break;
    case 4: 
      pin = led_4; 
      break;
    case 5: 
      pin = led_5; 
      break;
    }

    digitalWrite(pin,HIGH);
    delay(1000);
    digitalWrite(pin,LOW);
    delay(1000);
  }
}

void loop()
{   
  ledList[niveau] = random(1 , 5);
  ledSequence();
  delay(2000);
  niveau++;
}

The random() command is not a really random generator.
http://arduino.cc/en/Reference/Random

You can set the "seed" using RandomSeed() command (in setup), read here:
http://arduino.cc/en/Reference/RandomSeed

I am well aware that I have to replace the delay in my loop with the button-sequence, but how I have no idea.

You will first need to debounce the buttons, then you need to cycle through them and compare them to the lit LED. A simple FOR should work. Your buttons are 5 pins lower to your LEDs, so you can start on pin 4 (button) and see if the corresponding LED (+5) is lit. If the LED is lit add the next pattern, if it's not then player has lost.

Also, it's a problem that everytime I reset my Arduino, it generates the same sequence of LED's, making it "non-random", since every game is the same then.

The random function as you found out, is not random. So what you can try is use a seed that takes the quotient of analog inputs A0 and A1, because one analog pin alone is still not enough.

I realise that this is not answering your questions but all of this is unneccesary

    switch(ledList[i])
    {
    case 1: 
      pin = led_1; 
      break;
    case 2: 
      pin = led_2; 
      break;
    case 3: 
      pin = led_3; 
      break;
    case 4: 
      pin = led_4; 
      break;
    case 5: 
      pin = led_5; 
      break;
    }

Put the LED pin numbers in an array and use ledList[i]as the index to it.

UKHeliBob:
I realise that this is not answering your questions but all of this is unneccesary

    switch(ledList[i])

{
    case 1:
      pin = led_1;
      break;
    case 2:
      pin = led_2;
      break;
    case 3:
      pin = led_3;
      break;
    case 4:
      pin = led_4;
      break;
    case 5:
      pin = led_5;
      break;
    }



Put the LED pin numbers in an array and use `ledList[i]`as the index to it.

Yeah, I know of this aswell, that's how I used to do it, but chose to do the testing (if the button input is corresponding to the LED, so as soon as you do a wrong buttonpress, you will get game over'ed.

nid69ita:
The random() command is not a really random generator.
random() - Arduino Reference

You can set the "seed" using RandomSeed() command (in setup), read here:
http://arduino.cc/en/Reference/RandomSeed

That's what it's called, thanks! Couldn't remember. Let me get back to you and see if it works!

HazardsMind:

I am well aware that I have to replace the delay in my loop with the button-sequence, but how I have no idea.

You will first need to debounce the buttons, then you need to cycle through them and compare them to the lit LED. A simple FOR should work. Your buttons are 5 pins lower to your LEDs, so you can start on pin 4 (button) and see if the corresponding LED (+5) is lit. If the LED is lit add the next pattern, if it's not then player has lost.

Also, it's a problem that everytime I reset my Arduino, it generates the same sequence of LED's, making it "non-random", since every game is the same then.

The random function as you found out, is not random. So what you can try is use a seed that takes the quotient of analog inputs A0 and A1, because one analog pin alone is still not enough.

Could you please elaborate on the button part?
I'm very new at programming with little-to-no knowledge. I know the basics, but I'm not a good programmer.

The buttons in your picture are called tactile buttons. When pressed, they don't produce a perfect signal, they bounce first then give a stable signal. That bounce is no good to have because your code will do its action when the state of the button changes. So in order to take out that bounce, you need to read the button, let some time pass (milliseconds, 50 is often used) then read the button again and that second reading is the one that's used. This seems complicated but don't worry, you have an example sketch you can learn from under File -> Examples -> digital -> Debounce.

HazardsMind:
The buttons in your picture are called tactile buttons. When pressed, they don't produce a perfect signal, they bounce first then give a stable signal. That bounce is no good to have because your code will do its action when the state of the button changes. So in order to take out that bounce, you need to read the button, let some time pass (milliseconds, 50 is often used) then read the button again and that second reading is the one that's used. This seems complicated but don't worry, you have an example sketch you can learn from under File -> Examples -> digital -> Debounce.

Is it OK if all of this is bypassed? I mean, will it work as intended, even though I haven't debounced them?

It might work, but your really better off debouncing them. This way if you game doesn't work, you'll know it's not the buttons causing it, but another issue. Why leave gaps if you don't want to fall in later?

HazardsMind:
It might work, but your really better off debouncing them. This way if you game doesn't work, you'll know it's not the buttons causing it, but another issue. Why leave gaps if you don't want to fall in later?

You're correct.
I'm just very time limited, I have to hand over this assignment in about 10 hours. That is fairly long time, but I still have no idea how to do the buttons, other than I have to debounce them.
Can you be a little more specific, or just link to the reference page required, perhaps?

Thankfully I keep all my past sketches.

/*
  This code is set for buttons to be normally LOW by default. 
  To change from normally LOW to HIGH, use "if (buttonState[count] == LOW && buttonState[count] != lastButtonState[count]) "
*/

byte LEDpin[2] = {
  4,5}; //on-board LED
byte ButtonPin[2] = {
  2,3}; //digital pin 2

int buttonState[2];
int last[2];
int lastButtonState[2];

long lastDebounceTime[2];  
long debounceDelay = 50;

void setup() {
  for(int count = 0; count < 2; count++) {
    pinMode(LEDpin[count], OUTPUT);
    pinMode(ButtonPin[count], INPUT);
  }
}

void loop() {
  for(int count = 0; count < 2; count++) // loop through all the buttons
  {
    buttonState[count] = digitalRead(ButtonPin[count]); // read button states

    if ( buttonState[count] && buttonState[count] != lastButtonState[count]) // check to see if the state has changed from last press
    {
      lastDebounceTime[count] = millis(); // record the time of the last press
      lastButtonState[count] = buttonState[count]; // update last press state
    } 

    if ((millis() - lastDebounceTime[count]) > debounceDelay)  // check to see if the desired time has passed
    {
      digitalWrite(LEDpin[count], buttonState[count]); // output results
    }
  }
}

HazardsMind:
Thankfully I keep all my past sketches.

/*

This code is set for buttons to be normally LOW by default.
  To change from normally LOW to HIGH, use "if (buttonState[count] == LOW && buttonState[count] != lastButtonState[count]) "
*/

byte LEDpin[2] = {
  4,5}; //on-board LED
byte ButtonPin[2] = {
  2,3}; //digital pin 2

int buttonState[2];
int last[2];
int lastButtonState[2];

long lastDebounceTime[2]; 
long debounceDelay = 50;

void setup() {
  for(int count = 0; count < 2; count++) {
    pinMode(LEDpin[count], OUTPUT);
    pinMode(ButtonPin[count], INPUT);
  }
}

void loop() {
  for(int count = 0; count < 2; count++) // loop through all the buttons
  {
    buttonState[count] = digitalRead(ButtonPin[count]); // read button states

if ( buttonState[count] && buttonState[count] != lastButtonState[count]) // check to see if the state has changed from last press
    {
      lastDebounceTime[count] = millis(); // record the time of the last press
      lastButtonState[count] = buttonState[count]; // update last press state
    }

if ((millis() - lastDebounceTime[count]) > debounceDelay)  // check to see if the desired time has passed
    {
      digitalWrite(LEDpin[count], buttonState[count]); // output results
    }
  }
}

I have a hard time understanding all this, and feel like I should focus on getting the game functional, before fine-tuning it, if you feel me?
I just want it to be functioning, and afterwards I can perhaps debounce the buttons, if this makes the game more "viable" - if you can call it that.
Afterwards I can always add a point system if I want.
Do you know how I can do this, with the code provided as code?

Well, you still have 8 hours to learn. I already told you what you had to do after you debounced the buttons, which you could hold of on, but it may give faulty results if you do.

. . . then you need to cycle through them and compare them to the lit LED. A simple FOR should work. Your buttons are 5 pins lower to your LEDs, so you can start on pin 4 (button) and see if the corresponding LED (+5) is lit. If the LED is lit add the next pattern, if it's not then player has lost.

In this case, you need to cycle through your pattern and see if the correct buttons were pressed. If they were, then add another LED or sequence to your pattern, and if not, then player has lost.

HazardsMind:
Well, you still have 8 hours to learn. I already told you what you had to do after you debounced the buttons, which you could hold of on, but it may give faulty results if you do.

. . . then you need to cycle through them and compare them to the lit LED. A simple FOR should work. Your buttons are 5 pins lower to your LEDs, so you can start on pin 4 (button) and see if the corresponding LED (+5) is lit. If the LED is lit add the next pattern, if it's not then player has lost.

In this case, you need to cycle through your pattern and see if the correct buttons were pressed. If they were, then add another LED or sequence to your pattern, and if not, then player has lost.

You explain it like it's very simple. I've been sitting trying to figure all this out for the past hour, and have gotten nowhere. I simply do not know how to begin.

Forget about debouncing for now. Here is some pseudo code to get you going on reading the buttons. It assumes that you have an array of LED numbers that have been displayed and know how many there are

set currentLed to zero
for (currentLed = 0; currentLed <= totalLedsDisplayed; currentLed++) //cycle through all LEDS displayed 
{
  wait for a button press

    if (buttonPressed != array[currentLed])
  {
    set flag to indicate failure
      break;         //causes premature exit from for loop
  }
  currentLed++
}
if failed flag is false go back, add another LED to the list and display the LEDs
else take failed actions and start again

UKHeliBob:
Forget about debouncing for now. Here is some pseudo code to get you going on reading the buttons. It assumes that you have an array of LED numbers that have been displayed and know how many there are

set currentLed to zero

for (currentLed = 0; currentLed <= totalLedsDisplayed; currentLed++) //cycle through all LEDS displayed
{
  wait for a button press

if (buttonPressed != array[currentLed])
  {
    set flag to indicate failure
      break;         //causes premature exit from for loop
  }
  currentLed++
}
if failed flag is false go back, add another LED to the list and display the LEDs
else take failed actions and start again

Hey mate, thanks!
I've gotten to this so far:

void buttonList()
{
  for(int currentLed=0; currentLed <= niveau; currentLed++)
  {
    while(digitalRead(pin)==HIGH);
    {
    }
  
  if (buttonPressed != ledList[currentLed])
    {
       digitalWrite(led_3,HIGH);
       delay(500);
       digitalWrite(led_2,HIGH);
       digitalWrite(led_4,HIGH);
       delay(500);
       digitalWrite(led_1,HIGH);
       digitalWrite(led_5,HIGH);
       delay(3000);
       digitalWrite(led_1,LOW);
       digitalWrite(led_2,LOW);
       digitalWrite(led_3,LOW);
       digitalWrite(led_4,LOW);
       digitalWrite(led_5,LOW);
       
     break; //causes exit from the loop
    }
  currentLed++
  }
}

Now of course it doesn't work, mainly because of the "pin" & "buttonPressed" being undeclared. How do I fix this?

You need to read each button pin in turn to see whether any are pressed and keep doing it until you find one. At that point you will have the value of buttonPin which you can use to derive the number of the actual button pressed to compare with the current value in the array.

UKHeliBob:
You need to read each button pin in turn to see whether any are pressed and keep doing it until you find one. At that point you will have the value of buttonPin which you can use to derive the number of the actual button pressed to compare with the current value in the array.

I'm sorry to bother you further, but this is really hard to understand for me.
Also, does the code look usable?

The code looks close to what you want but you don't need the currentLed++ near the end because the LED number is already being incremented by the for loop.

As to the button testing.
Set a buttonPressed variable to zero.
Test the first one. If it is pressed then save its number in buttonPressed
Test the second one. If it is pressed then save its number in buttonPressed
etc, etc

If after testing each button buttonPressed is not zero then one was pressed.

You can do this neatly with an array of button numbers and a for loop but if you have to do it the long winded way then do it like that.