My arduboy is acting odd [solved]


(Nolan) #1

So, I program for the arduboy very casually. I mostly just play games on it and sometimes make code that generates patterns, or something else thats super basic. This time I wanted to try finishing an actual project for the thing. As I’m working on it yesterday it started acting odd. The arduboy would restart roughly every 12 seconds.

In addition, I can’t upload anything else to it.

I’ve tried:

  • using different cables
  • Kevin’s restart tutorial
  • uninstalling and reinstalling drivers
  • uninstalling and reinstalling arduino

The code that I have (And what presumibly started all this) manipulates a couple of large arrays of objects.

The issue started when I tried to move a bit of code from the maingame() to its own function, placeunit()

any ideas? Also, let me know if I’m getting terminology wrong, or if there’s something I can do to better with my program. I’m still learning.


[WIP] March - a "tug of war" game
(Stephane C) #2

Is it doing it even when it’s not plugged in?


(Matt) #3

I find when I get in this state, it usually means I’ve written to memory I didn’t mean to, like beyond the end of an array. What happens when you do that just depends on what that memory is being used for. Sometimes nothing major happens, and sometimes the Arduboy crashes.

If you run your app in the emulator, you’ll likely see the emulator crash. If so, it is likely a similar issue.


(Nolan) #4

Vaprirics yea it happens when it’s off the cord

city41 I’ll give the emulator a shot once I get a chance to sit down at my comp again. do you have a link to one?

Thank you guys for the quick response. I had to go to work so I’m not able to give the specific error code, but it says that an error occurred whenever I try to upload new code. Whether its mine or someone else’s.


(Pharap) #5

InB4 it’s a stack overflow… :P


First thing’s first, it’s nothing to do with placeUnit.


(Pharap) #6

Found your problem:

When you’re iterating through AllyUnits, you’re checking for x <= UNIT_COUNT instead of x < UNIT_COUNT.

Arrays are 0-based.
A 25 element array has indices running from 0 to 24.
I.e. 25 is an invalid index.

(Also UNIT_COUNT should be const or constexpr.)


The moral of this story is:
put warnings on verbose, read the warnings, try to eliminate all warnings, they’re usually bugs.

(There’s a number of other issues with the code, but none that I can see that are actual bugs,
mainly redundancy and stylistic issues, like using and instead of && and using == true.)


Do you mind if I ask, have you been learning mainly from looking at crait and filmote’s code and/or tutorials?


Also, I played around with the code for a bit and managed to cut it down to around 10454 bytes (36%) of program storage space and 1828 bytes (71%) of dynamic memory, but then I realised you haven’t put a licence on it, just a copyright notice, so I can’t publish my edits.


(Nolan) #7

I didn’t realize I wasn’t use verbose, I will in the future. I was planning on cleaning things up after I had some semblence of a working project. but I see now thats probably is not the best way to go about it.

I’ve read Crait’s tutorials, but I haven’t seen filmotes.The bulk of what I know comes from my background in java that I learned back in college.


(Simon) #8

I don’t use that key buffer stuff and I have never used arduboy.drawSlowXYBitmap() in my life! :slight_smile:

@Nolan_Maxwell_Gams there is a arduboy.justPressed( ... ) method in the Arduboy2 library. It will save you a lot of leg work when trying to determine whether a button has been pressed or not.

https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/classArduboy2Base.html#acab90d5bfbcb9a65841cd9f812dbbf97


(Pharap) #9

Sadly it’s turned off by default to avoid scaring beginners.
You can turn it on in preferences.

I’m guessing that’s where you learnt about and, or, == true, == false and possibly byte and boolean and the outdated ‘button buffer’ technique?

Typically && and || are used instead of and and or (like in Java).

if(x == true) is redundant because x == true evaluates to just x so you can use if(x) instead, as in if(arduboy.justPressed(A_BUTTON)).
Likewise if(x == false) is redundant because it evaluates to !x, so you can use if(!x) instead, as in if(!arduboy.nextFrame()).
(Again, this is the same in Java.)

byte and boolean are Arduino-specific types that aren’t available in a non-Arduino environment.
It’s better to get used to using uint8_t and bool.
bool is a fundamental type, while uint8_t comes from a library, but that library is standardised.

As for the ‘button buffer’ technique, that’s just an archaism from the days when the original Arduboy library didn’t have justPressed, but now most people use Arduboy2, which does have justPressed (and justReleased).

No, but you do have a habit of using x instead of i in your for loops. (e.g. here and here.)

@Nolan_Maxwell_Gams and you are the only people I’ve ever seen do that (when the thing being manipulated doesn’t actually involve x and y coordinates).

That’s one of the many changes I made on my local copy.


@Nolan_Maxwell_Gams
if you do decide to licence your code then I’ll show you the rewrites I did so you can decide if you’re interested in any of them (and if you are, I’d be happy to explain the rationale behind them).


(Nolan) #10

The copywrite notice is just a holdover from one of my old professors. The same goes for using ‘x’. I’ve just been doing it outta habit, I would love to see your changes! How would i go about that? Just getting rid of the comment at the top?

Thank you for your time, and I’m sorry I’m so slow to respond. I’ve been way busier than I thought I would be this weekend.


(Simon) #11

I broke out of that Fotran-77 habit many years ago. X is the great unknown, i looks like a 1!


(Pharap) #12

It’s a little bit more complicated than that to licence code.
Adding a licence is easy, but picking one can be difficult,
it depends what you want to allow people to do with it.

Most people who publish Arduboy games pick either MIT, Apache 2.0 or BSD 3-clause.
All of them allow people to copy and modify your code as long as they retain the licence and copyright notice, and all of them state that your code has no warranty (which is standard for almost all licences).

Apache 2.0 requires people to document the fact the code is modified if they modify it.
BSD 3C explicitly prevents people from using the names of any contributors to the code to promote any modified versions of the code (though this is probably implicit in most countries).
MIT doesn’t have either of those restrictions.

There’s lots of other licences that have different levels of restriction, but those three are the most popular.

The only licence I’d advise against is the GPL because it prevents people from mixing your code into theirs unless they publish their code under the same licence.
With the other ones, people are free to take bits and pieces of your code if they keep the licence notice to indicate that part of their code is licenced differently.

Whatever you decide to do, any licence is usually better than no licence, because not having a licence implies that other people can’t actually do anything with the code other than look at it.

Strictly speaking, most people who don’t use a licence aren’t likely to try to take legal action against someone who does use their code,
but using a licence makes it clear to people what is and what isn’t allowed and gives people assurance that you won’t change your mind about it later.

Sorry if that’s a lot to take in.

There’s no rush, I understand that a lot of people have commitments.
I won’t get bored waiting, I have plenty to do myself.

Besides which, as long as my opinion is valued, I’m always happy to help people.

The i habit or the x habit?

I must admit, I sometimes try to use index instead if I want to be particularly well documenting.

Get a better font :P


(Simon) #13

Fortran had a nasty feature called ‘implicit typing’ where you didn’t have to declare variables before use. If you didn’t declare them, the compiler assumed anything starting with and i, j, k, l, m or n was an integer - the rest were considered to be real.

Or better eyesight.


(Nolan) #14

So, I managed to unfutz my Arduboy. I transferred the ‘hello world’ example code, while the arduboy was in flashlight mode. This made the screen bug out. but after that, I was able to upload another game with no problems.

I looked through the licenses, and I’m going to go with the MIT license. Would It be better if I made a new thread in Games or continue the conversation here?


(Pharap) #15

Oh, I vaguely remember reading about that when researching older languages.

I don’t think anyone’s ever repeated that mistake since.
(BASIC and Perl come close with sigils, but you can’t accidentally use a sigil (usually) so it’s slightly less painful.)

Even then, that’s robably not the worst language feature I’ve ever seen.
There’s a nice plethora of oddities here.


If your problem has been solved then I think we can mark this as solved and you could create a new thread.

If your game is still a work in progress, best mark it as ‘WIP’ in the title though.

Excellent.

A useful tip:

On GitHub if you do ‘Create new file’ and try to create a file called LICENCE or LICENSE (without any extension) a button will appear that will let you chose a licence.

You’ll then get taken to a screen that has various licences to choose from.

Once you’ve picked MIT you fill in your name and the year on the right hand side and then click ‘Review and submit’.

You can then review the licence text.

And before you submit, I’d recommend chosing ‘Commit directly to the master branch’ if you don’t know about PRs (‘Pull Requests’) and branches yet.

When you’ve done that, just hit ‘Commit new file’ and you should end up with a licence file and a little icon near the top of your repo indicating the licence.

This is what the licence looks like on one of my repos where I’m using Apache 2.0: