// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/

Siren Toggle with Code Car

Blink Hookup: 1 LED in pin 13. Remember shorter leg of LED is ground.

Create separate buttons for each siren light so that you can control how they toggle on and off!


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 each siren light with a different button */ void setup(){ pinMode(5, OUTPUT); // Blue siren light pinMode(6, OUTPUT); // Red siren light pinMode(9, INPUT_PULLUP); //Button for the blue light pinMode(10, INPUT_PULLUP); //Button for the red light } void loop(){ //if the 9 pushbutton is pressed, turn the 5 LED on if (digitalRead(9) == LOW) { digitalWrite(5, HIGH); //the code between the braces is the 'action' } else { digitalWrite(5,LOW); //otherwise, turn the 5 LED off } if (digitalRead(10) == LOW) { //if the 10 button is pressed digitalWrite(6,HIGH); //turn on the 6 LED } else { digitalWrite(6,LOW); //otherwise, turn the 6 LED off } } //This curly brace ends the loop // (c) 2017 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!


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!


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: A Pattern of Statements

This lesson takes one step up from your previous 'if-else' statements, and it definitely contains the most curly braces so far! If you showed this program to someone who had never seen code before, they'd be amazed (and maybe confused). You should be seeing the patterns in the programs at this point and understanding that, although there are lots of syntax marks, line breaks, and strangely typed words in the code, you're really looking at a simple 'if' statement, repeated twice. If you played around with the challenges and the code, you probably saw that the 'if' statements aren't related to each other; that is, you can delete one 'if-else' statement and the code still works. Continue trying to see the patterns in a program when you first look through it. You might even notice that you can skim over some of the details of the example code at this point and quickly get the 'general idea' of the program just by looking at it.


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

What is the right way to string together four if and else statements?

What is the repeated pattern in this program?