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

Brake Pedal Light with Code Car

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

Code one of the buttons on Code Car to act as a brake pedal, turning on the brake lights when the 'pedal' is pressed!

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.

/* * Press button 11 for the brakes and brakelight of Code Car */ void setup(){ pinMode(7, OUTPUT); // Tail light pinMode(11, INPUT_PULLUP); //"Brakes" button } void loop(){ //if the pushbutton is pressed, hold the LED on if (digitalRead(11) == LOW) { //if the button is pressed digitalWrite(7, HIGH); //the code between the braces is the 'action' } else { // an else statement runs when the 'if' condition isn't true digitalWrite(7,LOW); //otherwise, turn LED off with a LOW command } } // (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: Inputs vs. Outputs

Up to this point, after you hit "Upload" for your code, all you could do is observe what it does. With the addition of buttons to your program, you now have the ability to interact with your code and change what Code Car does after you've uploaded code. This is also the point when you could show your Code Car to someone else and they could interact with it without being involved with the coding process. In computer science terms, the button is an 'interface' to your program, so that a simple action (pressing it) makes something complicated happen in the background, and then an outcome occurs (the light turns on!).

It's useful to think of inputs as things that send information into a program. Then the program can do something with that information. The do something is often a process that leads to an output.

 

New Concept: Decision Making

This program is the first time that your Code Car has 'made a decision' about what to do. To put it a different way, each time the void loop of the program runs, two different things could happen: the brake light could be signaled to turn on or it could be signaled to turn off. Almost any time there is an input to a program, there is a 'decision' being made. Imagine logging off of your computer. You go and find the 'log out' button and click it. Somewhere in the computer code, there must be a 'decision' made by the code so that "if 'log off' is clicked, run the log off process."

 

Styling Refresher

The if-else statements represent the importance of styling. The if statement is inside the void loop and the digitalWrite() statement is inside the if statement! The else statement is inside the void loop, but it's not inside the if statement. The example code is styled so that if you see a closing curly brace, you should be able to trace an invisible line straight upward to it's starting point. The closing curly brace of void loop, for example, is directly below the words 'void loop.'

Indentation also comes into play. When you see that something is indented, you can think of it as 'inside' something else. For example, the digitalWrite(7,HIGH); command is inside the 'if' statement, which is inside the void loop function. That may sound confusing, but that's all the more reason to style your code in a way that makes it more readable and clear. 

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 syntax of an 'if' statement?



What are the argument(s)-pieces of information- that the digitalRead() function needs?