Introduction to Arduino Programming 5: Troubleshooting Your Code

Why you buggin’: Troubleshooting your Arduino!

One of the most frustrating moments of your early programming career will be getting used to debugging your programs. I won’t sugar coat it, it’s not fun at first. Once you’ve mastered your tools though, you’ll feel more confident and better at squashing them.

A fun fact Grace Hopper, an incredibly influential computer scientist had one her “computer operators” (as they used to be called back in the day) remove an actual bug from a program that had “bugs”.

src: wikipedia

 

Red red console

Most common errors when you first program are syntax errors ! Remember that statements like:


Need semicolons!

Also functions like analogWrite need two arguments, one being the output snap you are turning on and the amount of electricity you’d like to send it.

As far as variables go don’t forget to telly your Arduino what type they are. They Arduino can’t read minds (YET) so you’ll need to indicated if your variable is an integer, number, or string of characters.

Lastly you’ll enter all your code in-between the setup and loop { } curly braces. Depending on what function you’re calling, code like Serial.begin(9600) will be in setup() and Serial.println(“hello”) will exist in loop().

Following the clues

Look closely at the bottom of the gif we just saw, you’ll see a little message about what and where things went wrong.

Sometimes the messaging can be cryptic, but it will give you some clues as to what went wrong syntactically (what language rules were broken) and where it happened.

You’ll be given a line number in the error message telling you where you can hunt down the bugs. You can find line numbers of your code on the bottom left of the IDE.

Note! Errors can happen at or before the line number indicated by the compiler / IDE.

 

Uploading remedies

This is so important that it needs it’s own heading! If you can’t your program to run or if your Arduino is stuck on “Uploading…” like this:

Then try some of these homemade remedies!

  • First things first: BEFORE you open the Arduino IDE , you have to create your circuit or at least turn the Arduino on. The IDE does not automatically pick-up your module sometimes.
  • Make sure the right port and board are selected, if you don’t know what that means look at this guide.
  • If you don’t see the correct port or board try turning it of and turn it on. Restart the Arduino IDE or restart your Arduino (with USB cable hooked-up to your computer):

  • If that doesn’t help try restarting your computer.
  • Look out for the lights you’ll know your program is being uploaded when you see this sequence of lights, it shouldn’t take more then a few seconds:

  • Don’t give up! Rinse, repeat and you’ll be coding in no time!

More Tips & Tricks

Tips & Tricks
Friday’s Tips & Tricks: The Logic Bits

Let’s explore some of our smartest modules: the logic modules! The double AND, double OR, NAND, NOR, and XOR all have specif...

Tips & Tricks
Friday’s Tips & Tricks: Fan Bit

The fan Bit is just what you’d think: a small electric fan tethered to a littleBits module. It’s great for those hot s...

Tips & Tricks
Friday’s Tips & Tricks: Light Sensor & Light Trigger

The light sensor and light trigger Bits are handy inputs that can turn your circuit on and off by detecting the brightness or dark...