Hey i tried to make tetris game on dot matrix max7219 and i want to use android trough NodMcu and Blynk app as a control button, but i have problem here's the code :
#include "MatrixMathInt.h"
#include "LedControl.h"
#include <SoftwareSerial.h>
SoftwareSerial ArduinoUno (3, 2);
#define SCREENS (4)
#define SCREEN_SIZE (8)
#define COLS (8)
#define ROWS (32)
#define CENTER (3)
LedControl lc=LedControl(11,13,10,SCREENS);
unsigned long delaytime=500;
const long interval = 1000;
unsigned long previousMillis = 0;
bool gameOver = false;
int world[ROWS][COLS] = {};
const int pinL;
const int pinU;
const int pinR;
const int pinD;
#define PIEZED (4)
#define PIEZES (7)
int piezes[PIEZES][PIEZED][PIEZED] = {
{
{1,0,0},
{1,0,0},
{1,0,0},
{1,0,0},
},
{
{1,0,0},
{1,1,0},
{1,0,0},
},
{
{1,1,0},
{1,1,0},
{0,0,0},
},
{
{1,0,0},
{1,0,0},
{1,1,0},
},
{
{0,1,0},
{0,1,0},
{1,1,0},
},
{
{1,0,0},
{1,1,0},
{0,1,0},
},
{
{0,1,0},
{1,1,0},
{1,0,0},
},
};
int pieze[PIEZED][PIEZED] = {};
int piezeX = CENTER;
int piezeY = 0;
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
ArduinoUno.begin(4800);
randomSeed(analogRead(0));
//we have already set the number of devices when we created the LedControl
int devices=lc.getDeviceCount();
//we have to init all devices in a loop
for(int address=0;address<devices;address++) {
/*The MAX72XX is in power-saving mode on startup*/
lc.shutdown(address,false);
/* Set the brightness to a medium values */
lc.setIntensity(address,1);
/* and clear the display */
lc.clearDisplay(address);
}
Serial.println("starting....");
generatePieze();
pinMode(pinR, INPUT);
pinMode(pinL, INPUT);
pinMode(pinU, INPUT);
pinMode(pinD, INPUT);
}
void beginGameOver(){
Serial.println("gameover" );
for(int screen=0;screen<SCREENS;screen++){
for(int row=0;row<SCREEN_SIZE;row++) {
lc.setColumn(SCREENS-screen-1, row, B11111111);
delay(100);
}
}
int newWorld[ROWS][COLS] = {};
Matrix.Copy((int*)newWorld, ROWS, COLS,(int*)world);
piezeX = CENTER;
piezeY = -1;
generatePieze();
gameOver = false;
render(world);
}
void generatePieze(){
int randNumber = random(PIEZES);
Matrix.Copy((int*)piezes[randNumber], PIEZED, PIEZED,(int*)pieze);
};
void checkLine(){
for(int row=0;row<ROWS;row++) {
int count = 0;
for(int col=0;col<COLS;col++) {
count = count + world[row][col];
if (count >= COLS){
deleteRow(row);
}
}
}
}
void deleteRow(int toDelete){
int screen = (toDelete / SCREEN_SIZE);
int row = SCREEN_SIZE - (toDelete - (screen * SCREEN_SIZE)) - 1;
for(int i=0;i<10;i++) {
sound(3000);
lc.setColumn(screen, row, B11111111);
delay(25);
lc.setColumn(screen, row, B00000000);
delay(25);
}
for(int row=ROWS;row>0;row--) {
if(row <= toDelete){
if(row == 0){
//world[row] = {};
} else {
for(int col=0;col<COLS;col++) {
world[row][col] = world[row-1][col];
}
}
}
}
}
void actions(){
if (digitalRead(pinR) == HIGH){
if (validPosition(pieze, piezeY, piezeX+1)){
moveSound();
piezeX++;
} else {
noMoveSound();
}
}
if (digitalRead(pinL) == HIGH){
if (validPosition(pieze, piezeY, piezeX-1)){
moveSound();
piezeX--;
} else {
noMoveSound();
}
}
if (digitalRead(pinD) == HIGH){
if (validPosition(pieze, piezeY+1, piezeX)){
moveSound();
piezeY++;
} else {
noMoveSound();
}
}
if (digitalRead(pinU) == HIGH){
rotatePieze();
}
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if (incomingByte == 97){ // a
if (validPosition(pieze, piezeY, piezeX-1)){
piezeX--;
}
}
if (incomingByte == 100){ // d
if (validPosition(pieze, piezeY, piezeX+1)){
piezeX++;
}
}
if (incomingByte == 115){ // s
if (validPosition(pieze, piezeY+1, piezeX)){
piezeY++;
}
}
if (incomingByte == 119){ // w
rotatePieze();
}
}
}
void moveSound(){
sound(800);
}
void noMoveSound(){
sound(200);
}
void sound(int frequency){
tone(8, frequency, 16);
}
void rotatePieze(){
int newPieze[PIEZED][PIEZED] = {};
int tPieze[PIEZED][PIEZED] = {};
Matrix.Transpose((int*)pieze,PIEZED, PIEZED, (int*)newPieze);
for(int row=0;row<PIEZED;row++) {
for(int col=0;col<PIEZED;col++) {
tPieze[row][PIEZED-col-1] = newPieze[row][col];
}
}
for(int i=0; i<PIEZED; i++){
if(tPieze[0][0] + tPieze[1][0] + tPieze[2][0] == 0){
for(int row=0;row<PIEZED;row++) {
for(int col=1;col<PIEZED;col++) {
tPieze[row][col-1] = tPieze[row][col];
}
tPieze[row][PIEZED-1] = 0;
}
}
}
if (validPosition(tPieze, piezeY, piezeX)){
moveSound();
Matrix.Copy((int*)tPieze, PIEZED, PIEZED,(int*)pieze);
} else {
noMoveSound();
}
}
bool validPosition(int mPieze[PIEZED][PIEZED], int destY, int destX){
bool valid = true;
int x = 0;
int y = 0;
for(int row=destY;row<(PIEZED+destY);row++){
for(int col=destX;col<(PIEZED+destX);col++){
if (mPieze[y][x] == 1){
if(world[row][col] == 1){
return false;
}
if(col>COLS-1){
return false;
}
if(col < 0){
return false;
}
if(row > ROWS-1){
return false;
}
}
x++;
}
x = 0;
y++;
}
}
bool collision(int big[ROWS][COLS], int small[PIEZED][PIEZED], int bM, int bN, int sM, int sN, int pY, int pX){
int mergeX = 0;
int mergeY = 0;
for(int row=0;row<bM;row++) {
for(int col=0;col<bN;col++) {
if( col>= pX && mergeX < PIEZED && row >= pY && mergeY < PIEZED ){
if (small[mergeY][mergeX] == 1 && big[row+1][col] == 1) {
return true;
}
if (row == ROWS-1 && small[mergeY][mergeX] == 1){
return true;
}
if( col>= piezeX && mergeX < PIEZED){
mergeX++;
}
}
}
mergeX = 0;
if( row >= pY && mergeY < PIEZED){
mergeY++;
}
}
return false;
}
void mergeMatrix(int big[ROWS][COLS], int small[PIEZED][PIEZED], int bM, int bN, int sM, int sN, int pY, int pX){
for(int row=0;row<bM;row++) {
for(int col=0;col<bN;col++) {
if (col >= pX && col < pX + PIEZED && row >= pY && row < pY + PIEZED){
int val = small[row - pY][col - pX];
if (val == 0) {
val = big[row][col];
}
big[row][col] = val;
}
}
}
}
void render(int matrix[ROWS][COLS]){
int row = 0;
for(int screen=0;screen<SCREENS;screen++) {
int matrox[SCREEN_SIZE][SCREEN_SIZE] = {};
for(int srow=0;srow<SCREEN_SIZE;srow++) {
for(int col=0;col<COLS;col++) {
matrox[srow][col] = matrix[row][col];
}
row++;
}
int matrex[SCREEN_SIZE][SCREEN_SIZE] = {};
Matrix.Transpose((int*)matrox,SCREEN_SIZE, SCREEN_SIZE, (int*)matrex);
for(int srow=0;srow<SCREEN_SIZE;srow++) {
for(int col=0;col<COLS;col++) {
lc.setLed(screen,srow,SCREEN_SIZE-col-1,matrex[srow][col]);
}
}
}
}
void loop() {
while (ArduinoUno.available() > 0) {
int val = ArduinoUno.parseInt();
if (ArduinoUno.read() == '\n') {
Serial.println(val);
if (val == HIGH) {
digitalWrite (pinL, HIGH);
}
else {
Serial.println("No");
}
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (collision(world, pieze, ROWS, COLS, PIEZED, PIEZED, piezeY, piezeX)){
sound(800);
mergeMatrix(world, pieze, ROWS, COLS, PIEZED, PIEZED, piezeY, piezeX);
if(piezeY <= 0){
gameOver = true;
}
piezeY = 0;
piezeX = CENTER;
generatePieze();
} else {
piezeY++;
}
checkLine();
}
if (gameOver == true){
beginGameOver();
} else {
int view[ROWS][COLS] = {};
Matrix.Copy((int*)world, ROWS, COLS,(int*)view);
mergeMatrix(view, pieze, ROWS, COLS, PIEZED, PIEZED, piezeY, piezeX);
render(view);
actions();
}
}
The max7219 wont run because this code, event i place all function inside this code the max7219 won't run :
while (ArduinoUno.available() > 0) {
int val = ArduinoUno.parseInt();
if (ArduinoUno.read() == '\n') {
Serial.println(val);
if (val == HIGH) {
digitalWrite (pinL, HIGH);
}
else {
Serial.println("No");
}
}
}
so i dont know anymore how to solve this problem please. (main code generatePieze();
Tetris Source Code : tetris-arduino/tetris.ino at master · ceritium/tetris-arduino · GitHub