Debugging via serial

General

ragnarok93

3 months ago

Hello, how can I debug via serial port? I want to print on serialport values of variables. Tried doing it like this -


#include <Gamebuino-Meta.h> 

int number; 

void setup () 
{ 
  number = 0; 
  gb.begin (); 
  Serial.begin (9600); 
} 

void loop () 
{ 
  while (! gb.update ()); 
  gb.display.clear (); 

  gb.display.print (number); 
  Serial.println (number); 
  number ++; 
  delay (1000); 
}



But it dont works :(. It just crashes Gamebuino :(. Someone knows how to do this properly ?

jicehel

NEW 3 months ago

Just look hee: https://gamebuino.com/fr/community/topic/serial-print-not-working?page=1   ;)  I think it's will help. One thing, don't put a space betwwen function and (. For example, write: Serial.println(number); and not Serial.println (number); For me, your delay(1000); is useless as you are in a loop done 5 times / s.  Use a counter if you want something initaliszed to 0 in setup (compt for example) after you write something like :

void loop () 
{ 
  while (! gb.update ()); 
 compt++
  if(compt>=25) {
      gb.display.clear (); 
     gb.display.print (number); 
     Serial.println (number); 
     number ++;
     compt = 0;
   }
}

ripper121

NEW 3 months ago

You need to use "SerialUSB.print" instead of "Serial.print"

ragnarok93

NEW 3 months ago

Thanks for help ;). It works with SerialUSB.print :D.

Sorunome

NEW 3 months ago

Please note that with SerialUSB the `while(!SerialUSB);` will actually freeze the sketch until you open the serial monitor on the IDE

NoName

NEW 3 months ago

can you somehow use serial when running programs from the SD-card?

edit:  i tried to do so, and i first thought it worked, but then i changed the test program to get some real debug info and found out it just spits out the same stuff it did when running the test program via upload program.  

jicehel

NEW 3 months ago

Did you use "SerialUSB.print" instead of "Serial.print" ?

NoName

3 months ago

yes

NoName

NEW 3 months ago

jicehel jicehel

yes

NoName

NEW 3 months ago

first i wrote something like this:

#include <Gamebuino-Meta.h>

//more stuff

void setup() {
  gb.begin();
  //more stuff
  SerialUSB.begin(9600);
}

void loop() {
  while(!gb.update());
  gb.display.clear();
  SerialUSB.println("yay it works");
  //muuuuuuuch more stuff
}

, uploaded the code and got this:

Then I exported the compiled binary of the same code, copied it to the SD-card, started the program with the Gamenuino plugged into the computer, found the right serial monitor and got the same as above. After that I changed the code:

#include <Gamebuino-Meta.h>

#define DEBUG true   //toggle debug info

//more stuff

void setup() {
  gb.begin();
  //more stuff
  if (DEBUG)
  {
    SerialUSB.begin(9600);
    while (!SerialUSB)
    {
      gb.display.setCursor(0,0);
      gb.display.print("please open seril monitor");
      gb.display.clear();
    }
  }
  SerialUSB.println("-------------Serial successfully started-------------");
}

void loop() {
  while(!gb.update());
  gb.display.clear();
  //muuuuuuuch more stuff
  // \/ inside some func
  if (DEBUG)
  {
    SerialUSB.println([my debug info]);
  }
}

I compiled the code, copied it to the SD-card and got the same Serial output as in the vid above - only when I press the home-menu button the utput stops.

Sorunome

3 months ago

Are you sure you loaded the bin from the sd card? as in, exit to the loader, navigate to the game, and flash it


Sorunome

NEW 3 months ago

NoName NoName

Are you sure you loaded the bin from the sd card? as in, exit to the loader, navigate to the game, and flash it


NoName

2 months ago

Pretty sure but I got it to work anyway by putting this

while (!SerialUSB)
     {
       gb.display.setCursor(0,0);
       gb.display.print("please open seril monitor");
       gb.display.clear();
     
   }

to the beginning of void loop() instead of void setup().

NoName

NEW 2 months ago

Sorunome Sorunome

Pretty sure but I got it to work anyway by putting this

while (!SerialUSB)
     {
       gb.display.setCursor(0,0);
       gb.display.print("please open seril monitor");
       gb.display.clear();
     
   }

to the beginning of void loop() instead of void setup().

NoName

NEW 2 months ago

Does the SAVE.SAV-file somehow get damaged if you read it too often?

Currently I have a program which reads the file up to thrice or so times per frame, although thats not really necessary, I was just too lazy to chage it so it doesn't do that anymore. The thing is: It seems like the program reads what I expect it to read, but after some time it suddenly reads complete bullshit. Might there be a problem with reading the savefile to often?


A part of my serial output(watch the last number in block 2):

Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , 76
 , 256
 , 5
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , -1
 , 256
 , 0
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , -1
 , 256
 , 0
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , -1
 , 256
 , 256
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , -1
 , 256
 , 256
 , 
Got from block 3: 5
Got from block 1:  ,  ,  ,  , name , 
Got from block 2: 60
 , -1
 , -1
 , 256
 , 256
 , 
Got from block 3: 5


Sorunome

2 months ago

Does the SAVE.SAV-file somehow get damaged if you read it too often?

It shouldn't, however every single time you read from it the gamebuino will have to read from the SD card, not only slowing it down, but also using up some of the read speed needed for things like streaming audio. That is why it is highly recommended to read the values once at beginning and then cache them in ram in some variable.

The thing is: It seems like the program reads what I expect it to read, but after some time it suddenly reads complete bullshit

Even though what you are doing is bad practice this still shouldn't happen. Would you mind posting your program so that I can look into it more?


EDIT: Also, this is kinda off-topic for this topic....

Sorunome

NEW 2 months ago

NoName NoName

Does the SAVE.SAV-file somehow get damaged if you read it too often?

It shouldn't, however every single time you read from it the gamebuino will have to read from the SD card, not only slowing it down, but also using up some of the read speed needed for things like streaming audio. That is why it is highly recommended to read the values once at beginning and then cache them in ram in some variable.

The thing is: It seems like the program reads what I expect it to read, but after some time it suddenly reads complete bullshit

Even though what you are doing is bad practice this still shouldn't happen. Would you mind posting your program so that I can look into it more?


EDIT: Also, this is kinda off-topic for this topic....

NoName

2 months ago

I figured that it works if I init the highscores[]-array while I was copypasting together this, but heres part of my code anyway:

#include <Gamebuino-Meta.h>

String highscoreNames[5] = {"1", "2", "3", "4", "5"};
int highscores[] = {0, 0, 0, 0, 0};
int score;
int highscore;

void setup()
{
  gb.begin();
  //init everything for the game
}

void loop()
{
  while(!gb.update());
  while (!SerialUSB)
  {
    gb.display.setCursor(0, 0);
    gb.display.clear();
    gb.display.println("Please start Serial monitor");
  }
  gb.display.clear();     //some of these gb.display.clear() statements are obviously redundant
  //update all the stuff for the game
  //if gameover, checkForNewHighscore() to determine whether or not to display the "NEW GIGHSCORE!!!"message + display the current highscore
  //if new highscore, ask the user to enter their name and then setNewHighscore(the-name-the-user-entered, score), after that drawHighscoreList until user presses A
}

bool setNewHighscore(String Name, int Highscore)
{
  readSaveFile();
  
  //updating the highscores[] and highscoreNames[] arrays
  for (int i = 0; i <= 4; i++)  //put all items of the array one position "higher", 1 gets deleted and the last bcomes free space(to be exact in this case it gets replaced with the new stuff rightaway)
    {
      if (i != 4)
      {
        highscoreNames[i] = highscoreNames[i + 1];
        highscores[i] = highscores[i + 1];
      }
      else
      {
        highscoreNames[i] = Name;   //set this to "" or "5" and
        highscores[i] = Highscore;  //this to 0 to clear the entries instead
      }
    }
  
  SerialUSB.print("Saving to block 1: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.print(highscoreNames[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.print(" ...........");
  if (gb.save.set(1, highscoreNames))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  SerialUSB.print("Saving to block 2: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.println(highscores[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.print(" ...........");
  if (gb.save.set(2, highscores))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  SerialUSB.print("Saving to block 3: ");
  SerialUSB.print(Highscore);
  SerialUSB.print(" ...........");
  if (gb.save.set(3, Highscore))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  return true; //normally i would return this: gb.save.set(1, highscoreNames) and gb.save.set(2, highscores) and gb.save.set(3, Highscore); but i didnt want to save it twice as the whole saving thing is a bit fragile
}

void readSaveFile()
{
  gb.save.get(1, highscoreNames);
  gb.save.get(2, highscores);
  highscore = gb.save.get(3);
  
  SerialUSB.print("Got from block 1: ");
  for (int i = 0; i <= 4; i++)           //4 because currently the highscorelist has 5 entrys max.
  {
    SerialUSB.print(highscoreNames[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.println("");
  SerialUSB.print("Got from block 2: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.println(highscores[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.println("");
  SerialUSB.print("Got from block 3: ");
  SerialUSB.println(highscore);
}

bool checkForNewHighscore()
{
  readSaveFile();
  if (score > highscore)
  {
    return true; 
  }
  else
  {
    return false;
  }
}

void drawHighscoreList()
{
  readSaveFile();
  gb.display.setCursor(0, 0);
  for (int i = 0; i <= 4; i++)
  {
    gb.display.print(String(highscoreNames[i]));
    gb.display.print(" ");
    gb.display.println(highscores[i]);
  }
}

It still stops working as soon as you exit and restart the game though, it does read the highscores[]-array correctly, but the highscoreNames[]-thing seems to be broken. After playing 2 rounds(saving a new highscore twice which seemed to work just fine) and then restarting I got this:

Got from block 1:  ,  , ⸮ , 4 , 5 , 
Got from block 2: 0
 , 0
 , 0
 , 5
 , 10
 , 
Got from block 3: 10


About this being off-topic:

I got the serial-thing to work a few weeks ago and after that I didn't work on this project until today and still was like the serial thing still was my problem(although it obviously works now )so I posted it here without thinking, sorry.

NoName

NEW 2 months ago

Sorunome Sorunome

I figured that it works if I init the highscores[]-array while I was copypasting together this, but heres part of my code anyway:

#include <Gamebuino-Meta.h>

String highscoreNames[5] = {"1", "2", "3", "4", "5"};
int highscores[] = {0, 0, 0, 0, 0};
int score;
int highscore;

void setup()
{
  gb.begin();
  //init everything for the game
}

void loop()
{
  while(!gb.update());
  while (!SerialUSB)
  {
    gb.display.setCursor(0, 0);
    gb.display.clear();
    gb.display.println("Please start Serial monitor");
  }
  gb.display.clear();     //some of these gb.display.clear() statements are obviously redundant
  //update all the stuff for the game
  //if gameover, checkForNewHighscore() to determine whether or not to display the "NEW GIGHSCORE!!!"message + display the current highscore
  //if new highscore, ask the user to enter their name and then setNewHighscore(the-name-the-user-entered, score), after that drawHighscoreList until user presses A
}

bool setNewHighscore(String Name, int Highscore)
{
  readSaveFile();
  
  //updating the highscores[] and highscoreNames[] arrays
  for (int i = 0; i <= 4; i++)  //put all items of the array one position "higher", 1 gets deleted and the last bcomes free space(to be exact in this case it gets replaced with the new stuff rightaway)
    {
      if (i != 4)
      {
        highscoreNames[i] = highscoreNames[i + 1];
        highscores[i] = highscores[i + 1];
      }
      else
      {
        highscoreNames[i] = Name;   //set this to "" or "5" and
        highscores[i] = Highscore;  //this to 0 to clear the entries instead
      }
    }
  
  SerialUSB.print("Saving to block 1: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.print(highscoreNames[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.print(" ...........");
  if (gb.save.set(1, highscoreNames))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  SerialUSB.print("Saving to block 2: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.println(highscores[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.print(" ...........");
  if (gb.save.set(2, highscores))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  SerialUSB.print("Saving to block 3: ");
  SerialUSB.print(Highscore);
  SerialUSB.print(" ...........");
  if (gb.save.set(3, Highscore))
  {
    SerialUSB.println("successfull!");
  }
  else
  {
    SerialUSB.println("failed!");
  }
  return true; //normally i would return this: gb.save.set(1, highscoreNames) and gb.save.set(2, highscores) and gb.save.set(3, Highscore); but i didnt want to save it twice as the whole saving thing is a bit fragile
}

void readSaveFile()
{
  gb.save.get(1, highscoreNames);
  gb.save.get(2, highscores);
  highscore = gb.save.get(3);
  
  SerialUSB.print("Got from block 1: ");
  for (int i = 0; i <= 4; i++)           //4 because currently the highscorelist has 5 entrys max.
  {
    SerialUSB.print(highscoreNames[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.println("");
  SerialUSB.print("Got from block 2: ");
  for (int i = 0; i <= 4; i++)
  {
    SerialUSB.println(highscores[i]);
    SerialUSB.print(" , ");
  }
  SerialUSB.println("");
  SerialUSB.print("Got from block 3: ");
  SerialUSB.println(highscore);
}

bool checkForNewHighscore()
{
  readSaveFile();
  if (score > highscore)
  {
    return true; 
  }
  else
  {
    return false;
  }
}

void drawHighscoreList()
{
  readSaveFile();
  gb.display.setCursor(0, 0);
  for (int i = 0; i <= 4; i++)
  {
    gb.display.print(String(highscoreNames[i]));
    gb.display.print(" ");
    gb.display.println(highscores[i]);
  }
}

It still stops working as soon as you exit and restart the game though, it does read the highscores[]-array correctly, but the highscoreNames[]-thing seems to be broken. After playing 2 rounds(saving a new highscore twice which seemed to work just fine) and then restarting I got this:

Got from block 1:  ,  , ⸮ , 4 , 5 , 
Got from block 2: 0
 , 0
 , 0
 , 5
 , 10
 , 
Got from block 3: 10


About this being off-topic:

I got the serial-thing to work a few weeks ago and after that I didn't work on this project until today and still was like the serial thing still was my problem(although it obviously works now )so I posted it here without thinking, sorry.

Sorunome

NEW 2 months ago

Even with the constant reading off of gb.save.get I am unable to reproduce your issue :/

Please log in to join the discussion!

Log in