Bit behind due to the fact I managed to delete the last post like a total spanner. ANYWAYS #dafttwat
Digging deeper into the Arduino code I showed a bit of in the last post, I want to talk about each section of the code and how it effects the system.
Here you see two sections of code, aspects of which will be familiar to anyone who uses Arduino. The first thing that you always have to do with any Arduino project is define your pins (that is unless you are doing pure maths equations, geek). We do this by using the "int" prompt, and then the corresponding name we want to give the pin, followed by "Pin" = "Corresponding Pin Number".An example of this would be...
int ledPinR = 9;
Next, we have the void setup. This area of Arduino code controls the starting conditions for any patch. We use a serial input res of 9600.
As the Arduino has both Analogue Inputs and Outputs, we have to define which of the MSGEQ7's in's and out's go to the Arduino.
In order to ping the MSGEQ7 we also have to set the digitalWrite values in either Low or High states. As described by J Skoba and talked about earlier in the blog, we strobe the MSGEQ7 by seting our digitalWrite value High.
So to start the code off, we need the value Low (resetPin), as we want it to strobe when it first detects a signal when run. The reference voltage is also set at 5V, native to the arduino and enough to run the circuit.
The next section of code, which again will be familiar to Arduino Geeks, is the "Void Loop". Essentially this is a block of code that repeats indefinitely, in order to run your program.
As you can see, the first thing we do is set the strobePin value to High, and resetPin Low.
Next, we define the number of values we are looking for after the strobe is bounced. In this case, that is the 7 values of the MSGEQ7. Then, we have to define what the delay time is until the next strobe is bounced. As I have already talked about, this is now set to 25ms.
Then, we take the read rfom the analogue input pin of the Arduino, which is taking the feed from the MSGEQ7.
Next, we need to constrain this value. As discussed before, this value that comes in from the MSGEQ7, runs upto 1023 per band. In order to make it compatible with the colour theory discussed in the disertation and how LED voltages work, we need to constrain this value to within a range of 0-255 per band.
We do this using the "map" and "constrain" functions of Arduino coding. As you can see above, it is pretty simple to understand. We use these in combination to remap values that fall in between one range, into values that fall between a range either larger or smaller than the original.
All we need to do is then define which band of data we have analysed, constrained and remapped, to send to which LED colour. In this case we go RGB from Low to High, so band [0] - 63hz goes to Red, band [3] - 1000hz goes to Green and band [6] - 16000hz goes to Blue.
And that is that. We we apply colour theory in this practical manner, it turns out we dont need very complex FFT to generate every colour of the rainbow!
No comments:
Post a Comment