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

Hack the Code Car!

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

The ultimate Code Car creation! This 'hack' page doesn't cover any specific topics, but it gives you the chance to dig through some working code and tinker with how it functions. Don't be afraid to change numbers and break things- the "Restore" button in the code editor will bring the Code Car back into a working state. 

Watch the walkthrough video if you want a line-by-line explanation of what's happening in the program, but if you want to dig into each concept step-by-step, we recommend checking out the lessons.

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.

/* * Code Car with a headlight switch, a siren light and tone that fades up and down, a horn, and a brake pedal that lights the brake light */ int headlight = 0; //an integer variable called 'headlight' is equal to 0. void setup() { // //Each button needs a pinMode command to be used as an input pinMode(8,INPUT_PULLUP); pinMode(9,INPUT_PULLUP); pinMode(10,INPUT_PULLUP); pinMode(11,INPUT_PULLUP); pinMode(2,OUTPUT); //the speaker pin is set as an output //Each LED light needs a pinMode command to be used as an output pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); } //this curly brace ends the 'setup' section of the code void loop() { //everything starting here until the end the program repeats over and over //An if-else statement for the 'horn' if (digitalRead(10) == LOW) { //if button 10 is pressed... tone(2, 350); //play a tone of 350 hertz on pin 2 (speaker) } else { //otherwise if button 10 is not pressed... noTone(2); //play no tone on pin 2 } //an 'if' statement and two 'for' loops to fade the siren lights up, then down. if (digitalRead(9) == LOW) { //if button 9 is pressed... //create a variable called 'i' that is equal to 0. Until it equals 255, run the 'for' loop and increase 'i' by one each time the loop runs for (int i = 0; i < 256; i++) { analogWrite(5, i); //Use 'i' as the brightness for LED 5. i starts at 0, then increases by 1 each time the 'for' loop runs, until 'i' equals 255. analogWrite(6, i); //same effect on LED 6 as LED 5 in line above tone(2,(i*10)); //use a value of (i*10) for the siren tone. i will play as 0, then 10, then 20, up to 2550 hertz delay(3); }//this closing curly brace ends the 'for' loop started on line 37 //create a NEW variable called 'i' that equals to 255. As long as 'i' is greater than or equal to 0, decrease it by 1 each time the 'for' loop runs. for (int i = 255; i >= 0; i--) { analogWrite(5, i); //Use 'i' as the brightness for LED 5. i starts at 255, then decreases by 1 each time the 'for' loop runs, until 'i' equals 0. analogWrite(6, i); //same effect on LED 6 as LED 5 in line above tone(2,(i*10)); //use a value of (i*10) for the siren tone. i will play as 2550, then 2540, down to 0 hertz. delay(3); } //this closing brace closes the 'for' loop started on line 43 }//this closing brace closes the 'if' statement started on line 36 //an 'if' statement for the headlight switch if (digitalRead(8) == LOW) { //if button 8 is pressed... headlight = 1 - headlight; //make the value of the 'headlight' variable either 1 or 0, the 'opposite' of what it previously was digitalWrite(4, headlight); //use the headlight variable to send a value of 1 (HIGH) or 0 (LOW) to the headlight LED delay(250); //wait 250 milliseconds before switching the variable again. } //'brake pedal' if (digitalRead(11) == LOW) { //if button 11 is pressed... digitalWrite(7, HIGH); //turn on the brake light LED } else { digitalWrite(7, LOW); //otherwise, turn the brake light off. } }//curly brace to end the loop // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Video

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