Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
Arduino Coding Tips
Coding Errors Will happen - Problems are expected!
Be patient - Don’t panic, get frustrated or give up!
Analyzing and resolve computer coding is where the learning really begins!
Embrace the learning
1) The code on the Wizrobotics.com site is Valid - It Works!!!
Many other students have used the codes successfully
2) You must write the code exactly as provided
3) Code MUST VERIFY without errors before it can be uploaded
4) There are 2 entires of code for some actions
Example: Flashing LED
Need 1 line of code to turn on (HIGH) the LED
Need 1 line of code to turn off (LOW) the LED
Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
On and OFF results in a flashing LED
Duration is the length of time the LED stays on or off -
The durations can be different for length of time
For example: (Note: This example Code is incomplete)
An LED can stay on longer than it stays off
Or one wheel stays on longer than the other wheel which will impact how the robot turns
Example of different durations:
digitalWrite(led1,HIGH);
delay(2000);
digitalWrite(led1,LOW);
delay(500);
//LED will turn on for 2 seconds and then turn off for half a second
//Then the code will loop or repeat itself until given different instructions
Remember:
1000m/s = 1 second in time
2000m/s = 2 seconds in time =
500m/s = Half a seconds (.5) in time
Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
2 Common Types of Coding Errors
1) Syntax errors
Errors in spelling, capitalization and/or punctuation
Examples: Loup instead of loop
Missing brackets - regular brackets of curly brackets
Brackets not on the correct line of the code
2) Logic Errors
Robot code uploads but does not result in the expected outcome
Example: LED doesn’t blink, Duration of a blinking LED is not as expected
robot turns left instead of right, Wheels turn opposite direction
unintentionally
Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
Typical Coding Errors - Remember errors are expected and where the learning starts
Syntax Errors - the most common errors
Breaking down the code
// 2 forward slashes - the “human language” that follow will describe what a line or
section of code is expected to do. The Arduino computer program will ignore the slashes
as part of the code. It is only meant for humans to read
Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
Syntax Errors Cont’d
{ } Squiggly Brackets define the beginning and the end of a section of code
a) Required for the void set-up section
b) Required for the void loop section - actual commands for the robot to follow
{ bracket facing left is the start of the section of code
} bracket facing right is the end of a section of the code
Brackets must both be positioned correctly
Begin setup
code
End setup
code
Begin loop
code
End loop
code
( ) Take note of the regular brackets ( )
Brackets must be included and in correct position in the code
Electronics/Robotics Unit TIJ1O0 - 2019 - 2020
Syntax Errors Cont’d
Common Spelling Errors:
loop NOT loop Incorrect spelling
loop NOT Loop Incorrect capitalization
digitalWrite(1, High);
NOT Capitalization errors (multiple)
Digital write(1,high);
; The semi-colon is required at the end of each line of instructions
Often it is left off one line and the code then fails to verify