Help With Code - Reading Pokitto Buttons

Could anyone say me what the mistake here is?

#include "Pokitto.h"

Pokitto::Core mygame;

int main () {
    mygame.begin ();
    while (mygame.isRunnning()) {
        if (mygame.update()) {
            mygame.display.setColor(1);  //setzt die farbe des textes - ich mache mal jeden text in einer anderen farbe :D
            mygame.display.printIn("Tasten Test"); //
            mygame.display.setColor(2);
            if (mygame.buttons.aBtn()) mygame.display.println("A");
            mygame.display.setColor(3);
            if (mygame.buttons.bBtn()) mygame.display.printIn("B");
            mygame.display.setColor(4);
            if mygame.buttons.cBtn ()) mygame.display.printIn("C");
            mygame.display.setColor(5);
            if mygame.buttons.upBtn()) mygame.display.printIn("Up");
            mygame.display.setColor(6);
            if mygame.buttons.downBtn()) mygame.display.printIn("Down");
            mygame.display.setColor(7);
            if mygame.buttons.leftBtn()) mygame.display.printIn("Left");
            mygame.display.setColor(8);
            if mygame.buttons.rightBtn()) mygame.display.printIn("Right");
            mygame.display.setColor(9);
        }
    }
    return 0;
}

EDIT : Don’t know why but it is changing my text automatically

Mbed doesnt even compile this, how did you compile it ?

That`s the problem i cant compile it.

i think you are missing {

Original code from forum

 #include "Pokitto.h"

Pokitto::Core mygame;

int main () {
    mygame.begin();
    while (mygame.isRunning()) {
        if (mygame.update()) {         
            mygame.display.setColor(1); // set text color - let's make all texts in different color!
            mygame.display.println("Button test"); // println prints a row of text and goes to the next row (=enter)
            mygame.display.setColor(2); 
            if (mygame.buttons.aBtn()) mygame.display.println("A");
            mygame.display.setColor(3); 
            if (mygame.buttons.bBtn()) mygame.display.println("B");
            mygame.display.setColor(4); 
            if (mygame.buttons.cBtn()) mygame.display.println("C");
            mygame.display.setColor(5); 
            if (mygame.buttons.upBtn()) mygame.display.println("Up");
            mygame.display.setColor(6); 
            if (mygame.buttons.downBtn()) mygame.display.println("Down");
            mygame.display.setColor(7); 
            if (mygame.buttons.leftBtn()) mygame.display.println("Left");
            mygame.display.setColor(8); 
            if (mygame.buttons.rightBtn()) mygame.display.println("Right");      
        }
    }
}

compare it, not sure where the difference is…

ah i see it, there are 3 n`s in runnning, and you spelled println wrong.
Also after a if you need a (

2 Likes

It works fine now thx!

2 Likes

@Wandi
I moved your question into its own topic and edited it to format the code properly.
It’s easier to attract attention when you’ve got a code problem if you make your own topic, and it’s also easier for people who have their own problems to search for solved problems if it’s in its own topic.


How to make a new topic:

If you don’t know how to open a new topic, the button should be at the top of the main page under your user icon:

The box for filling in the topic info is mostly the same as a comment, but you also have to pick a title and a category.

‘Pokitto Programming’ is probably fine for programming questions, unless they’re specifically about C++ then they belong in ‘C++ Programming’ .
(I think, I’ll have to double check this.)

Don’t worry if you aren’t sure what category something should be in, if you pick the wrong category a long-time user or a moderator can recategorise it afterwards.


How to format code:

Inline code:
`this` becomes this

Block code:
```cpp
this
```
becomes

this

The forum uses Markdown, there’s a ‘cheatsheet’ here.


Your code problems:

@Rakkachi already covered them, but to be completely clear:

Firstly it is actually println and not printIn, unfortunately certain fonts don’t make the distinction very clear. ln is short for line, so println is basically supposed to be printLine.
(Personally when writing code I avoid using contractions for this exact reason.)

This (and several lines like it) are missing brackets (also called ‘round brackets’ or ‘parentheses’):

if mygame.buttons.leftBtn()) mygame.display.printIn("Left");

It should look like:

if (mygame.buttons.leftBtn()) mygame.display.printIn("Left");

Some stuff about if:

In C++ the if statement can be structured several ways.
The way used here only works if there’s a single statement afterwards.
(A statement is basically an if statement, a while loop, a for loop, a do loop or almost anything that ends in a ;.)

if(condition)
  statement;

If there’s more than one statement then you need curly braces, like this:

if(condition)
{
  statement;
  statement;
}

But the amount of space around the important parts can vary.
So these are also valid and (debatably) easy to read (so you might see them in other people’s code):

if(condition) statement;
if (condition) {
  statement;
}
if (condition) { statement; }

And these are valid, but not very easy to read:

if       (condition)     statement;
if(condition)statement;
if (condition) {
statement;}
2 Likes