Sound Samples

This code is a demonstration of how you can combine code concepts, hardware, and built-in functions to make something unique out of a speaker and a button.

Code

/* * Press a button to activate various sound effects */ int pressCount = 0; //track presses on button to change the effect //Function to check for button presses void checkPress(){ if(digitalRead(12)==LOW){ pressCount++; delay(200); } } void setup() { pinMode(A5, OUTPUT); //speaker pinMode(12, INPUT_PULLUP); //button } void loop() { checkPress(); //run the checkPress function //rising pitch if(pressCount == 1) { //if pressCount = 1, start the rising tone for (int i = 0; (i < 1200); i++) {//play tones 0-1199 tone(A5, i); delay(5); checkPress(); //run the function to see if button was pressed if(pressCount!=1){ //if button pressed again, break the 'for' loop break; } // end the second if statement } // end the for loop } // end the first if statement //random tones if(pressCount == 2){ tone(A5, random(1,1201)); //Play any tone between 0 and 1200 delay(50); //pressCount is checked every 50 milliseconds } //falling pitch if(pressCount == 3) { for (int i = 1300; i > 30; i--) {//play tones from 1300 down to 31 tone(A5, i); delay(5); checkPress();//run function again to check pressCount if(pressCount!=3){ //if button pressed again, break the for loop break; } } } int pitch = 1 ; //tone value as it rises and falls int scale = 10; //the speed of the change of pitch. //rising and falling pitch while(pressCount == 4) { checkPress(); pitch = pitch + scale; tone(A5, pitch); delay(5); if(pitch <= 0 || pitch >= 1200){ scale = -scale; } } int lowTone = 10; //starting place for a rising tone int highTone = 1800; //starting place for a sinking tone //Low pitch rises as high pitch falls while(pressCount == 5){ checkPress(); tone(A5,lowTone); delay(50); tone(A5,highTone); delay(50); highTone-=10;// subtract 10 from highTone lowTone+=10; // add 10 to lowTone //reset the tones if(highTone < 10){ highTone = 1500; } if(lowTone > 1800){ lowTone = 10; } } //Turn off the sounds and reset presses to 0 if(pressCount > 5){ noTone(A5); pressCount = 0; } } // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Challenges

How many can you complete? Change the code and hardware according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Videos

Read the Code Walkthrough Text (click to open)

Concepts

New Concept: Combining Conditions with AND && and OR ||

Sometimes, you want to create the same outcome from two different actions. For example, you may want to turn on a fan if a room gets too warm or if there is smoke in a room. Rather than write two separate if statements, you can combine these conditions with a Boolean OR. It's simple- you simply type the first condition inside an if or while loop, then type the 'pipes' and type the second condition. After you've typed the conditions, you can close the condition portion of your loop. This can get complex when there are many parentheses involved, so remember the order of operations. The 'pipes' are found below the delete or backspace key on most keyboards.

In other cases, you may want to do an action only if two things are true at the same time. For example, you may want to turn off a light only when two buttons are pressed, but not either of the buttons alone. To combine conditions this way, you use the Boolean AND, which is two ampersands side by side: &&. Both conditions that you put in the statement must be true before the action runs.

New Concept: Not Equal To Comparator

You're probably familiar with most of the comparators you see in code- < , >, <=, >=, and = are all used in other fields. The not-equal-to comparator is more rare. When using this comparator in an if statement, the condition is true when the two things being compared are not equal.

This comparator is useful when you're not sure how a variable may change. Imagine you're holding a very precise level of light on a plant. You're measuring the light with a light sensor and you have an alarm to go off if the light level changes. You don't care if the light increases or decreases or the amount that the light changes- you just need to know it's changed. By using a != comparator comparing your reading and the perfect light value you need, an if statement will be true any time the light value doesn't match your perfect light value. 

New Concept: It's All Building Blocks

Code is complex, but it's made up of simple parts. You've seen how to combine them creatively to get something new and sometimes unexpected to happen. By paying attention to the basics and breaking down your problems until they're small enough to solve, you can create some wonderful things with code!

Quiz

If you're having trouble, try to run a program that can help you find the answer.

1. If you use “&&” between two condition in an 'if' statement, what are you doing?



2. What is the best way to build a complex program or piece of code?