In this tutorial, you will learn how to control the wind speed of a DC fan that is connected to an Arduino from an iPhone app, using Openframeworks to send OSC signals over wifi. It is quite simple if you have some foundational knowledge of Openframeworks and Arduino. Before starting this tutorial, it is important that you download the latest Openframeworks for OSX and iOS, which can be found here: http://openframeworks.cc/download/
Also, you need to have the latest version of Xcode installed on your Mac, which you can download here: https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12
Lastly, you need the Arduino IDE to upload the Standard Firmata sketch to your Arduino, which can be found here: http://arduino.cc/en/Main/Software
Once you download and install all this software, you will be ready to move forward in this tutorial. Let's get started!
Step 1: Materials
In order to get the software to do anything special in this case, you're going to need the hardware. First, you are going to need a DC fan. The reason we are using a DC fan instead of an AC fan is because we want to modulate the fan speed along a range of values, instead of simply switching it between on and off states. You can find a DC fan at your nearest Radio Shack, or order it from them online here: http://www.radioshack.com/product/index.jsp?productId=2102824
Next, we are using a special transistor called a MOSFET (metal–oxide–semiconductor field-effect transistor) so we can bypass using additional components like a diode and a resistor. If you want to learn more about the MOSFET and why it is useful, check out its wiki page here: http://en.wikipedia.org/wiki/MOSFET
Obviously, you are going to need an Arduino. I used an Uno, but any Arduino should be fine. Additionally, you will need a 9V - 12V power supply, either from a charger connected to an outlet, or an external battery. This is important, since the Arduino only supplies 5V, and we are using a 12V fan. Without this additional power supply, you run the risk of overheating your Arduino. Some Arduinos, like the Uno, allow you to plug a charger directly into the microcontroller. Others require you to have an adapter to extract the power and ground from the charger, like the one in the image above. Depending on which Arduino you have, you may or may not need this.
Lastly, you will need 5 wires and a breadboard, along with your Mac to run the Openframeworks OSC receive example.
Step 2: Assembly
The assembly of these components is very simple. First, if you have a charger adapter to extract the power and ground from your power supply, make sure you solder the power wire to the top of it, and the ground wire to the bottom, as shown in the image on the previous step. Connect these wires to the power and ground strips on the breadboard, respectively.
If the DC fan comes with additional wires and adapters as mine did, you can cut these off so that you only have the power and ground wires coming from the fan. Connect the power wire from the fan to the power strip on the breadboard, and the ground wire from the fan to the middle pin on the MOSFET transistor.
If you are looking directly at the front of the MOSFET transistor (with the bulge facing towards you), connect the left pin on the MOSFET transistor to pin 9 on the Arduino. Then, connect the right pin on the MOSFET transistor to the ground strip on the breadboard.
Lastly, connect a wire from the power strip on the breadboard to the Vin pin on the Arduino. That's it! Now let's get to the code.
Step 3: Sending OSC Messages From the IPhone
Now we get to the good stuff. Before you do any coding, make sure your wifi is running smoothly with no firewalls, and obtain your IP address. You can do this by clicking on the wifi icon on your Mac's toolbar and clicking on Open Network Preferences. Your IP address is the number that is highlighted and blurred out in the image above, without the final period.
Once you have your IP address, copy the folder called oscSenderExample within the folder of_v0.8.1_ios_release/examples/ios to the myApps folder within of_v0.8.1_ios_release/apps. Open the Xcode project file called "oscSenderExample.xcodeproj" within the copied folder. Once open, browse the Project Navigator on the left and click on the file called "ofApp.h". Where it says #define HOST "localhost", change local host to your IP address. Now navigate to the file called "ofApp.m".
Here, I created a gradient background that changes colors depending on the Y position of your finger as it touches the screen. The code for this, and the easing function that handles the transition, are in the images above. It is implemented by the "texColor.loadData(colorPixels, w,h, GL_RGB);" function. The example is set up to pass the X and Y position of your finger via OSC to the Receive project example through this function call, "sender.sendMessage( m );". That's all for the sending, now we can begin working on the receiving.
Step 4: Receiving OSC Messages on the Mac
Before you begin working on the receiving functionality, make sure you upload the Standard Firmata example to your Arduino Board. Once your Arduino is primed and ready to go, copy the folder called oscReceiverExample within the folder of_v0.8.1_osx_release/examples/addons to the myApps folder within of_v0.8.1_osx_release/apps. Open the Xcode project file called "oscReceiverExample.xcodeproj" within the copied folder. Once open, browse the Project Navigator on the left and click on the file called "ofApp.cpp". Once there, open another example project file within of_v0.8.1_osx_release/examples/communication/firmataExample called firmataExample.xcodeproj. We will be taking some of the code from this file, and placing it within the ofApp.cpp file within the oscReceiverExample.
The first bit of code we will need are these three lines:
ofAddListener(ard.EInitialized, this, &ofApp::setupArduino);
bSetupArduino = false;
Replace "/dev/tty.usbmodem1421" with your Arduino Port, and follow it with either 57600, as above, or 9600 depending on your Arduino configuration.
After this, copy the setupArduino, updateArduino, digitalPinChanged, and analogPinChanged functions into the ofApp.cpp file within the oscReceiverExample project above the update function.
Create 2 integers. One for the fan speed, and one for the finger input that is received from the iPhone.
Within the setupArduino folder, replace ard.sendDigitalPinMode functions with "ard.sendDigitalPinMode(9, ARD_PWM);"
Place the line "updateArduino();" within the update function. Also within update, find the function conditional that states "if(m.getAddress() == "/mouse/position)", and place "input = m.getArgAsInt32(0);" within it. This lets us save the finger Y position into the input variable.
Lastly, write these three lines in the draw loop to use our custom-made easing function to specify the range and re-map the input range sent from your iPhone finger position:
fanSpeed = (int)easing(fanSpeed, ofMap(input, 60, 500, 255, 0), 0.08);
if(fanSpeed >= 255) fanSpeed = 255;
if(fanSpeed <= 0) fanSpeed = 0;
And that's all! The images above provide a more in-depth look at the code.