[SOLVED] total newbie problems

Understanding the language, error messages, etc.

[SOLVED] total newbie problems

Postby Montiey » Sat Jan 24, 2015 12:40 am

I'm not to new to programming or the arduino IDE, but I can't get this to work. It is supposed to count up, and thats about it.

Code: Select all
   
num += 1;
gb.display.println(num);


When I run it on the gamebuino it just returns a stable "1", or sometimes a "0" if I try other methods of adding 1 to the variable every cycle.
Last edited by Montiey on Mon Feb 16, 2015 1:49 pm, edited 1 time in total.
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby GlueMen » Sat Jan 24, 2015 12:57 am

This should work:

Code: Select all
num = 1 + num;
gb.display.println(num);


Here the variable num will be taken in the variable num to add 1 point
GlueMen
 
Posts: 4
Joined: Mon Jan 19, 2015 1:46 pm

Re: total newbie problems

Postby Montiey » Sat Jan 24, 2015 1:44 am

This still doesn't work. I think the problem is with how 1 is added to the var, and not anything else because I set a var to 255, and it works fine.
Code: Select all
int num = 255


Now, if I use
Code: Select all
num = num + 3

it gives a 3, which leads me to believe that it goes once, but then does not loop for some reason.
Do you know if there is a sort of "while true do" loop or equivalent in c++?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Skyrunner65 » Sat Jan 24, 2015 1:54 am

Yeah, just like this:
Code: Select all
volatile int num = 0  //says that num is a volatile integer (volatile meaning it is stored in RAM for fast access)
while(true) {
  num += 1;
  gb.display.println(num);
}


Are you sure you have it in void loop?
Also, true can be changed to 1, since 1 also equals true.
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: total newbie problems

Postby Montiey » Sat Jan 24, 2015 2:08 am

Here is all my code, but when I run it the popup I set that says "now counting" comes up, but thats all that changes and the tittle screen is still there, partially covered up behind the popup.

Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("super dumb app"));
  gb.popup(F("now counting"), 40);
}

// the loop routine runs over and over again forever
void loop(){
  //updates the gamebuino (the display, the sound, the auto backlight... everything)
  //returns true when it's time to render a new frame (20 times/second)
  if(gb.update()){
    //prints Hello World! on the screen
    gb.display.println(F("Hello World!"));
    //declare a variable named count of type integer :
    int count;
    int test = 255;
    volatile int num = 0;  //says that num is a volatile fast acces RAM integer
    //get the number of frames rendered and assign it to the "count" variable
    count = gb.frameCount;
    //prints the variable "count"
    gb.display.println(count);
    //me stuffz
    gb.battery.show = false;
    gb.display.println("Jason Harriot 2015");
   
    while(true) {
      num += 1;
      gb.display.println(num);
    }
    }
  }
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Myndale » Sat Jan 24, 2015 3:19 am

Actually your code is working fine, it's rendering the numbers but the back buffer isn't being sent to the LCD itself. One extra line in your inner loop will fix it:

Code: Select all
while(true) {
      num += 1;
      gb.display.println(num);
      gb.update();     // <---- add this
    }
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: total newbie problems

Postby rodot » Sat Jan 24, 2015 8:28 am

You should prefer the structure

Code: Select all
while{
   if(gb.update()){ //returns true each time a frame is rendered
      //the code placed here will run once a frame
   }
}


See gb.update
Because the way you wrote it it will run repeatedly in loop at high speed between each frame

So I changed your code for it to work properly. What I did is make two different screens with two loops. I added the fact that when you press "A" it will break out of the loop to continue on the next one. I also addec the ability to get back to the title screen to be able to get back to the loader.

Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("super dumb app"));
  gb.popup(F("now counting"), 40);
}

// the loop routine runs over and over again forever
void loop(){

  //first screen
  while(1){
    if(gb.update()){
      //prints Hello World! on the screen
      gb.display.println(F("Hello World!"));
      //declare a variable named count of type integer :
      int count;
      int test = 255;
      //get the number of frames rendered and assign it to the "count" variable
      count = gb.frameCount;
      //prints the variable "count"
      gb.display.println(count);
      //me stuffz
      gb.battery.show = false;
      gb.display.println("Jason Harriot 2015");

      if(gb.buttons.pressed(BTN_A)){
        break; //break out of the while loop when A is pressed
      }
    }
  }

  //second screen
  int num = 0;  //says that num is a volatile fast acces RAM integer
  while(true) {
    if(gb.update()){
      num += 1;
      gb.display.println(num);
      if(gb.buttons.pressed(BTN_A)){
        break; //break out of the while loop when A is pressed
      }
    }
  }

  //title screen to be able to get back to the game list
  gb.titleScreen(F("super dumb app"));
}
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: total newbie problems

Postby Montiey » Sat Jan 24, 2015 2:22 pm

I kinda get it.
So the main loop runs any standalone code in it in loop fashion excluding other loops, but when there is a BREAK; it goes to the next loop down and when there is a BREAK; again it goes to the next one down it can find.

Looking at the code some more, it seems like there is an important reason that the gb.update is nested in a while loop. Can someone explain this further?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Skyrunner65 » Sat Jan 24, 2015 4:58 pm

If I put something like this:
Code: Select all
void loop {
  while(1) {
    if (gb.update()) {
      gb.display.println("Potato!");
    }
  }
}


Then it would break off after printing that, then immediately go back in, since gb.update would be true again.
If I did this, however:
Code: Select all
void loop {
  while(1) {
    if (gb.update()) {
      gb.display.println("Potato!");
      if (gb.button.pressed(BTN_C) {//If I Press C
        break;
        break;
      }
    }
  }
gb.titlescreen("Hi There!")
}

It would add two breaks to its "counter", for a total of three (one to get out of "Pressed C" situation, another to get out of the gb.update() check, and the last to get out of the while(1) statement, to be able to go back to the titlescreen.
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: total newbie problems

Postby Montiey » Sat Jan 24, 2015 10:06 pm

Great examples, thanks.
I've worked on this some more and I just can't see why this is giving me an error when I try to verify it.
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("super dumb app"));
}

// the loop routine runs over and over again forever
void loop(){

  //first screen
  while(true){
    if(gb.update()){
      //prints Hello World! on the screen
      gb.display.println(F("Hello World!"));
      //declare a variable named count of type integer :
      int count;
      count = gb.frameCount; //get the number of frames rendered and assign it to the "count" variable
      //prints the variable "count"
      gb.display.println(count);
      //me stuffz
      gb.display.println("Jason Harriot 2015");

      if(gb.buttons.pressed(BTN_A)){
        break; //break out of the while loop when A is pressed
      }
    }
  }

  //second screen
  int num = 0;
  int offsetpause = 0;
  while(true){
    if(gb.update()){
      if(gb.buttons.pressed(BTN_A)){
        if(offsetpause = 1){
          offsetpause = 0;
        }
        else{
          if(offsetpause = 0){
          offsetpause = 1;
        }
      if(offsetpause = 1){
        num += 1;
      }
      gb.display.println(num);
      if(gb.buttons.pressed(BTN_B)){
        num = 0;
        gb.popup(F("reset"), 5);
      }
      if(gb.buttons.pressed(BTN_C)){
        break; //break out of the while loop when C is pressed
      }
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("super dumb app"));
}


I can't see where my error was, but if someone can fix it I would be very glad. :)
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 16 guests

cron