My code doesn’t work! How to fix it?

Back to let’s code

I can’t upload my code to Arduino…

Possible reason:

  • Missing semicolon at the end of any statement. All Statements must end with “;”. It tells complier that this is the end of a command.
  • Missing bracket either at the end or start of the loop or statement. All function must start with “{” and end with “}”. 
  • Calling local variables in any function. If you use a local variable outside its scope, complier cannot be recognized it. You can learn more about variable scope from Data type and variable page
  • Using undefined variables in a function. All variable must be defined before you used it. 
  • Missing libraries in the Arduino code. Libraries provide extra functionality for use in sketches. You need to import library when you want to use it in your code. You can learn how to install library from how to install Arduino libraries.
  • Use of TX and RX pin while uploading the code. Since Arduino use serial communication to upload the code, sending info through TX and RX pin will conflict with uploading.
  • Port selection is wrong for uploading the code. Arduino IDE won’t automatically select port or board. If you use a different port to connect Arduino or a different Arduino board, you must select them in IDE. Read Select board and port in Arduino IDE for more detail. 
  • Data cable disfunction. Data cable is important when you are uploading code. Use the original data cable from the kit to make sure the data cable connection is good. 
  • Port disfunction. Sometime the problem might not be your Arduino but actually is on your PC. Try to use a different port to upload your code, it might fix the problem.
  • Still updating…

I uploaded your code to Arduino but the system does work as I expect…

Here are some tips to solve this problem:

  • Check your hardware. The problem might not be your code but your hardware. Following Hardware Degguing Instruction to check your hardware, make sure your circuit is setup correctly
  • Using “Serial.print()” to read the variable.  A very useful tool when you checking your system is the serial communication. You can use to read your variable while your system is running. Find the variable reading that doesn’t make sense. List all possible cause, including both hardware and software. Slowly cross all probability by testing until you hit the problem.
  • Still updating…

If you still cannot solve your problem, ask your lab instructor for more help.