Greeting, I'm using the library TVOut, and I want a specific function to be called if I send the "w" letter in the Serial monitor, but theres one problem:
When I send the letter, the arduino doesn't recieve it (The yellow led blinks but the function isn't called), I've tried to debug my code to know if the problem was the serial monitor, and when I start the program on my arduino it send the "ready" strings, and it works, but when I send something with the serial monitor the Serial.available() isn't higher than 0, and I want to know why.
I've tried differents baud rates, and differents timeouts functions, but it doesn't work.
Any ideas?
Thanks!
Here's my code:
#include <TVout.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stream.h>
TVout TV;
const int8_t gb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2}, //Grass Block
{2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2},
{2,2,2,1,1,1,1,0,0,1,1,1,1,2,2,2},
{2,1,1,1,1,0,0,1,1,0,0,1,1,1,1,2},
{1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1},
{1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1},
{1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1},
{1,0,0,0,1,0,0,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1},
{2,1,1,0,0,0,0,1,1,0,0,0,0,1,1,2},
{2,2,2,1,1,0,0,1,1,0,0,1,1,2,2,2},
{2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2},
{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2}};
const int8_t Cgb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2}, //Grass Block
{2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2},
{2,2,2,1,1,1,1,0,0,1,1,1,1,2,2,2},
{2,1,1,1,1,0,0,1,1,0,0,1,1,1,1,2},
{1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,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0,1},
{1,0,0,1,1,0,0,1,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,1,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,1},
{2,1,1,0,0,0,0,1,1,0,0,0,0,1,1,2},
{2,2,2,1,1,0,0,1,1,0,0,1,1,2,2,2},
{2,2,2,2,2,1,1,1,1,1,1,2,2,2,2,2},
{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2}};
int8_t WD[11][11][11]={64}; //World Array
int8_t cursorx = 0;
int8_t cursory = 6;
int8_t cursorz = 0;
String cmdd;
void drawCube(int8_t t, int8_t x, int8_t y, int8_t z) {
int8_t xx = (x - z) << 3;
int8_t yy_base = ((x + z) << 2) + (y << 3);
const int8_t (*block)[16]; // Pointer to the block array
switch (t) { //check the block type
case 1:
block = gb;
break;
case 2:
block = db;
break;
case 3:
block = sb;
break;
case 4:
block = bb;
break;
case 11:
block = Cgb;
break;
case 12:
block = Cdb;
break;
case 13:
block = Csb;
break;
default:
return; // Exit the function if block type is not recognized
}
int8_t i, j;
int8_t xx_i, yy_j;
int8_t *block_ptr;
for (i = 0, xx_i = xx; i < 16; ++i, ++xx_i) {
block_ptr = (int8_t *)block + i;
yy_j = yy_base;
for (j = 0; j < 16; ++j, ++yy_j) {
int8_t block_val = *block_ptr;
if (block_val != 2) {
TV.set_pixel(xx_i, yy_j, block_val); // Set pixel directly on screen
}
block_ptr += 16; // Move to the next row in the block array
}
}
}
void generateWorld() { //Generating the world
for(int8_t i=10; i>0; --i) {
for(int8_t j=0; j<10; ++j) {
for(int8_t k=0; k<10; ++k) {
if(i==10){
WD[i][j][k] = 4;
}
if(i==9){
WD[i][j][k] = 3;
}
else if(i==8){
WD[i][j][k] = 3;
}
else if(i==7){
WD[i][j][k] = 2;
}
else if(i==6){
WD[i][j][k] = 1;
}
else if(i==5){
WD[i][j][k] = 64;
}
else if(i==4){
WD[i][j][k] = 64;
}
else if(i==3){
WD[i][j][k] = 64;
}
else if(i==2){
WD[i][j][k] = 64;
}
else if(i==1){
WD[i][j][k] = 64;
}
else if(i==0){
WD[i][j][k] = 64;
}
}
}
}
for(int8_t i=10; i>0; --i) {
for(int8_t j=0; j<11; ++j) {
for(int8_t k=0; k<11; ++k) {
if(k==10){
WD[i][j][k] = 64;
}
if(j==10){
WD[i][j][k] = 64;
}
}
}
}
}
void drawWorld() {
TV.clear_screen(); //Clear the screen to eliminate excess pixels
for(int8_t i = 10; i >= 0; --i) { //Start drawing the world from the bottom, so blocks don't overlap when drawing
for(int8_t j = 0; j < 10; ++j) {
for(int8_t k = 0; k < 10; ++k) {
int8_t blockType = WD[i][j][k];
if(blockType != 0 && (WD[i - 1][j][k] > 9 || WD[i][j + 1][k] > 9 || WD[i][j][k + 1] > 9)) { //Checking if block is not covered
drawCube(blockType, k + 4, i, j - 8);
}
}
}
}
}
void setCursor() {
WD[cursory][cursorz][cursorx] = WD[cursory][cursorz][cursorx] + 10;
}
void moveCursor(int8_t dir) {
WD[cursory][cursorz][cursorx] = WD[cursory][cursorz][cursorx] - 10;
switch(dir) {
case 1:
++cursorx;
break;
case 2:
++cursory;
break;
case 3:
++cursorz;
break;
case 4:
--cursorx;
break;
case 5:
--cursory;
break;
case 6:
--cursorz;
break;
}
WD[cursory][cursorz][cursorx] = WD[cursory][cursorz][cursorx] + 10;
drawWorld();
}
int main() {
TV.begin(PAL,200,124);
Serial.begin(9600);
Serial.setTimeout(10);
generateWorld();
setCursor();
TV.delay_frame(1);
drawWorld();
Serial.println("ready");
while(1) {
if(Serial.available()>0) {
cmdd = Serial.readString();
Serial.println("test2");
cmdd.trim();
if(cmdd=="w") {
Serial.println("test");
moveCursor(3);
}
}
}
return 0;
}