Speaker Beep

Let’s start with basics- beep a single tone on speaker on repeat to learn about loops!

Code

The code in the editor below already works Just plug in your Code Speaker and press upload to see what it does! Tinker with it and make changes to see what each line of code controls.

// Beep a tone on the speaker once a second void setup(){ pinMode(10,OUTPUT); //Set the speaker as an OUTPUT } void loop(){ tone(10,400); //Play a tone of 400 hertz on pin 10 delay(1000); //delay the program for 1 second noTone(10); //Stop the tone on pin 10 delay(1000); //wait 1 second before the loop starts on line 6 again } // (c) 2018 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

Can you complete the challenge? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

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: Loops

Computers are great at doing things over, and over, and over, and over again. That’s what happens inside your void loop() function in this program. The computer reads your program from top to bottom, so it reads line 8 before line 9. However, it’s reading really fast, so you’ll usually have to add in some delay to make sure you can see how your program is working.

As you program more, you’ll get better at imagining which line of code is running as you watch your program work on Code Speaker. You might even start to think of the program more as a ‘circle’ than a single sheet of code.

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 coming up with an answer to a quiz question, try to run an experimental program or look at the example code to help you find the answer.

1. How many times would the code in the void loop run if you never unplugged Code Speaker?