Today I spent the entire morning finishing up the book Getting Started with Arduino by Massimo Banzi, the co-founder and co-creator of Arduino. I highly recommend this book to anyone else starting to tinker with arduinos. The book explains the different functions and coding style of the arduino language and teaches you through very simple examples the thinking process of how to write these sketches and how to prototype using a solderless breadboard. I really liked that Massimo keeps you engaged through really simple material and got me excited to play some more with these awesome microcontrollers.
I worked through all the simple projects and now I am posting about the last big project of this book, the Arduino Networked Lamp. This project is somewhat like a mood lamp for articles from your favorite site and measures how much light is in the room. This example project combines the smaller projects such as: wiring and coding an LED to shine, using pushbuttons to turn on and off an LED, and using a photo resistor to detect light. This lamp connects to the internet and reads through the RSS feed of any site of your choice. From the feed, it counts the instances of certain words of your choice. In the book, it'll have you look for the words: peace, love, arduino, from the make magazine website. I ran into a few bugs here because the make magazine site doesn't want to let me tap into the RSS feed so I choose another site- Muscle and Fitness Magazine (I also tried T-nation, my favorite fitness article site, but they took their RSS feed down, so sad). After keeping a count for each word, the program converts each count into a hexadecimal integer (a color value) and the arduino converts it into bytes. The bytes takes the bytes and assigns each measurement to a color: red, green, blue. The color is then displayed on an RGB LED that can be turned on or off by a pushbutton. But wait! That's not all! This project has also wired in a photo resistor sensor to detect how much light the device is receiving and records it in the serial port of the arduino. This is a pretty interactive project for a beginner, but its a good step toward trying to build sensors that will display data on my laptop.
First, we'll start off with the coding of the computer app to display the count data and light brightness. We'll be using another type of coding language that can be used in adjunct to the arduino coding language, called Processing (link to download: https://www.processing.org/download/?processing
) . It is a java based coding language that makes it easier to code up programs that allow us to see arduino measurements on our computers. The language is pretty similar to arduino C based code so in that sense transitioning over won't be so much of a pain. With Processing, we will be creating a Proxy that will download the RSS feed from muscleandfitness.com and extract all the words from the resulting XML file. Then it'll go through everything and count the instances of the words "workout", "pound", and "train". From those numbers, it will calculate a color value and send it over to the arduino. The arduino will send back to the program the amount of light measured and the program will display it on screen.
I added a couple lines of code in the beginning since the program did not understand what the URL function was without these few lines. I added the following on the left. Turns out the issue was that it needed to import some extra Java classes in order to recognize the URL function and some other functions used in the code.
The arduino code is a lot shorter. Most of the code is defining variables and pins that will be used. The setup loop simply sets up the serial monitor to run and declares the pushbutton as an input. In the loop function, it reads the light sensor and reports the value to the serial monitor and relays it to the java program running on screen. Then the arduino moves onto converting the hexidecimal values received from the program into bytes and assigns these values to each color r,g,b. Then it checks if the pushbutton has been pressed to indicate that the LED should be on and writes the values to each color.
(Note: you'll find the printed serial ports at the bottom of the Processing environment where it shows you the messages about your code)
In my case, the correct name of my arduino serial port is: /dev/tty.usbmodem1411
Now you can run the Processing code and you'll get something like this:
It'll change colors as the word count changes. It'll also show you the color value it is sending to the LED and the light level the arduino is sending the program back. At the very bottom a countdown is displayed, telling you every 10 seconds that it will update the count and light reading.
Lets not forget about the actual arduino wiring! Here's a sketch of how I wired the lamp together.
All the resistors used are 10K ohm resistors but I used 330 ohm resistors for the LED so it would shine brighter (don't worry there wont be enough voltage to blow anything out).