CxD Archive
Code: Robotics
Code: Robotics
  • Code Introduction
  • TUTORIALS
    • A. Meet Your Robot
      • A-1 Circuit Board
      • A-2 Motors and Wheels
      • A-3 Other Components
    • B. Hello World Test
      • B-1 Arduino Code Editor
      • B-2 New App Template
      • B-3 Global Variable
      • B-4 Setup Function
      • B-5 Loop Function
      • B-6 Upload App to Robot
      • B-7 Add Sound
      • B-8 Use Button
    • C. Driving and Turning
      • C-1 Driving
      • C-2 Turns (Pivoting)
      • C-3 Test Wheel Encoders
      • C-4 Drive for Specific Distance
      • C-5 Pivot By Specific Angle
      • C-6 Drive Straight Continuously
    • D. Detect Objects in Path
      • D-1 Test Mechanical Bumpers
      • D-2 Detect Collisions
      • D-3 Test Ultrasonic Sensor
      • D-4 Avoid Collisions
    • E. Detect Lines on Surface
      • E-1 Test IR Line Sensors
      • E-2 Follow Line
      • E-3 Avoid Line
      • E-4 Count Lines Crossed
      • E-5 Follow and Count Lines
    • F. Detect Other Conditions
      • F-1 Detect Surface Drop-Off
      • F-2 Test Accelerometer
      • F-3 Detect If Upside-Down
      • F-4 Detect If Bumped
  • REFERENCES
    • Arduino Language
    • Arduino Code Editor
      • Create New App
      • Save and Rename App
      • Include RedBot Library
      • Verify App Code
      • Upload App to Robot
      • Download Copy of App
      • Use Serial Monitor to View Data
      • Robot Demo App Template
    • Physical Inputs
      • Push Button (D12)
      • Mechanical Bumpers
      • IR Line Sensors
      • Wheel Encoders
      • Accelerometer
      • Ultrasonic Sensor *
    • Physical Outputs
      • LED Light (D13)
      • Speaker (Buzzer)
      • Motors
    • Robot Behaviors
      • Producing Alerts
      • Driving
      • Turning
      • Detecting Objects
      • Detecting Lines
      • Detecting Other Conditions
    • Navigation Modes
      • Distance Navigation
      • Line Counting Navigation
      • Line Following + Counting Navigation
      • Autonomous Navigation
  • LINKS
    • Robotics Project Guidebook
    • Arduino Create Web Editor
    • Arduino Language Reference
    • RedBot Experiment Guide
  • ELEGOO Experiments
    • Getting the Arduino IDE
    • Starting with the `techCar` Template
    • Template Code for techCar.ino
    • Template Experiments
    • Reinstall the Original Remote App Functionality, If Needed
Powered by GitBook
On this page
  • Open Code Editor
  • Add Comment
Export as PDF
  1. TUTORIALS
  2. B. Hello World Test

B-2 New App Template

PreviousB-1 Arduino Code EditorNextB-3 Global Variable

Last updated 6 years ago

An Arduino program (or app) is also referred to as a "sketch" because the Arduino language is designed to allow you to quickly create a program — just like a sketch is a quick drawing.

This guidebook will primarily use the term "app" but just keep in mind that program, app, and sketch all mean the same thing when coding in Arduino: a set of coded software instructions to control the operation of a computing device (which is your robot, in this case).

Open Code Editor

If you haven't already done so, open your Arduino code editor:

  • Arduino Create web editor: .

  • Arduino IDE desktop editor: Double-click the Arduino application icon on your desktop.

If you are using your Arduino code editor for the first time, it should display a new app template by default. The new app template will have some starter code automatically inserted.

CREATE NEW APP: If you do not see a new app template when you open your code editor, you can follow these instructions to .

If you're using the Arduino Create web editor, your new app template will probably look like this:

/*



*/



void setup() {

    

}



void loop() {

    

}

If you're using the Arduino IDE desktop editor, your new app template will probably look like this:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The starter code in the app template has two core functions that all Arduino programs must include:

  • Setup Function — which will run only one time when your program first starts. Code statements added within the setup() function perform one-time startup actions such as: set the pin modes for the device's inputs and outputs, initialize certain settings, etc.

  • Loop Function — which starts to run after the setup() function is completed, and then repeats itself in an endless loop (until the device is turned off). Code statements added within loop() function perform the main tasks of your device's program.

During this tutorial, you will be adding certain code statements within the setup() function and within the loop() function. You'll also add certain code statements outside of these two functions.

Here are two rules to follow when coding an Arduino app:

  1. Your app must have a setup() function and a loop() function — even if there are no code statements within these functions.

  2. Your app cannot have more than one setup() function — or more than one loop() function.

Add Comment

It's recommended to add a comment at the very top of your code to list a title for your app and any other information that might be helpful to you or to anyone else reviewing the program code.

Comments can be embedded throughout a program to provide information or to help clarify portions of the code. Comments are just written in plain language (instead of using code). Any comments added to the program are ignored when the program is compiled and uploaded to your robot.

A single-line comment starts with two forward slashes, like this:

// this is a single line comment

A block comment uses a forward slash and asterisk to mark its beginning and end, like this:

/*
this is a block comment
you can type as many lines of info as you want
anything written in a comment is ignored by the code editor
*/

Add a block comment at the beginning of your app code before the setup() function.

  • If you're using the Arduino Create web editor, your new app template already has an empty block comment at the beginning of the code, which you can use.

Inside the block comment, list your app's title, your team's information, your teacher's name, and your class period. (Your teacher might have specific information to list in this block comment.)

For example, your block comment might look like this:

/*
Hello World
Team 3 - Destiny, Katya, Miguel
Ms. Hopper - Period 2
*/

A comment can be a or a .

Log in using your Arduino account
create a new app template
single-line
block (multiple lines)