Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

Wrong USB port selected

Wrong Board profile selected

Use of TX and RX pin while uploading the code

We are allowed to use the TX and RX pins as general purpose inputs or outputs (GPIO), which are pins 0 and 1 on the Arduino Uno. However, these pins are primarily intended for serial communication, and are in use when uploading to the board, or communicating with the serial port. So, avoid using them unless you really need to use every single GPIO pin on the board (perhaps you could consider a shift register instead)

Programming Errors

A missing semicolon at the end of a statement

In c++ you must have a semicolon ( ; ) at the end of every statement! Fortunately, the compiler will spot this one straight away. Another common typo is using a colon ( : ) instead of a semicolon ( ; ).

An uneven number of opening and closing brackets, or wrongly placed closing brackets.

This is another easy mistake to make, that can cause a real headache. The compiler will recognise that there is a missing curly bracket, but it has no way of knowing where you intended to place it. Typically the compiler will indicate that the bracket is missing at the end of the code block at the last possible place it could be placed. However, this is probably not where it should be! Take great care to go through your code line by line if you ever get a missing bracket message, and ensure it gets put back in the right place.

If you have your code split into multiple tabs in the Arduino IDE, then the compiler will often indicate that the bracket is missing from the wrong tab.

Equals and Comparison

‘=' is not the same as '==’.

A single equals sign is used to assign a value:

float y = 20.4;
Serial.println(y); // this will give you 20.4

Two equals is a comparator

if (test == true) {
  Serial.println("true"); 
}

You probably already know this, but it’s a very easy typo to make and it can be frustrating to spot in your code once it happens. One way to avoid the problem is to always use this shorthand version below, and stop using '==' altogether.

if (test) {
  Serial.println("true"); 
}

if (!test) {
  Serial.println("false"); 
}

Float and Integer Arithmetic

A float can store values with a decimal point, and integers can store only whole numbers. What you might not realise, is that integer maths works a little different to floating point maths. On the Arduino, if you divide an integer by an integer, the result is always rounded to give an integer result.

int x = 2;
float y = 9 / x; // the compiler only sees integers here, so it does integer math!
Serial.println(y); // this will give you 4.00! 

One way to fix the above example is to show explicitly that 9 should be handled as a float.

float y = (float)9 / x; 

Or like this

float y = 9.0 / x;