// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/
 
Blink Hookup: 1 LED in pin 13. Remember shorter leg of LED is ground.
 

Hold down a single button to turn on both sirens of Code Car. Release the buttons to turn them off. 

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 both siren lights at once with a single button press. */ void setup(){ pinMode(5, OUTPUT); // Blue siren light pinMode(6, OUTPUT); // Red siren light pinMode(9, INPUT_PULLUP); //Button to activate the siren lights } void loop(){ //if the 9 pushbutton is pressed, turn the 5 and 6 LEDs on if (digitalRead(9) == LOW) { //the code between the braces is the 'action' digitalWrite(5,HIGH); digitalWrite(6,HIGH); } else { digitalWrite(5,LOW); //otherwise, turn the 5 LED off digitalWrite(6,LOW); //also turn LED 6 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!


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: Growing Your 'if' Statements

This lesson helps you build on the powers of the 'if' statement. If you completed the challenges, you now see that the 'if' statement can be used to combine a number of commands and control multiple outputs with a single button press. As the number of lines in your programs grows, try to keep looking for patterns or 'reversals' of the concepts you're using. For example, the challenge of keeping all of the lights off when the button was pressed has more to do with patterns and code structure than it does with any new code concept. 


Quiz

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 maximum number of code commands you can run inside an 'if' statement?




An else statement only runs when_____________________