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

To create a rolling die with a button and a single LED, you need to trigger a random event with your input and then show the outcome with your output. The dice can have 6 possible positions, so using blinks on the one LED can show the outcome of the ‘roll’.


/* * Press a button to 'roll a die' and get the outcome in blinks. * Placing loops inside loops is called 'nesting'. */ void setup() { pinMode(11,OUTPUT);//LED pinMode(A5,INPUT_PULLUP);//button } void loop() { //While the button is held down, 'roll the dice' by flickering the LED while(digitalRead(A5) == LOW){ digitalWrite(11,HIGH); delay(50); digitalWrite(11,LOW); delay(50); if(digitalRead(A5)==HIGH){ //if the button is released delay(500); //choose a random number 1-6 and blink the LED that number of times for(int blinks = 0; blinks<= random(1,7); blinks++){ digitalWrite(11,HIGH); delay(300); digitalWrite(11,LOW); delay(300); } //close the 'for' loop } //close the 'if' statement } //close the 'while' loop } //close the 'void loop' // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense


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!


Read the Code Walkthrough Text (click to open)


Special note about this program

In the example above, you can replace the while with an if statement- just delete 'while' and type 'if'. To complete the code challenge of adding a tone to the program while the dice is being shaken, you need a while loop if you want to declare your pitch variable within the loop. 

Why? When you declare a variable, it sets that variable's value. A variable declared at the top of the loop will be re-declared every time the loop runs. An if statement will continue to run the entire loop as the dice is 'shaken', result in constantly redeclaring the variable. In short, the tone won't ever rise.

A while loop, though, does not run the entire void loop while it's condition is true- it only runs the while loop. Therefore, a while loop can increase a variable even if that variable is declared in the void loop.

This is a good example of different approaches to the same result- you can also solve the challenge without a while loop- you just have to think creatively!

New Concept: While Loops

A while loop has one condition and a set of actions that happen when that condition is true. A while loop repeats until the condition between the parentheses is false. This statement is most useful when you're not sure what values will be in the condition.

The if statement and while loop are closely related and often interchangeable. There are some characteristics of a while loop, though, that make it unique and useful. The while loop doesn't check any conditions outside itself once it has started.

For example, in an earlier program, you could hold down a button to raise the tone on a speaker. When you released the button, the tone would fall. What if you wanted to ensure the tone went all the way to its minimum before it could rise again? That is difficult with an if statement. Whenever you press the button, the if statement will trigger and the tone will rise. With a while loop, that if statement is not checked until the while loop is complete. Using a while loop, your code would follow the logic: if button pressed, raise tone. Else, while the tone is above the minimum level, bring it down to the minimum level. The while loop cannot be interrupted by an outside line of code. 

While loops are also good to use when you're not sure the starting point of a loop. With a  for loop, you define how many times it will run. With a while loop, it will continue until the condition is false. Using the earlier example, imagine someone held the button down until the tone was at 2000 hertz. To get back to 0 hertz in steps of one, the while loop needs to run 2000 times. Now imagine that someone held the button down until the tone was at 100 hertz. Now the while loop only needs to run 100 times to bring the sound back to 0. You don't have to define the number of times a while loop needs to run.

New Concept: Nesting Statements

There aren't that many building blocks to most programs- loops, statements, variables, conditionals- but they can be combined in infinite ways. When a code statement is run inside another code statement, that is called nesting one statement inside another. The more decisions that are based on each other in a program, the more complex nesting can get.

Nesting is necessary for some programs, but there are tips that can help you keep track of your logic and code. Indentation is one of them. You can think of most of your program's code as nested inside the void loop(). That's why the code inside void loop is indented by one tab. When you create an if statement, the action of the statement should be indented one tab further than the if statement. When your if statement contains a for statement, it should be indented by one more tab than the if statement. 

In general, code indented further to the right on your screen depends on many things happening before it will run. 

When you create a new statement that doesn't depend on the code above it to run, remove indentations until that line is indented one tab further than the line it depends upon. 

You will see some programs that are poorly indented and they are very hard to follow. Try your best to keep up with your indentation to be a clear programmer.

You should also indent the closing curly braces of your statements to the same level as the word beginning that statement. For example, the closing curly brace of an if statement should always line up vertically with the letter 'i' that starts the if statement. The closing curly brace of a for loop should line up vertically with the letter 'f' that starts the loop.

This technique will keep your code easy to read and change. You can trace a vertical line from any closing curly brace up to the statement it closes. If you have many closing curly braces, it may even be a good idea to comment them so you know what they close. 

Nesting is purely stylistic- it doesn't matter to the computer if you nest your code or not. However, it's a practice that can save you many hours of frustration if you learn to use it.

New Concept: random() Function

The random function is built in to the Arduino software and there is some variety of it available in most programming languages. Give the random() function two numbers and it will return a number between those two numbers. The random function includes the lowest number you provide and excludes the highest number you provide, so keep that in mind. 

To use the random() function, place it wherever in the code you want the random value. Imagine that "random(x,y)" will be replaced with a number between x and y.


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

1.What are the possible numbers that random(1,5) could return?

2. When you create code statements inside other code statements, what is that called?