1

I am trying to create a basic snake game using the arduino UNO along with the MAX72XX module, the LCD module and the analog joystick module. I am wanting to program my program using object orientated programming. I have no experience with C++ so I am struggling quite a bit to understand why the serial comes up with unusual characters whenever I am trying to print off variable values. The serial is able to print text, but whenever I try to print variable values the unusual characters come up. I had tried changing the baud to a higher one and that makes no difference. The two variables that I am trying to print off both have an integer data type. I have tried using Serial.write, Serial.print and Serial.println, none of these seem to work. Here is the code if it helps solve the problem:

#include "LedControl.h"
#include <LiquidCrystal.h>
LedControl lc=LedControl(12,10,11,1);
LiquidCrystal lcd(8, 13, 3, 5, 6, 7);


class Snake_body
{


  public:

    int following_direction;
    int x;
    int y;
    int previous_x;
    int previous_y;
    bool enabled;
    Snake_body()
    {
      this->enabled = false;
    }

    void configure(int x_value, int y_value)
    {
      this->enabled = true;
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;  
    }

};


class Power_up
{
  private:

  public:
    virtual void init() 
    {
      this->x = 0;
      this->y = 0;
    }

    int x;
    int y;
};

Snake_body *snake[64];//this is the snake array that contains the snake objects

int snakes_length;


Power_up *power_up;//define the power up object

int score;

void setup() {

  Serial.begin(9600);


  lcd.begin(16, 2);

  snakes_length = 1;

  for(int z = 1; z < 64; z++)
  {
    snake[z] = new Snake_body();//create 63 un-enabled snake objects  
  }

  snake[0] = new Snake_body();//the 1st snake object is defined
  snake[0]->configure(0,0);//here it is initialised 
  power_up = new Power_up();//create the power_up object

  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  lcd.setCursor(0, 0);
  score = 0;
  lcd.print("score: ");
  lcd.setCursor(6, 0);
  lcd.print(score);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  power_up->x = random(0, 8);
  power_up->y = random(0, 8);
}

int snake_direction;


void loop() 
{
  int x_axis = analogRead(A0);
  int y_axis = analogRead(A1);
  int switcher = digitalRead(2);


   if(x_axis <= 400) //&& x < 7)//each of the if statements are for the direction of the snake head (snake[0])
   {
     snake[0]->following_direction = 0;
     //x += 1;
     //Serial->println("right");
   }


   else if(x_axis >= 600) //&& x > 0)
   {
     snake[0]->following_direction = 1;
     //x -= 1;
     //Serial->println("left");
   }


   else if(y_axis <= 400) //&& y > 0)
   {
     snake[0]->following_direction = 2;
     //y -= 1;
     //Serial->println("up");
   }


   else if(y_axis >= 600) //&& y < 7)
   {
     snake[0]->following_direction = 3;
     //y += 1;
     //Serial->println("down");
   }





  if(snake[0]->following_direction == 0 && snake[0]->x < 7)//used for snake movement
  {
    snake[0]->previous_x = snake[0]->x;
    snake[0]->x += 1;
  }

  else if(snake[0]->following_direction == 1 && snake[0]->x > 0)//used for snake movement
  {
    snake[0]->previous_x = snake[0]->x;
    snake[0]->x -= 1;
  }

  else if(snake[0]->following_direction == 2 && snake[0]->y > 0)//used for snake movement
  {
    snake[0]->previous_y = snake[0]->y;
    snake[0]->y -= 1;
  }

  else if(snake[0]->following_direction == 3 && snake[0]->y < 7)//used for snake movement
  {
    snake[0]->previous_y = snake[0]->y;
    snake[0]->y += 1;
  }


  if(snake[0]->x == power_up->x && snake[0]->y == power_up->y)//used for collision with power up
  {
    score += 1;
    lcd.setCursor(6,0);
    lcd.print(score);

    power_up->x = random(0, 8);
    power_up->y = random(0, 8);


    for(int j = 0; j < 64; j++)
    {
      if(snake[j]->enabled == false)//enables the next un-enabled snake object in the array
      {
         //Serial.println(true);
         Snake_body *snake[j];

         //next_snake_body = new Snake_body();
         //Snake_body next_snake_body;
         snake[j] = new Snake_body();
         snake[j]->configure(snake[j-1]->previous_x, snake[j-1]->previous_y);
         //Serial.print("x: " + snake[j-1]->previous_x);
         //Serial.print("y: " + snake[j-1]->previous_y);
         snake[j]->following_direction = NULL;
         break;
      }
    }
  }


  for(int i = 1; i < 64; i++)
  {
    if(snake[i]->enabled == true)//updates each snake object in the array
    {

      snake[i]->x = snake[i-1]->previous_x;
      snake[i]->y = snake[i-1]->previous_y;    
    }
  }


  lc.setLed(0,power_up->x,power_up->y,true);//draws the power up
  for(int w = 0; w < 64; w++)
  {
    if(snake[w]->enabled == true)//draws each snake object that is enabled
    {
      Serial.print("x: " + snake[w]->x);
      Serial.print("y: " + snake[w]->y);
      lc.setLed(0,snake[w]->x,snake[w]->y,true);  
    }
  }

  /*Serial->print("x: ");
  Serial->print(snake->x);
  Serial->println();

  Serial->print("y: ");
  Serial->print(snake->y);
  Serial->println();*/



  delay(100); 

  lc.clearDisplay(0);
}

the weird characters that show up are characters like this: enter image description here

with the follow picture y is just simply text to show which axis it is, the weird characters are the integer y axis value of the snake object that is being looked at.

15
  • Make a copy of your sketch and cut out everything but printing one text message and one variable value. Do you still have a problem? Add back a little bit at a time until the problem reappears. What you just added back is the cause. Commented May 27, 2020 at 16:11
  • everything can be important information when debugging code, including the weird characters ... please add the weird character printout to your post Commented May 27, 2020 at 16:31
  • There are some weird statements in the code, like the line Snake_body *snake[j];.. this does nothing and looks like a variable declaration, I'd remove that. Also in the beginning you snake[z] = new Snake_body(); but don't initialize the values (like y, x) in them via a constructor or initialize() function -- those will be random then and possibly garbage. You calso call Serial->print(snake->x); but snake is an 64-element array of Snake pointers, so that won't work. You need an index here. Commented May 27, 2020 at 16:51
  • Oh, and you also have a memory leak when you snake[j] = new Snake_body();. There already is a Snake_body element in there and you've overwritten the pointer to it with a newly allocated object without delete-ing the previous on -- reevaluate whether you really need to allocate a new object here of want to modify the existing one. If the latter, cleanup the memory. Commented May 27, 2020 at 16:52
  • You are using quite a bit of dynamic memory allocation. Note that this is a bad idea on an Arduino in 99% of cases. You can easily fill up all RAM without knowing beforehand and even when you correctly delete objects, that aren't needed anymore, you are getting your heap fragmented Commented May 27, 2020 at 17:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.