const int leds[] = { 13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5};
const int ledStripSize = sizeof(leds) / sizeof(leds[0]);
const int rows = 3;
const int cols = 3;
int posX = 0;
int posY = 0;
//controls
int HORZ_PIN = A1;
int VERT_PIN = A0;
int SEL_PIN = 4;
//
int randomX;
int randomY;
//
const int speakerPin = 3;
//creating a static array since dynamic arrays are not allow
int ledMatrix[rows][cols];
void setup() {
Serial.begin(9600);
for(int i = 0 ; i < ledStripSize ; i++ ){
pinMode(leds[i] , OUTPUT);
}
//controls
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
//speaker
pinMode(speakerPin , OUTPUT);
//assigning
int counter = 0;
while(counter < ledStripSize){
for(int x = 0; x < rows ; x++ ) {
for(int y = 0 ; y < cols ; y++){
ledMatrix[x][y] = leds[counter];
// Serial.println(ledMatrix[x][y]);
counter++;
}
}
}
spawnSeed();
blink(300);
}
// generating a dot in the random positon
void loop() {
//check if colliding
if(ledMatrix[randomX][randomY] == ledMatrix[posX][posY]){
Serial.println("Collided");
tone(speakerPin , 165);
clear();
delay(100);
noTone(speakerPin);
spawnSeed();
}
int horz = analogRead(HORZ_PIN);
int vert = analogRead(VERT_PIN);
if(horz == 512 && vert == 1023){
Serial.println("UP");
up();
}
if(horz == 512 && vert == 0){
Serial.println("DOWN");
down();
}
if(horz == 0 && vert == 512){
Serial.println("RIGHT");
right();
}
if(horz == 1023 && vert == 512){
Serial.println("LEFT");
left();
}
//reset positons
if(digitalRead(SEL_PIN) == LOW){
posX = 0;
posY = 0;
}
blink(100);
}
void right(){
if(posY < cols - 1){
posY++;
}
blink(300);
}
void left(){
if(posY > 0){
posY--;
}
blink(300);
}
void up(){
if(posX > 0){
posX--;
}
blink(300);
}
void down(){
if(posX < rows - 1){
posX++;
}
blink(300);
}
void blink(int duration){
digitalWrite(ledMatrix[posX][posY] , HIGH);
delay(duration);
// Serial.println("X"); Serial.println(posX);
// Serial.println("Y"); Serial.println(posY);
digitalWrite(ledMatrix[posX][posY] , LOW);
delay(duration);
}
///let H
void spawnSeed(){
randomX = random(0 , 3);
randomY = random(0 , 3);
//if the led is at the same postion as the seed , then redo
if(ledMatrix[randomX][randomY] == ledMatrix[posX][posY] ){
spawnSeed();
}
digitalWrite(ledMatrix[randomX][randomY] , HIGH);
delay(1000);
}
void clear(){
digitalWrite(ledMatrix[randomX][randomY] , LOW);
}