## Laser Harp : Hackathon 2017 |
## May 18th, 2017 |

For the second time, I served as a mentor for my high school/middle school’s annual Hackathon. Despite being extremely busy helping out with the incredible projects the students were building, I found time and was able to work with my friend Andrew to complete a Laser Harp. Here is a video demonstrating our harp

https://drive.google.com/open?id=0B2rOmItJ08iScVBTZVJEVVhoX0E

In this post, I will attempt to explain the basics of how this project was built. Hopefully this project inspires others and sparks some interest in both Electronic Instruments and Arduino as a whole.

– Arduino Mega

– (8) 5V lasers

– (8) photoresistors

– (8) 3k$\Omega$ resistors

We used the Arduino Mega because we needed 8 analog inputs and the Arduino Uno only has 6. We needed our lasers, of course, and the photoresistors and 3k resistors were be used to detect the breaking of the laser beam.

To sense how much light was hitting the photoresistor, we used a potential divider, shown below

To find the ideal value for the resistor, we first looked at the datasheet for the photoresistors. When dark, the resistance is around 10k$\Omega$, and when exposed to natural light, the resistance is approximately 1k$\Omega$. With $R_{pr}$ being the resistance of the photoresistor, the voltage across a potential divider is given as

$$V_o = V_i \cdot \frac{R_{pr}}{R_{pr} + R}$$

We wanted to maximize the difference between $V_o$ when the beam was shining and $V_o$ when the beam was broken.

$$V_{dark} – V_{light} = V_i \cdot \frac{R_{dark}}{R_{dark} + R}-V_i \cdot \frac{R_{light}}{R_{light} + R}$$

$$\approx V_i \cdot \frac{10000}{10000+ R}-V_i \cdot \frac{1000}{1000 + R}$$

Graphing this function as a function of R, we see that the largest difference occurs around $R=3000$. By using this value, we maximized the difference between our two readings, minimizing the chance that we would make an error.

Unfortunately, we didn’t take pictures while actually building, so we have to skip the construction step. It was pretty easy, though, as it’s pretty much just 4 pieces of wood screwed together in a rectangle. For the code, we used a program called Max/MSP. It’s a visual programming language that is extremely well documented and has very nice MIDI interfacing. MIDI, which stands for Musical Instrument Digital Interface, is the globally accepted protocol for electronic instruments (instruments that don’t actually have an acoustic chamber, essentially). We used the Arduino to read in analog data, and then used a serial port to relay that data into Max. Here is a look at the Max patch we wrote

It looks really complicated. Well, actually, *it IS pretty complicated*, but I’m going to ignore all the boring stuff, which eliminates some of the more complicated things. I’m going to focus mostly on two parts; the note generation section and the scale selector.

Let’s go piece by piece here and analyze what is happening. The user selects a starting note (C is MIDI note 60, C# is 61,… B is 71), and a scale pattern. It sets the starting note as $n1$, the note of the first string on our harp. That’s the left side of the picture. On the right, we have the scale setter. It uses the pattern for the scale shape selected (a major scale is 2212221). As it cycles through, the router object connects the next note to the correct string, going up by one for each note. By adding the interval width to the previous note, we build the scale note by note. At the bottom we see the notes for C Major, the default scale.

This is the subpatch that actually takes an analog signal reading and decides what to do. It takes the analog value (the 1 with the blue triangle below it at the top of the image) and puts it into two if statements. If it goes above the threshold, it triggers the note, but it uses a onebang object, which only sends 1 “trigger” message at a time. Once the signal goes below the threshold (i.e you unblock the laser), the note off is sent, and the note on onebang is reopened. This prevents the user from sending tons and tons of notes when only plucking the “string” once. The pack objects just insert the correct note number into a message that is then sent to the MIDI output.

This project was pretty awesome. It worked on the first go, which is always nice. In addition, I got to work with a bunch of awesome students who were pretty interested in what we were cooking up with a bunch of lasers. Andrew and I were extremely proud of the harp, especially given that we only had 2 days to complete it. As always, thanks for reading!

## Viète’s Formula |
## May 3rd, 2017 |

Today we are going to derive one of the most elegant formulas math has ever seen (in my opinion), Viète’s formula. The formula is based on the following idea

$$\frac{2}{\pi} = \frac{\text{area of inscribed square}}{\text{area of unit circle}}$$

Viète realized this formula could be manipulated by adding intermediate steps between the circle and the square

$$\frac{2}{\pi} = \frac{\text{area of inscribed square}}{\text{area of inscribed octagon}}\cdot\frac{\text{area of inscribed octagon}}{\text{area of unit circle}}$$

$$\frac{2}{\pi} = \frac{\text{area of inscribed square}}{\text{area of inscribed octagon}}\cdot\frac{\text{area of inscribed octagon}}{\text{area of inscribed }2^4\text{-gon}}\cdot\frac{\text{area of inscribed }2^4\text{-gon}}{\text{area of unit circle}}$$

As we look at polygons with more and more sides, their area approaches that of the circle. In math terms, we can write

$$\lim_{n\rightarrow\infty}\frac{\text{area of inscribed }2^n\text{-gon}}{\text{area of unit circle}} = 1$$

First, we need to create some notation to help our calculations. Define $Q_n$ as

$$Q_n = \frac{\text{area of } 2^n\text{-gon}}{\text{area of } 2^{n+1}\text{-gon}}$$

We know that as $n\rightarrow\infty$, $Q_n\rightarrow 1$. As a result, the infinite product has a chance of converging (and in this case, it does!). We can then write

$$\frac{2}{\pi} = Q_{2}\cdot Q_{3} \cdot Q_{4}\dots$$

Consider the regular n-gon. We will compute the area by dividing the polygon into $n$ triangles.

*********

The interior angle for each of these isosceles triangles is

$$\theta=\frac{2\pi}{n}$$

We also know that the side lengths are 1, since the shape is inscribed inside a circle of radius 1. We will use some trig to get the base and height of the triangle. Here is a picture for an octagon.

$$B = 2\sin\frac{\theta}{2}\hspace{10mm}H=\cos\frac{\theta}{2}$$

$$\text{area} = \frac{BH}{2} = \sin\frac{\theta}{2}\cos\frac{\theta}{2}$$

We have $n$ triangles in our shape, and we already said that the angle $\theta=\frac{2\pi}{n}$, so

$$\text{area of inscribed n-gon} = n\cdot\sin\frac{\pi}{n}\cos\frac{\pi}{n}$$

We now go back to our definition for $Q_n$

$$Q_n = \frac{\text{area of } 2^n\text{-gon}}{\text{area of } 2^{n+1}\text{-gon}}$$

$$Q_n = \frac{ 2^n\cdot\sin\frac{\pi}{2^n}\cos\frac{\pi}{2^n}}{2^{n+1}\cdot\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}}$$

$$= \frac{\sin\frac{\pi}{2^n}\cos\frac{\pi}{2^n}}{2\cdot\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}}$$

Now we need to make this formula look a little nicer. We can do this by using the trivial fact that

$$\frac{\pi}{2^n} = 2\cdot\frac{\pi}{2^{n+1}}$$

$$Q_n = \frac{\sin\frac{2\pi}{2^{n+1}}\cos\frac{\pi}{2^{n}}}{2\cdot\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}}$$

Now use the double angle formula for $\sin$

$$\sin2\theta = 2\sin\theta\cos\theta$$

$$\sin\frac{2\pi}{2^{n+1}} = 2\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}$$

$$Q_n = \frac{2\cdot\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n}}}{2\cdot\sin\frac{\pi}{2^{n+1}}\cos\frac{\pi}{2^{n+1}}}$$

The ugly stuff cancels out wonderfully, leaving us with

$$ Q_n= \cos\frac{\pi}{2^{n}}$$

Therefore

$$ \bbox[15px,border:1px solid black]{\frac{2}{\pi} = \cos\frac{\pi}{2} \cos\frac{\pi}{4} \cos\frac{\pi}{8}\dots}$$

The issue with the formula above is that, well, it uses $\pi$ to compute $\pi$. This is *criminal* circular logic. However, we can easily remedy this. Let’s look at the double angle formula for $\cos$

$$\cos 2\theta = 2\cos^2\theta – 1$$

$$\cos\theta = 2\cos^2\frac{\theta}{2} -1$$

$$ \cos\frac{\theta}{2} = \sqrt{\frac{1+\cos\theta}{2}}$$

$$ \cos\frac{\theta}{2} = \frac{\sqrt{2+2\cos\theta}}{2}$$

Using this, and starting from $\cos\frac{\pi}{4} = \frac{\sqrt{2}}{2}$ yields

$$\cos\frac{\pi}{4}=\frac{\sqrt{2}}{2}\hspace{20mm}\cos\frac{\pi}{8}=\frac{\sqrt{2+\sqrt{2}}}{2}\hspace{20mm}\cos\frac{\pi}{16}=\frac{\sqrt{2+\sqrt{2+\sqrt{2}}}}{2}$$

Therefore, we can finish off this post, having derived Viète’s formula.

$$ \bbox[15px,border:1px solid black]{\frac{2}{\pi} = \frac{\sqrt{2}}{2}\cdot \frac{\sqrt{2+\sqrt{2}}}{2}\cdot \frac{\sqrt{2+\sqrt{2+\sqrt{2}}}}{2}\cdot\frac{\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2}}}}}{2}\dots}$$