Ok so basically the 0b prefix tells the compiler we are going to be typing 1's and 0's instead od actual script? Then I assume the six digits that follow the prefix are my outputs?
Sort-of. The 0b means that you are writing a number as binary digits (bits) rather than decimal digits (0 through 9.) You are defining a piece of data that describes the desired pattern. Rather than write a lot of code to do all of the explicit output control, you put the pattern in a data structure, and then write a relatively small generic piece of code to read the data and output the patterns.
The other part that I might be having trouble trying to grasp is this. Do I define my inputs and outputs with script in the setup and loop sections? Then just write the patterns in the way you suggest? Or am I missing something?
Yes, set up the pins in setup(). And define the patterns you want. What you're missing is the code in loop() that will read the patterns and actually set the outputs.
I do agree, the way that the KITT pattern is redone makes it a lot easier to visualize in your head whats going on.
Easier to see what's going on, easier to write and modify, and a lot less total code.
To read the data and write the outputs, you will have to extract the output values from the data. The way it's all encoded as bits in a byte is a very efficient way to store the data. The downside is that it will take a bit more code and some bitwise boolean operators to extract the data. The "&" operator is a bitwise boolean AND: it takes two values, and does a bit-by-bit AND operation: for each bit, if the corresponding bits of the two operands are a 1, the result is a 1, else it's a zero.
byte a = 0b10101010;
byte b = 0b11001100;
byte c = a & b;
The result of this is c will be 0b10001000.
Another way to look at it, writing it like an arithmetic problem, so that all of the corresponding bits are aligned in columns:
The left-most bit is 1 because that bit is 1 in a and b.
The next bit is 0 because that bit is 0 in a, so it doesn't matter what that bit is in b.
The next bit is 0 because that bit is 0 in b, so it doesn't matter what that bit is in a.
The next bit is 0 because that bit is 0 in both a and b.
The next bit is 1 because that bit is 1 in both a and b.
And so on...
So how does this help you? It gives you a way to test the individual bits in each byte of the pattern. You need to do this to figure out what values to write to each output. There are many ways to code this up, but here is one way that is (relatively) easy to understand. Let's make a function that sets a single output:
void setOutputPin(byte pin, byte mask, byte pattern)
// Set the output "pin" depending on whether the "mask" bit is set in "pattern"
// To be useful, only a single bit in "mask" should be set at one time.
if (mask & pattern) // True if the same bit in mask and pattern are set
This function takes one byte of the pattern, a mask value describing the interesting bit in the pattern, and a pin number. The mask has a single bit set that is the bit corresponding to the LED indicated by pin. It does a bitwise AND of the mask and the pattern to see if the desired bit is set in the pattern, and sets the output HIGH if the bit was set, and LOW if it was clear.
Now, that takes care of one bit. How to output all of the LEDS in the current pattern byte? Well, it just takes calling the setOutputPin() function once for each pin, giving it the same pattern byte:
void SetOutputPattern(byte pattern)
// Set all of the outputs according to the pattern byte
setOutputPin(LED1, 0x00000001, pattern);
setOutputPin(LED2, 0x00000010, pattern);
setOutputPin(LED3, 0x00000100, pattern);
setOutputPin(LED4, 0x00001000, pattern);
setOutputPin(LED5, 0x00010000, pattern);
setOutputPin(LED6, 0x00100000, pattern);
For each output pin (LED1 through LED6 are assumed to be defined as pin numbers) it calls setOutputPin to set that pin number, using a mask value that picks out the bit that corresponds to that pin, and the current pattern byte that was passed in as a function parameter. Now, all that's necessary to do is loop through all of the bytes in the pattern array, calling setOutputPattern() for each byte, and waiting an appropriate time before calling it again. Then, define several pattern arrays, and come up with some code to select a pattern to display, and then display that pattern. And, of course, while looping through the patterns, you will need to check if the button is pressed (and debounce the button press!) to select the next pattern.
I'll give you a chance to let all of this sink in, and maybe come up with some of this additional code before I totally overload you with more new concepts. Take some time to read and re-read all this until it starts to make sense, and feel free to ask questions.