My Arduino Game

I am working on a game on my Arduino UNO and have no idea of how to program it at all. I have attached a photo of what the setup looks like on my breadboard. What I ultimately want is for it to be like a game of Pong. I want either LED 2 or LED 3 to turn on. By either, I mean it chooses a random one to set HIGH. Then it turns that one off and moves over to LEDs 4 and 5 and turns on a random one of those on. Then over to LEDs 6 and 7 and lights a random one of those. Once at the end, I want one of the yellow LEDs to be lit. However, lets say that the code gets to LEDs 6 and 7 and that LED6 was chosen by the random function. I want LED 8 to be lit before the program continues. I would like LEDs 0 and 8 to constantly be lit unless the button above it turns it off and turns the other Yellow LED on. So if LED 6 is lit and LED 8 is not, I want the speaker to give like a “YOU LOSE” type of tone. So LEDs 0 and 8 will like be the default LEDs always lit unless the button above turns it off. I want Button 1 to control LEDs 0 and 1, and Button 2 to control LEDs 8 and 9. In visa versa, if LED7 was chosen by the random function, then a button press to turn on LED 9 would be required for the code to continue. So back to the earlier part about once it gets to the end, if the correct LED was lit then it starts moving the other way and does the same thing once it gets to that end. I want the game to get increasingly harder via the time interval between lighting LEDs getting lower as to make it harder to make the right Button press in time.
However, I do not want someone to just give me the code I need to download, unless they give a very detailed explanation of what each part of the code is doing and why it’s necessary. I would preferably like programming ideas and how those ideas should work. A guidance is the word I am looking for.

Thanks.

What I would do is have separate parts for each thing one part just increasing and decreasing a count at an adjustable time one part to randomly choose which side led will be lit when the count is in that spot one part to check the count and when it is on either end wait a specified time for a button press before the game ends, if the button is pressed it allows it to continue counting

You could have two states, continue and end While its in continue the first two parts can loop indefinetly, maybe add a changing time interval to speed things up the third part would have it keep going or go into end, in which the first two stop and it does the ending perhaps something like a simple tone call to a speaker that sounds like losing and light up or flash the leds to indicate the losing sides

Also the timing interval would have to be done with a sort of timer, any delays called will affect the button reads

Thank you for the tips winner! I have been working on some code. I have been able to get the Yellow LEDs to work properly with the button presses. I was also able to make it to where it randomly turns on either LED2 or LED3. The problem arises when I want the code to move on to LED4 and LED5. If I use a delay, it will interfere with my button presses. Is there a way I can make a delay without actually using the delay() function? Here is what I have so far.

int LED0 = 2;
int LED1 = 3;
int BUTTON1 = 0;
int LED8 = 11;
int LED9 = 12;
int BUTTON2 = 1;
const int LED2 = 4;
const int LED3 = 5;
int PIEZO = 6;
const int LED4 = 7;
const int LED5 = 8;
const int LED6 = 9;
const int LED7 = 10;
const int MAX = 6;
int LEDs[MAX] = {LED2, LED3, LED4, LED5, LED6, LED7};
int State = LOW;
long previous = 0;
long interval = 500;

void setup()
{
  for(int i=0; i < MAX; i++)
  {
    pinMode(LEDs[i], OUTPUT);
  }
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(BUTTON2, INPUT);
}

void loop()
{
  if(digitalRead(BUTTON1) == HIGH)
  {
    digitalWrite(LED0, HIGH), digitalWrite(LED1, LOW);
  }
  else
  {
    digitalWrite(LED1, HIGH), digitalWrite(LED0, LOW);
  }
  if(digitalRead(BUTTON2) == HIGH)
  {
    digitalWrite(LED8, HIGH), digitalWrite(LED9, LOW);
  }
  else
  {
    digitalWrite(LED9, HIGH), digitalWrite(LED8, LOW);
  }

  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    if(State == LOW)
    State = HIGH;
    else
    State = LOW;
  
      for(int i=0; i < MAX; i++)
        {
          digitalWrite(LEDs[i], LOW);
        }
          digitalWrite(LEDs[random(2)], State);      
  }
}

Yes, just keep exanding on this:

if(current - previous >= interval)

Right now you are at 500mS intervals.

Shorten it up to 100, then do something different on every 100th:

if (current - previous >= interval) {
previous = current;
Program_state = program_state +1;
if (Program_state ==6) {Program_state = 1;} // reset
switch (Program_state){
Case 1:
//code
break;
Case 2:
// code
break;
:
:
Case 5:
// code
break;
} // end switch
} // end interal check
} // end void loop

Surprisingly, this makes sense to me. Thank you very much!!!

Glad to help.

How does this look so far? It says Program_State was not defined so how would I fix that?

int LED0 = 2;
int LED1 = 3;
int BUTTON1 = 0;
int LED8 = 11;
int LED9 = 12;
int BUTTON2 = 1;
const int LED2 = 4;
const int LED3 = 5;
int PIEZO = 6;
const int LED4 = 7;
const int LED5 = 8;
const int LED6 = 9;
const int LED7 = 10;
const int MAX = 6;
int LEDs[MAX] = {LED2, LED3, LED4, LED5, LED6, LED7};
int State = LOW;
long previous = 0;
long interval = 500;
int program_state = 0;

void setup()
{
  for(int i=0; i < MAX; i++)
  {
    pinMode(LEDs[i], OUTPUT);
  }
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(BUTTON2, INPUT);
}

void loop()
{
  if(digitalRead(BUTTON1) == HIGH)
  {
    digitalWrite(LED0, HIGH), digitalWrite(LED1, LOW);
  }
  else
  {
    digitalWrite(LED1, HIGH), digitalWrite(LED0, LOW);
  }
  if(digitalRead(BUTTON2) == HIGH)
  {
    digitalWrite(LED8, HIGH), digitalWrite(LED9, LOW);
  }
  else
  {
    digitalWrite(LED9, HIGH), digitalWrite(LED8, LOW);
  }

  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Program_State = program_state + 1;
      if(Program_State == 3)
      {
        Program_State = 1;
      }
      switch(Program_State)
      {
        Case 1:
        for(int i=0; i < MAX; i++)
        {
          digitalWrite(LEDs[i], LOW);
        }
          digitalWrite(LEDs[random(2)], HIGH);
          break;
         Case 2:
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(2,4)], HIGH);
           break;
         Case 3:
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(4,6)], HIGH);
          break; 
       
  }
}

Add it to your list of variables at the start

if(Program_State == 3) { Program_State = 1; }

Probably want to reset at 4, so case 3 can be executed.

Program_State and program_state are 2 different variables. C++ is case sensitive.

It is confusing to use the same name for different variables.

This is not working like I would like it to. It is not turning on any of the Red LEDs at all?

int LED0 = 2;
int LED1 = 3;
int BUTTON1 = 0;
int LED8 = 11;
int LED9 = 12;
int BUTTON2 = 1;
const int LED2 = 4;
const int LED3 = 5;
int PIEZO = 8;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 9;
const int LED7 = 10;
const int MAX = 6;
int LEDs[MAX] = {LED2, LED3, LED4, LED5, LED6, LED7};
int State = LOW;
long previous = 0;
long interval = 100;
int Program_State = 0;

void setup()
{
  for(int i=0; i < MAX; i++)
  {
    pinMode(LEDs[i], OUTPUT);
  }
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(BUTTON2, INPUT);
}

void loop()
{
  if(digitalRead(BUTTON1) == HIGH)
  {
    digitalWrite(LED0, HIGH), digitalWrite(LED1, LOW);
  }
  else
  {
    digitalWrite(LED1, HIGH), digitalWrite(LED0, LOW);
  }
  if(digitalRead(BUTTON2) == HIGH)
  {
    digitalWrite(LED8, HIGH), digitalWrite(LED9, LOW);
  }
  else
  {
    digitalWrite(LED9, HIGH), digitalWrite(LED8, LOW);
  }

  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Program_State = Program_State + 1;
      if(Program_State == 4)
      {
        Program_State = 1;
      }
      switch(Program_State)
      {
        Case1:
        if(Program_State = 1)
        {
        for(int i=0; i < MAX; i++)
        {
          digitalWrite(LEDs[i], LOW);
        }
          digitalWrite(LEDs[random(2)], HIGH);
          break;
        }
         Case2:
         if(Program_State = 2)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(2,4)], HIGH);
           break;
         }
         Case3:
         if(Program_State = 3)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(4,6)], HIGH);
          break;
         }    
    }
  }
}

You’ve got assignments in if statements
Use == instead.
Why all the labels?
You’ve almost got switch/case constructs, but case is spelled incorrectly, so your code structure is all wrong.

What are labels? I am just using the code that everyone is giving me. I honestly understand very little about what I am doing and how it works.

Case3 is a label, but with no corresponding goto (which is kind of a good thing)

Until you decide whether you're using if constructs or switch/case, you're not going to make much progress Who told you to separate statements with commas?

The LEDs are working now!!! I didn't fully read one of your posts that said I spelt case wrong. I changed it and they turn on now!

Who told you to separate statements with commas?

What do you mean by this? Also, the LEDs are moving very fast. Is there a way to keep them on for a longer time?

Oh! I know what you mean now about the comma thing. That was my idea and I tried it and it worked. I discovered this about a two weeks ago when I was programming a group of LEDs to display the current temperature using port manipulation. Is it frowned upon?

int temperaturePin = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
float temperature = getVoltage(temperaturePin);
temperature = (temperature -.5) * 100;
int time = 500;

  if (temperature < 20)
  {
    DDRD = B01000000, DDRB = B000101;
    PORTD = B01000000, PORTB = B000101;
    delay(time);
    DDRD = B11011100, DDRB = B111011;
    PORTD = B11011100, PORTB = B111011;
    delay(time);
    DDRD = B11111100, DDRB = B111110;
    PORTD = B11111100, PORTB = B111110;
    delay(time);

Try making your interval longer long interval = 100;

or add a little pause after the last break and before the } that ends the switch:case section.

MathematicsFanatic: Oh! I know what you mean now about the comma thing. That was my idea and I tried it and it worked. I discovered this about a two weeks ago when I was programming a group of LEDs to display the current temperature using port manipulation. Is it frowned upon?

Yes. It's confusing in this context and not usually done. The comma is not exactly a statement separator, and using it is likely to confuse you and other people. Consider this test program:

void setup ()
{
  int a = 5;
  
  Serial.begin (115200);
  Serial.println ("Starting ...");
  if (a == 6)
    Serial.println ("hello"), 
  Serial.println ("world");     
}

void loop () {}

Output:

Starting ...

Now consider this other very slightly different program:

void setup ()
{
  int a = 5;
  
  Serial.begin (115200);
  Serial.println ("Starting ...");
  if (a == 6)
    Serial.println ("hello"); 
  Serial.println ("world");     
}

void loop () {}

Output:

Starting ...
world

Different results, but the difference between the comma and the semicolon is hard to spot.

Now that I have the LEDs blinking correctly, my next question has to do with the random function. How do I make the program know which of the LEDs it chose so it knows that the button press I make is correct? I want to say if LED7 was chosen by the random function, wait a certain amount of time for BUTTON2 to be pressed to turn on LED9. If the BUTTON2 is not pressed in the certain amount of time, make Piezo speaker play a losing tone.