material:
LED matrix 8x8
joystick controller
arudino uno
This is a snake game, however the there are some problems with the joystick where i cannot move right and unresponsive, hope someone can help me to solve this Thanks! Heres the code :
#include <LedControl.h>
// Joystick PINs
#define VRX Â Â A0
#define VRY Â Â A1
#define SW Â Â Â 2
// Display PINs
#define CLK Â Â 8
#define CS Â Â Â 9
#define DIN Â Â 10
#define SIZE Â Â 8
#define ADVANCE_DELAY 50
// we set up the initial value/integer for snake and food
int snake[SIZE*SIZE][2];
int length;
int food[2], v[2];
bool is_game_over = false;
long current_time;
long prev_advance;
int blink_count;
//set up the led control ports
LedControl lc = LedControl(DIN, CLK, CS);
//initial game phrase, setting up the corelation between the food and snake
//Snake increase its size when it eats the food
void init_game() {
prev_advance = current_time = 0;
blink_count = 3;
int half = SIZE / 2;
length = SIZE / 3;
for (int i = 0; i < length; i++) {
  snake[i][0] = half - 1;
  snake[i][1] = half + i;
}
food[0] = half + 1;
food[1] = half - 1;
v[0] = 0;
v[1] = -1;
}
void render() {
for (int i = 0; i < length; i++) {
 lc.setLed(0, snake[i][0], snake[i][1], 1);
}
lc.setLed(0, food[0], food[1], 1);
}
//the x and y axis, and the size of the snake
void clearScreen() {
for (int x = 0; x < SIZE; x++) {
 for (int y = 0; y < SIZE; y++) {
  lc.setLed(0, x, y, 0);
 }
}
}
/*
moves the snake forward
returns true if the game is over
returns false when the game is over
*/
bool advance() {
int head[2] = {snake[0][0] + v[0], snake[0][1] + v[1]};
if (head[0] < 0 || head[0] >= SIZE) {
  return true;
}
if (head[1] < 0 || head[1] >= SIZE) {
  return true;
}
for (int i = 0; i < length; i++) {
  if (snake[i][0] == head[0] && snake[i][1] == head[1]) {
    return true;
  }
}
//snake increase size - grow
bool grow = (head[0] == food[0] && head[1] == food[1]);
if (grow) {
  length++; Â
  randomSeed(current_time);  Â
  food[0] = random(SIZE);
  food[1] = random(SIZE);
}
for (int i = length - 1; i >= 0; i--) {
  snake[i + 1][0] = snake[i][0];
  snake[i + 1][1] = snake[i][1];
}
snake[0][0] += v[0];
snake[0][1] += v[1];
return false;
}
void setup() {
Serial.begin(9600);
pinMode(SW, INPUT_PULLUP);
pinMode(VRX, INPUT);
pinMode(VRY, INPUT);
attachInterrupt(digitalPinToInterrupt(SW), restart, RISING);
lc.shutdown(0, false);
lc.setIntensity(0, 3);
init_game();
render();
}
void loop() {
if (!is_game_over) {
 clearScreen();
 render();
 if (current_time - prev_advance > ADVANCE_DELAY) {
  is_game_over = advance();
  prev_advance = current_time; Â
 }
} else {
 while (blink_count > 0) {
  clearScreen();
  delay(300);
  render();
  delay(300);
  blink_count--;   Â
 }
}
readControls();
current_time++;
}
//false = game is over = GG
void restart() { Â
init_game();
is_game_over = false;
}
//setting the controller control
void readControls() {
int dx = map(analogRead(VRX), 0, 1023, 0, 5);
int dy = map(analogRead(VRY), 0, 1023, 5, 0);
if (dx != 0) {dx = dx / abs(dx); Â }
if (dy != 0) {dy = dy / abs(dy);}
if (dy != 0 && v[0] != 0) {
 v[0] = 0;
 v[1] = dy;  Â
}
if (dx != 0 && v[1] != 0) {
 v[0] = dx;
 v[1] = 0;
}
}
Does your code REALLY look like that? Read the stickies at the top of the forum - the ones you were supposed to read BEFORE posting here - and fix your post.
Why is these so much code if you are having trouble with the joystick?
What range of values are you storing in the array called snake? Is the type of the array appropriate for the range of values you are storing in the array?
How many times do you need to call randomSeed()? Here's a hint: ONCE!
PaulS:
Does your code REALLY look like that? Read the stickies at the top of the forum - the ones you were supposed to read BEFORE posting here - and fix your post.
Why is these so much code if you are having trouble with the joystick?
What range of values are you storing in the array called snake? Is the type of the array appropriate for the range of values you are storing in the array?
How many times do you need to call randomSeed()? Here's a hint: ONCE!
hi there, forgot to mention this is a traditional snake game where the snake(LED dots) eat apple and grow until the snake filled the led matrix, the controller settings are in the void readControls(), but I'm not really sure what went wrong as the controller is highly unresponsive.
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.
Hi,
Did you write this code?
Did you write it in stages?
Getting each stage to work before writing for the next stage, then combined them?
Do you have code that just reads and displays the joystick outputs, with the LED array disconnected?
TomGeorge:
Hi,
Did you write this code?
Did you write it in stages?
Getting each stage to work before writing for the next stage, then combined them?
Do you have code that just reads and displays the joystick outputs, with the LED array disconnected?
Tom...
Hi tom, ive edited the post, hope this is better for you to read :). Nope I dont have code that reads and displays, this is what i've got. I think the problem is that i have to push the joystick in certain angle in order to turn, like if i need to turn right, i have to push the joysticm to 90degree angle otherwise it wont turn, from my code what do you think i should add in order to make it user friendly. Im just a beginner of arduino so i learn through compiling people's code, so i didnt write this code. I really dont know what went wrong, and its really frustrating since im not good at arduino and coding.
You will never get good at programming just reading other people's code. You MUST learn to write your own code.
Start NOW. Write a program that reads the value of the X axis of the joystick and prints the value to the serial port. The program should do nothing else. The program should have less than a dozen lines, not counting the blank lines and the lines that have only curly braces. Mine has 6, including 2 Serial.print() statements that print an identifier and the value.
When you get that working, then modify it to read the y value.
Do NOT attempt to use the values until you KNOW that you are reading the data correctly.
If you have problems, post the code YOU write. Do NOT post any more code that someone else wrote.
PaulS:
You will never get good at programming just reading other people's code. You MUST learn to write your own code.
Start NOW. Write a program that reads the value of the X axis of the joystick and prints the value to the serial port. The program should do nothing else. The program should have less than a dozen lines, not counting the blank lines and the lines that have only curly braces. Mine has 6, including 2 Serial.print() statements that print an identifier and the value.
When you get that working, then modify it to read the y value.
Do NOT attempt to use the values until you KNOW that you are reading the data correctly.
If you have problems, post the code YOU write. Do NOT post any more code that someone else wrote.
thanks for the advice, but I'm not too sure about how to set the joystick x and y axis in order to make it functional, I've been browsing through many websites for a week or so trying to figure it out, but I don't know what went wrong, cause i really want this snake game is work functionally, its always been my dream
Btw, Ive made one program called moving dots, where I set out the x and y axis with the controller and display it on the led matrix, it works perfectly fine, but for this program it is kinda different since there are rules into it, and so many variable together so I'm really confused... Ive been trying to figure out the x and y axis on this program with different parameters, but it doesn't seem to work as what i have expected.
You won't have a clue until you can get some reasonable data from the joystick.
How do I get data from the joystick? Sorry I'm just a newbie
PaulS:
You don't intend to start with the basics, do you?
I'm done wasting my time in this thread.
I know basics of how the joystick x and y axis works and how to set it out, I'm applying my knowledge onto this program, but it doesn't work on this program functionally , i just want to know how to fix it
for the value of the analog read, should I put it as (0, 1023, 5, -5)? Since they are the highest value?
No, not as parameters for analogRead() The extra parameters in the line
int dx = map(analogRead(VRX), 0, 1023, 0, 5);
are to do with mapping the range returned by analogRead(), which is 0 to 1023, to a smaller range (0 to 5), using the map() function. Start with the basics and read the raw analogRead() value then consider mapping it it another range rather than doing it all in one statement.