Simple Arduino Stacker Game Using LED's 3x3

Hey guys i just wanted to try making something like this person had did link to the clip

Arduino Stacker game - YouTube
(mine is a trim down version instead of a 4x4 i am doing 3x3 to simplify things more as i am just starting out there)

it’s seem to be like a pretty simple Stacker game using LED’s but i tried several way’s of doing it and had no success with it so far, if anyone would give some advise or tips on how to do it properly that would be grate thanks in advance appreciated your time in reading it :smiley:

some of the code’s i have tired

TEST01 (didn’t when the way i wanted as i can’t seem to make it stop if i did break the loop it wont continue which is one of the problems i run into and sorry if its a mess)

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
int button = 2;
boolean old_val = LOW;
int Lightposition1 = 0;
int pause1 = 200;
int pause2 = 100;
int pause3 = 50;
long lastmove = millis();
int stop = HIGH;
int a = 0;
int buttonState = 0;
void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(button, INPUT);
  Serial.begin(9600);  
}

void move(int Lightposition1)
{
  for (int x = led1; x <= led6;x++)
  {
    digitalWrite(x, LOW);
  }
  digitalWrite(Lightposition1 + 1, HIGH);

}

void loop()
{
loopy1(); 
     if(millis()- lastmove  > pause1)
  {
    lastmove = millis();
    Lightposition1++;
    if(Lightposition1 >= 8) Lightposition1 = 2;
    move(Lightposition1);
  }
}

void loopy1() {
   if (digitalRead(button) == HIGH && digitalRead(led2) == HIGH && old_val == LOW)
  {
     // first press. Wait here until they release
      while   (digitalRead(button)  == HIGH);
      // they've released. Wait until they press again
      while   (digitalRead(button)  == LOW);
  digitalWrite(led2, HIGH);
  }
}

Why don't you just use 4x4 since supposedly his code works for such a thing?

The first issue I see is that you have a bit of extraneous code here:

void move(int Lightposition1)
{
  for (int x = led1; x <= led6;x++)
  {
    digitalWrite(x, LOW);
  }
  digitalWrite(Lightposition1 + 1, HIGH);

}

In the for loop you go through every possible LED and you set it to LOW. There is only one LED that should be on at a time, so there is no need to set all of the LEDs to low, but instead you should just set the LED that is on to LOW.

Shouldn’t there be 9 LEDs since it is a 3x3?

The rest of your code confuses me since inside loopy1 you check to see if old_val is equal to LOW, and you check to see if LED 2 is HIGH, but there is no where in the code where either of these values could change. I’m also confused why there are pauses; shouldn’t the game keep running until the until the user guesses?

The way your code should work is something like this:

LED1 = 3;
...
LED9 = 11;
int lightPosition = 0;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int button = 12;
int startPosition = LED1;
int endPosition = LED3;

void move(int lightPosition1)
{
  digitalWrite(lightPosition1, LOW);
  
  if ((endPosition - lightPosition) < 0) {
    lightPosition = startPostion;
  }
  else {    
    lightPosition++;
  }
  digitalWrite(lightPosition, HIGH);
}

void loop() {

while (digitalRead(button) == LOW){
  move(lightPosition);
}

if (pos1 = 0) {
  pos1 = lightPosition;
  startPosition = LED4;
  endPosition = LED6;
}
else if (pos2 = 0) {
  pos2 = lightPosition;
  if ((pos1 + 3) != pos2) {
    Serial.println(F("Sorry you lost"));
    for (int i = LED1; i <=LED6; i++) {
       digitalWrite(i,LOW);
    }
    pos1 = 0;
    pos2 = 0;
  }
  else {
    startPosition = LED7;
    endPostion = LED9;
}
else {
  pos3 = lightPosition;
  if (((pos1 + 6) != pos3) || ((pos2 + 3) != pos3)) {
    Serial.println(F("Sorry you lost"));
    for (int i = LED1; i < LED9; i++) {
      digitalWrite(i,LOW);
    }
    pos1 = 0;
    pos2 = 0;
    pos3 = 0;
  }
  else {
    Serial.println(F("CONGRATS, YOU WON!"));
    delay(1000);
    for (int i = LED1; i < LED9; i++) {
      digitalWrite(i,LOW);
    }
    startPosition = LED1;
    endPosition = LED3;    
 }
}

The idea of this is that it will keep moving the LEDS until the user presses the button down. When that happens it look to see if pos1 is equal to 0. If this is true, that means that the LED in the first row still hasn’t been chosen yet. It’ll run through the code inside that if statement, then keep lighting the LEDs in the second row. After the LED in that row is chosen it’ll check to see if it is in the same column as the LED in the first row. It does the same for the third.

If you have any other questions on how this code works let me know. Also keep in mind this code might need a little changing since I just wrote this up without actually seeing everything or knowing all of your requirments.

ieee488: Why don't you just use 4x4 since supposedly his code works for such a thing?

due to hardware constraint i am only working on a 3x3 instead, the code wasn't provided by him hahaha i working on the code totally fresh from the start :)

Thanks for your reply and your time in reading the code that i had put together, i understood what are you talking about on the first paragraph, intentionally i am only inputting 6LEDs first to make sure that it does work before moving on the 9LEDs.

I had read through your code man i could understand it so much better :) i am gonna use your code as a understanding check afterwards i am just gonna start all over again from zero by doing that i believe so that i would be able to get more out of it :D

Status Update (Working on a 1x3 currently then moving on to 3x3)

  • The Program seem to be able to compile and run on the hardware
    (Hardware using an Arduino Uno)
  • All the LED’s seem to be lighting up but it didn’t seem to be moving (Link to image http://imgur.com/a/mkWpZ)

Thanks in advance for any tips or advise :slight_smile:

(Provided by "michaelsm" TYTY :) )
// The Stacker 3x3 grid 
// P Meaning = Position
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
int led9 = 11;
int p1 = 0;
int p2 = 0; 
int p3 = 0; 
int button = 2;
int lightP = 0;
int startP = led1; 
int endP = led3;

void setup() {
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(button, INPUT); 
}

void move(int lightP1)
{
 digitalWrite(lightP1, LOW);
 
 if ((endP - lightP) < 0) {
   lightP = startP;
 }
 else {    
   lightP++;
 }
 digitalWrite(lightP, HIGH);
}

void loop() {

while (digitalRead(button) == LOW){
 move(lightP);
}

if (p1 = 0) {
 p1 = lightP;
 startP = led4;
 endP = led6;
}
else if (p2 = 0) {
 p2 = lightP;
 if ((p1 + 3) != p2) {
   Serial.println(F("Sorry you lost"));
   for (int i = led1; i <=led6; i++) {
      digitalWrite(i,LOW);
   }
   p1 = 0;
   p2 = 0;
 }
 else {
   startP= led7;
   endP = led9;
}
 } 
 else {
 p3 = lightP;
 if (((p1 + 6) != p3) || ((p2 + 3) != p3)) {
   Serial.println(F("Sorry you lost"));
   for (int i = led1; i < led9; i++) {
     digitalWrite(i,LOW);
   }
   p1 = 0;
   p2 = 0;
   p3 = 0;
 }
 else {
   Serial.println(F("CONGRATS, YOU WON!"));
   delay(1000);
   for (int i = led1; i < led9; i++) {
     digitalWrite(i,LOW);
   }
   startP = led1;
   endP = led3;    
}
}
}
}