Serial.available and read doesn't work with TVOut

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,1},
                 {1,1,1,1,1,0,0,1,1,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,1,1,0,0,1,1,0,0,1,1,0,0,1},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,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 nb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Testing Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,0,0,0,0,0,1,1,2,2,2},
                 {2,1,1,0,0,0,0,0,0,0,0,0,0,1,1,2},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,0,0,1,1,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},
                 {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 cursor[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Testing Block
                            {2,2,2,2,2,2,1,2,2,1,2,2,2,2,2,2},
                            {2,2,2,2,1,2,2,1,1,2,2,1,2,2,2,2},
                            {2,2,1,2,2,2,1,1,1,1,2,2,2,1,2,2},
                            {1,2,2,2,2,2,1,1,1,1,2,2,2,2,2,1},
                            {1,1,2,2,2,2,2,1,1,2,2,2,2,2,1,1},
                            {2,2,2,1,2,2,2,2,2,2,2,2,1,2,2,2},
                            {1,2,2,2,2,1,2,1,1,2,1,2,2,2,2,1},
                            {2,2,2,1,1,2,2,2,2,2,2,1,1,2,2,2},
                            {1,2,1,1,1,1,2,1,1,2,1,1,1,1,2,1},
                            {2,2,1,1,1,1,2,2,2,2,1,1,1,1,2,2},
                            {1,2,2,1,1,2,2,1,1,2,2,1,1,2,2,1},
                            {2,1,2,2,2,2,2,2,2,2,2,2,2,2,1,2},
                            {2,2,2,1,2,2,2,1,1,2,2,2,1,2,2,2},
                            {2,2,2,2,2,1,2,2,2,2,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 sb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Stone Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,1,1,0,0,0,1,1,2,2,2},
                 {2,1,1,0,0,0,0,1,0,0,1,1,0,1,1,2},
                 {1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1},
                 {1,1,1,0,1,1,0,0,0,0,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1},
                 {1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1},
                 {1,0,0,1,0,0,0,1,1,0,0,0,0,1,0,1},
                 {1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1},
                 {1,0,0,0,1,0,0,1,1,0,1,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 Csb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Stone Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,1,1,0,0,0,1,1,2,2,2},
                 {2,1,1,0,0,0,0,1,1,0,1,1,0,1,1,2},
                 {1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
                 {1,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1},
                 {1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,1,0,1},
                 {1,0,0,1,1,0,1,1,1,0,0,1,1,0,0,1},
                 {1,0,0,0,1,0,0,1,1,0,1,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 bb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Bedrock Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,0,1,1,0,0,1,1,2,2,2},
                 {2,1,1,0,0,1,1,0,0,1,1,0,0,1,1,2},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1},
                 {1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1},
                 {1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1},
                 {1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1},
                 {2,1,1,0,0,1,1,1,1,1,1,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 db[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Dirt Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,0,0,0,0,0,1,1,2,2,2},
                 {2,1,1,0,0,0,0,1,0,0,0,0,0,1,1,2},
                 {1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1},
                 {1,1,1,0,0,0,0,0,0,1,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1},
                 {1,0,0,0,0,1,0,1,1,0,0,0,0,0,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}};

const int8_t Cdb[16][16]={{2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2},  //Dirt Block
                 {2,2,2,2,2,1,1,0,0,1,1,2,2,2,2,2},
                 {2,2,2,1,1,0,0,0,0,0,0,1,1,2,2,2},
                 {2,1,1,0,0,0,0,1,1,0,0,0,0,1,1,2},
                 {1,0,0,0,1,0,1,1,1,1,1,1,0,0,0,1},
                 {1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1},
                 {1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1},
                 {1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,1},
                 {1,0,0,0,0,1,0,1,1,0,0,0,0,0,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;
}

Why did you write your own main() function rather than using the more usual Arduino0 setup() and loop() functions ?

Are you aware of what the hidden main() function in the Arduino normal environment does such as initialising the environment ?

The void loop and setup functions take more memory, and are slower than the classic C language main() function.

Using your own main() function in the Arduino environment is only OK if you use it correctly, which I don't think you are

As I asked previously,

Wow, you're right, after reverting to void loop and setup all worked, thanks!

If you really want to use your own main() function then take a look at how it is used behind the scenes in the Arduino environment

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

What is your arduino board?
The arrays in your code almost twice a size than entire RAM of Arduino Uno.

I have an Arduino MEGA 2560.

Thanks for your time, i will take a look!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.