WS2812 LED Connect 4 Game, Please Help!!

Hello,

i have been trying to use 36 (6x6) WS2812 LEDs, connected in series, and running off of one digital pin, in order to create a connect 4 game. My plan was to implement buttons to move the counter (LED) around the board and store the location etc, in order to create a working game of connect 4, that could also be used as a music sequencer, however i have been struggling to get it off the ground and cannot figure out how to implement this idea, my fear is that because it is running off one digital pin, i will not be able to use a meaningful array in order to move the counter around etc.

If anyone has any guidance for me at all i would greatly appreciate it,

Thanks Again, Andrew :) :) :)

Running off one pin is no hindrance.

'buttons to move a counter' makes no sense to me. Please explain it better.

The term 'meaningful array' is meaningless. The array gets meaningfulness when you make it in such a way that it has meaning to you. So, what exactly do you imagine a meaningful array to look like? You have not shown any code whatsoever.

I have shown no code because I do not have anything that works properly yet, so i tend to just start new with each day.

i'm very inexperienced with arduino, and code in general as i am an audio technology student who has only been introduced to this aspect of engineering recently, sorry if i am confusing you, the buttons will ideally be up, left, down, right, and confirm (to store LED position, and move onto player 2).

its hard to explain but if anyone could at least point me in the right direction i would greatly appreciate it

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Show us some code and tell us what you want it to do and what it is doing. We will help you develop code but not write it for you from scratch.

What library are you using and have you tried the examples that come with the library?

Can you get individual LEDs to light up in sequence?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks... Tom.. :)

Sorry, I can't point you in the right direction to explain yourself better. Use your words. Maybe the first step is to not hide behind 'its hard to explain'.

Would you be happy if I told you that I know how to make your project, but 'it's hard to explain'?

Hello,

i have been trying to use 36 (6x6) WS2812 LEDs, connected in series, and running off of one digital pin, in order to create a connect 4 game. My plan was to implement buttons to move the counter (LED) around the board and store the location etc, in order to create a working game of connect 4, that could also be used as a music sequencer, however i have been struggling to get it off the ground and cannot figure out how to implement this idea, my fear is that because it is running off one digital pin, i will not be able to use a meaningful array in order to move the counter around etc.

If anyone has any guidance for me at all i would greatly appreciate it,

Thanks Again, Andrew :slight_smile: :slight_smile: :slight_smile:



#include "FastLED.h"

#define NUM_LEDS 36

#define LED_PIN 3

int  buttonPin = 8;  

int  buttonState = 0;

CRGB leds[NUM_LEDS];

void setup() {
  // put your setup code here, to run once:
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

pinMode(buttonPin,  INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
buttonState  = digitalRead(buttonPin); 

if  (buttonState  ==  HIGH) {

leds[0] = CRGB(255,0,0);
FastLED.show();
}
else  {
  leds[0] = CRGB(0,0,0);
FastLED.show();
}
}

I have included some very basic code that i have been using as a starting point, which just turns the first LED in the circuit on when a button is pressed, I have very little experience with programming, Arduino, and as far as hardware goes, only sound systems, and audio equipment.

Again what I am really struggling with is creating a means of changing the LEDs position through the use of a button, i have done it with simple LEDs running off of individual pins, as I can just use a for loop to move from one pin to another, but i cannot figure out how to achieve this when all of my LEDs are running from one pin. so what i'm looking for is some advice on how to achieve that

One issue. Connect 4 has a 7x6 play grid, not a 6x6. Can you add 6 more LEDs to make the 7th column?

Another issue: Your code is difficult to read. When you are in the IDE, hit CTRL-T to autoformat. And get rid of unnecessary white space. I copied in your code, made these 2 changes, and now it is much easier to read.

#include "FastLED.h"
#define NUM_LEDS 36
#define LED_PIN 3
int  buttonPin = 8;
int  buttonState = 0;
CRGB leds[NUM_LEDS];

void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode(buttonPin,  INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState  = digitalRead(buttonPin);
  if  (buttonState  ==  HIGH) {
    leds[0] = CRGB(255, 0, 0);
    FastLED.show();
  }
  else  {
    leds[0] = CRGB(0, 0, 0);
    FastLED.show();
  }
}

How do you want the buttons to behave?
One button to move where you want to drop the game piece one column to the right (wrap around if they push the move button from the right most column).
Another button to drop the game piece.

Or do you want 2 move buttons, one for right and one for left.

Oh, how are you arranging your LEDs? Something like this (with or without the 7th column)

 5 11 17 23 29 35 41
 4 10 16 22 28 34 40
 3  9 15 21 27 33 39
 2  8 14 20 26 32 38
 1  7 13 19 25 31 37
 0  6 12 18 24 30 36

If so, your code turns on and off the lower left LED, right? (or the zero pixel wherever that is with your arrangement)

Thanks for your reply vince :)

the LEDs are set up like so:

35 34 33 32 31 30 24 25 26 27 28 29 23 22 21 20 19 18 12 13 14 15 16 17 11 10 9 8 7 6 0 1 2 3 4 5

With the code that i uploaded turning the lowest and leftmost LED on and off .

I have removed one column for the game as the sequencer element will have control parameters placed in these 6 empty slots, and the aim is to create an interactive music tool, so this is one of the sacrifices I had to make.

I would ideally like to have 4 buttons to move the LED up, left, right, and down, and one button to confirm the players choice, and switch to player 2. in this way it is fairly different from original connect 4 again as the counters do not all drop to the bottom, and can be placed anywhere regardless of the fact that there are no counters underneath them in a column, I would prefer this as it means that when the sequencer element of it is in use, it can be utilised a lot more effectively.

Thanks again for your reply, Andrew :)

If it was arranged as above in vince's post, then your directions would be something like:

have a variable along the lines of 'currentPosition' which will be the address of the LED for your function leds[currentPosition] = CRGB::White or whatever.

if up button is pressed, currentPosition gets +1 if down button is pressed, currentPosition gets -1 if right button is pressed, currentPosition gets +6 if left button is pressed, currentPosition gets -6

then you can decide if you want wraparound or just boundaries, so something like if left button is pushed and currentPosition = 3, do nothing

So, take your time, try a little, and logic it out.

The dropping action would simplify user controls as the game would only require right and left buttons. Your 'counter' as you call it, for whatever reason you call it that, could just sit on the top row. Coding wouldn't be too hard, just more if statements. Maybe make arrays for each column, and just check if each position from the lowest has been changed to whatever color you're using as 'filled' and if so, change the next element in the array.

There are a few things you need to know about your index into the array.
First you need to know your normalized position in a ROW:
normpos = (ndx - 6) % 6 // where ndx is current LED index
That gives you 0 through 5 no matter what ROW you are on.
You need to know which ROW you are on:
row = ndx / 6
So you can tell if you are on an even or odd numbered ROW:
rank = row & 1

If you want to go left:
next = rank ? ndx + 1 : ndx - 1
If you want to go right:
next = rank ? ndx - 1 : ndx + 1
If you want to go up:
next = 11 - (normpos * 2) + ndx // EDITED 4/11
If you want to go down:
next = ndx - ((normpos * 2) + 1) // EDITED 4/11

You need to specify your desired actions for moving away from the edges.
TESTED

OK, a modified connect 4 it is.

Do you have 5 buttons? Have you connected them up and confirmed that you can read each one?

Do that as a separate sketch with no LED work in it at all. Just write to the serial monitor when each button is pressed. Up. Down. Left. Right. Place.

Once you have confirmed that our buttons are wired up and you can read them, start working on your game algorithm.

Something to think about. Your current simple sketch to turns on and off a single pixel with the single button. But it does so by looking at the current state of the button. The entire time your button is pressed, the pixel is repeatedly set on, and the entire time the button is pressed, the pixel is repeatedly set off. I think you will need to change your logic to something that reacts 'when a button becomes pressed' instead of reacting the entire time a button is pressed.

But beginning ideas for you:

Make a row and column variable. Set them both to zero. When UP becomes pressed, row++ When DOWN becomes pressed, row-- When LEFT becomes pressed, column-- When Right becomes pressed, column++

On all of those, only if that would not put row or column outside of your 0-5 bounds.

Then turn on the pixel defined by row * 6 + column

This should let you draw on your board.