**Ok, what next?**

With the 7 LEDs on the P.O.V., the speaker and the switch, we can make various patterns and combine them with weird sounds. The switch can be used to turn the sound on and off.

But first, let's look at the Blink sketch a little more.

C++ has a number of ways to manipulate numbers. When we create a program, we often need to count how many times something happened. We could do this with the commands:

int cntr = 0; // Creates an integer variable called "cntr" and initializes it to 0.

cntr = cntr + 1;

This should be pretty easy to understand. But, adding numbers to a variable is a very common operation, so C++ added a shortcut version of the command that looks like:

cntr += 1;

This just means "add 1 to the current value of cntr".

Even this command can be simplified. The following command means "increment cntr by 1".

cntr++;

The C++ command set gives us these commands:

++ // Increment

+= // Add some value to the variable

*= // Multiple the variable by some value

/= // Divide the variable by some value

-= // Subtract the variable by some value

!= // Invert the variable, so that TRUE becomes FALSE

Even more specialized operations include >> and <<. ">>" is "shift right". If we consider the decimal number "6" in its binary form, it looks like:

0110

If we shift the bits of the number one position to the right, we get

0011

Which is the decimal number "3". In effect, ">>" divides the number by 2. We can expect that shifting decimal number 3 to the left to have the opposite effect; i.e. - that of multiplying the number by 2.

>> // shift right

<< // shift left

Now, if we keep looking at our numbers in binary form, we can see that each position is either going to contain a "0" or a "1". Sometimes, we want to check individual bit positions one at a time. We do this with what's called bit-wise operations.

& // Bit-wise AND

| // Bit-wise OR

~ // Bit-wise invert

cntr = 0b0111;

cntr = cntr & 0b0100;

In the above example, we are checking whether bit 3 is 1 or not. If it is a 1, then the binary value 0b0100 will be stored to cntr. What's happening is that the bit value in each position of cntr is being "ANDed" with the corresponding bit of the constant binary number "0100". With an AND operation, 1 AND 1 results in 1. 1 AND 0, 0 AND 1, and 0 AND 0 all result in 0.

cntr = 0b1011;

cntr = cntr & 0b0100; // Will store 0b0000 to cntr

The "~" character for bit-wise invert turns each 0 to a 1 and vice versa.

cntr = 0b1010;

cntr ~= cntr; // Will store 0b0101 to cntr.

Sorry about the long explanation. Just remember that a BOOLEAN variable only uses the values 0 and 1 (TRUE and FALSE/ HIGH and LOW), and doing the following:

boolean display_val = HIGH;

display_val != display_val;

causes the contents of the variable to toggle between 1 and 0. This means that we can make the Blink program more interesting.

// Blink, ver. 2

boolean display_val = LOW;

int ledPin = 13;

void setup() {

pinMode(ledPin, OUTPUT);

}

void loop() {

display_value != display_value;

digitalWrite(ledPin, display_value);

delay(1000);

}

Return to the Index

Learning about Looping