Headlight On with Code Car

 
 

Turn on the LED light in the headlight position on your Code Car. Your car should look like the image above when your code is successfully uploaded!

Code

The code below already works and is ready to upload! Upload the code to Code Car and see what happens. Then, you'll 'take apart' the code to learn what each piece of the program does.

/* * Turn on the headlight */ void setup() { //the setup section of code starts here and all the code inside it runs one time pinMode(4,OUTPUT); //set your headlight LED as an OUTPUT } //the setup section of code ends here. void loop() { //the loop section of code starts here and all code inside here is repeated digitalWrite(4,HIGH); //Send power to pin 4 } //the loop ends here. When the code runs this line, it jumps to the top of the loop and repeats // (c) 2022 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Videos

Watch the videos for line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!

Challenges

How many can you complete? Change the code according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Concepts

These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.

New Concept: Getting Used to the Code Editor

When you're writing code, you need a program to do that, just like you need a program to write an essay. The code editor has the basic tools you need to write and upload your code.

If you look at the top bar of the code editor, the first button you see is "Restore." Clicking that button will erase all of your custom-written code and replace it with the example code that was in the code editor when you started. Use this button if you're lost or confused about an error you made, and you'd like to just start over with the example code. Beware! Once you click "Restore," your custom code is gone.

The second button is a drop-down menu for font size. Use this to help you see the details of your code by making the font bigger, or to see more lines of code by making the font smaller.

A drop-down menu in the middle of the code editor top bar shows you which port your Code Car is connected to on your computer. On Windows, this is usually a COM3 port or something similar. If that port is empty, ensure that Code Car is plugged in and the power light is on. 

On the right-hand corner of the code editor, you have the "Upload" button. This button will send your code from the editor to your Code Car. Make sure you press this button after each update to your code, or your code won't be running on Code Car!

In the bottom right corner of the code editor, you'll see little messages that tell you the status of your code. After an upload, for example, there will be a message that says "Upload complete!"

If there is an error in your code when you press "Upload," you'll see red lines appear in the code editor. Scroll down just below the code editor to see the errors that the computer found and use those clues to fix the bugs!

 

New Concept: Syntax!

Syntax is the set of rules you use to structure your programs. Though it uses some different characters, code syntax works the same way that you use commas, periods, paragraphs and chapters to structure a book. Computer code is very picky about syntax, but luckily there are only a few key things to learn.

Semicolons usually end a single function. You'll see them at the end of most lines of code.

Parentheses usually hold the specific information that a function needs to run. For example, pinMode needs a pin number and a mode. Those two things are grouped together inside some parentheses.

Commas separate pieces of information. You can't use a comma in a big number like 1,500,000 because the computer will think you have three numbers: 1, 500, and 000.

Curly braces usually hold multiple commands. These are the big 'chunks' of code and they will often have commands inside of them so an opening curly brace and a closing curly brace are not usually on the same line. 

Syntax errors are the most common mistake when you're starting out. Don't be too frustrated by them, but instead pay close attention to the patterns of syntax and don't copy/paste or delete syntax if you're not sure where it came from.

Quiz

If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.

1. Which syntax ends the entire 'void loop' function?




2. Which syntax ends a single code command?