Introduction: Nocturnal Emissions: My Arduino Powered Internet Enabled Dream Generator

We all need to dream more so I've created this Arduino Powered "dream generator" to help plant the seeds for some big thinking craziness.  When you approach the generator a Maxbotix ultrasonic sensor triggers lights and music that draw you in so you can stare into the blue LED enshrouded infiniteness of the box.  Press the pulsing blue button and a thermal receipt printer generates your dream which is mashed up from Allan Ginsberg's poem Howl Part 1, and the twitter feeds of the NY Times and pop star Taylor Swift.  The juxtaposition of these three elements is designed to stimulate your own free-associating amygdala powered dream machine so you can make papa Descartes proud in your quest for absolute dream supremacy! 

Step 1: The Basic Components

My dream generator includes a lot of separate components and getting them to work together is a challenge. Here's a laundry list of what it entails.

There's a Maxbotix LV-EZ1 Ultrasonic Proximity sensor. http://www.adafruit.com/products/172 They weren't kidding. It is pretty easy. It's got 7 hook up holes but you only really need three of them if you want to use PWM which seems to be a little more accurate than Analog. It takes a PWM pin on your Arduino though so that could be a factor in your project.

It's got a string of awesome blue LEDs from SuperBright LEDs. They run on 12 Volts which created some powering complications that I'll explain later.

There's a music box that plays my own custom song. The LEDs and the music box are activated by an approaching person (or pet, they can dream too can't they?) when the sensor gets to a predetermined proximity reading.

There's a cool blue pulsing button that activates dream generation. (And happens to be waterproof.) http://www.adafruit.com/products/481 (Be careful when ordering this because there's one with an identical picture that's an on/off switch.) For our purposes we wanted one that acts as a pushbutton.

There's a thermal receipt printer that prints out the dreams. http://www.adafruit.com/products/600

And finally there's an Ethernet Enabled Arduino. (You could also use a regular Arduino and an Ethernet Shield.)

So it all goes together like this. You are attracted to the box because of the blue pulsing button. As you approach the entire thing lights up and mysterious music begins to play. You press the button to activate the dream generator and it goes out to the internet and mashes up relevant (or irrelevant) internet fodder such as twitter feeds, headlines, facebook statuses, etc. and then mashes them up through the filter of iconic Beat poet Allan Ginsberg's signature work Howl and voila! You've turned the internet into your friend instead of a mind numbing cavalcade of non-sequiters.

Step 2: First Things First

There were a lot of components to this project so I built them up one at a time starting with the Maxbotix Sensor.

Despite the 7 pin holes there are really only 4 pins that you're interested in. Power Ground Analog and PWM. Connect Power to 5v on the arduino, ground to gnd, and (ignoring Analog for now) connect PWM to pin 9 on your internet enabled Arduino. (That's one of the PWM pins.)

I started out with the code from this tutorial http://playground.arduino.cc/Main/MaxSonar .

There you'll find code for Analog, Digital, and a version that averages several readings to smooth out the results. There's also a Maxbotix Library available from Github but as far as I can tell none of these sketches activated that library so I don't think it's necessary.

I loaded the PWM sketch and changed the pin number from 7 to 9.

Uploaded to the arduino and after opening the serial monitor could see that I was reading proximity!

Step 3: Now Let's Turn on the Lights!

With the Maxbotix generating measurements in inches I set up a Transistor to activate the power to the lights and music. I used a Tip 120 NPN transistor connected to Pin 7. The transistor acts just like a switch and when Pin 7 goes high it opens the circuit by allowing power to flow to ground from the motor and lights. I declared pin 8 to be an output named music and created a short piece of code to check the sensor values and to send pin 8 high when the sensor reading was less than 5 inches. (I eventually changed this to a larger number but for testing this kept my activation area compact.)

A motor generates a backflow of electricity when the magnetic field of the rotors collapses and this surge could damage your transistor and potentially your Arduino so it's VERY important to include that diode in parallel across your circuit. I got this diagram of how it's hooked up off of Bildr and I triggered both the lights and the motor off of the same Tip 120 circuit.

Eventually I decided I wanted the motor to run at 5v and lights are supposed to run at 12 so I set it all up at 12v and put a 5v Voltage regulator into the circuit for the motor but it got way too hot so I divided the rails on my breadboard and included two separate power supplies, one at 12v for the lights and one at 5v for everything else and then everything seemed happy.




Step 4: You Need a Pulsing Blue Button.

The pulsing blue button is a really high quality looking chrome device with a blue LED ring. (It comes in other colors too.) Classic and classy for sure. It's a little confusing at first glance because it has 5 connector poles but there's a really easy explanation at http://www.adafruit.com/products/481 in the product description.

Basically it's just a standard LED (with a built in resistor which simplifies connection!) combined with a switch that can be wired to be either normally open or normally closed.

The two outside pins are for the LED so I hooked them up first. Just a ground pin and a PWM pin on the arduino cause I wanted it to pulse. I used pin 3 connecting the positive lead to it.

I always forget the orientation of LEDs (although it's easy to remember the long pin is positive) but here's a good instructable with simple outline https://www.instructables.com/id/Identifying-LED-pi...

Once it was connected I got the basic fade sketch that comes in the Arduino examples, modified the pin number from 9 to 3 and then fiddled with the delay eventually settling on 50 for a suitably mesmerizing hypnotic cadence to the flow of the pulse.

Step 5: Now for the Printer

The thermal receipts printer runs on 5v and has the most complicated set up of all.  My hook up was further complicated by the fact that my printer had the wire colors swapped from green to yellow so I couldn't get it to work right away because both the tutorials I was looking at used wire color as the hookup instruction whereas they really should have used TX and RX which stands for transmit and receive.  The tutorials are good though once this was straightened out but be aware that the wire colors could be off.

There's a lot of tutorials available . . .

http://learn.adafruit.com/mini-thermal-receipt-printer/overview

http://tronixstuff.com/2011/07/08/tutorial-arduino-and-a-thermal-printer/

my favorite one was this one on Bildr

http://bildr.org/2011/08/thermal-printer-arduino/

It provides links to the libraries you need to get on github and you'll need to load this into your Arduino IDE before you can use it but in the end it's very easy to get your printer to print things with simple commands that are part of the Thermal library.


Step 6: We Interrupt This Program!

One of the trickiest things I had to learn for this project was all about interrupts.  An interrupt is a process that can run in parallel in the background while your sketch takes care of other things.  I couldn't get the button to activate the printer and I decided it was because with the Maxbotix sensor polling all the time, and the music and lights to turn on there was too much happening.  A button press is a perfect thing for an interrupt especially because it doesn't happen that often.  I've heard it described this way.  Your doorbell's broken and you're expecting an important shipment of right angle headers for your final project that's due next week.  You're frantically writing your instructable for Making Studio and you don't want to stop every minute or two to see if the delivery guy has dropped off the package.  So you hire a task rabbit to stand at your door and alert you when the package arrives.  That's like an interrupt!  Your code keeps plugging away while the interrupt operates in parallel without interrupting your code until it has some important info.

They seem complicated and it's made more so by the fact that there's a lot of info about these Arduino implemented interrupts that only seem to function on pins 1 and 2.  I learned about a deeper level interrupt called an AVR Interrupt that can work regardless of pin number.  They seem sort of complicated because they're written in a low level code that just looks like gobbldygook but I found the code here in the Adafruit Wave Shield Examples.

You start out by "including" AVR at the beginning of your sketch

#include <avr/pgmspace.h>//so we can program using AVR code

Then you activate the interrupt like this . . .

//initalize AVR interrupt 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;

(I know, right?)

Then you identify a function activated by the interrupt

//the interrupt that runs in parallel with the loop function and calle the check_button() function

SIGNAL(TIMER2_OVF_vect) {
  check_button();
}

Then you program the function

}


void check_button(){

  buttonState = digitalRead(buttonPin);

  if(buttonState == HIGH && previousButtonState == LOW){
    buttonPressed = 1; //use this global variable to tell the loop function the button is pressed
    previousButtonState = HIGH; //set the previou button state to the current button state
  }

  if(buttonState == LOW){
    buttonPressed = 0; //tell the loop funtion that the button is not pressed
    previousButtonState = LOW; //set the previous buttonState to the curent button state
  }


}

Now when the interrupt sees that the button has been pressed it turns the music and lights on!

Step 7: Lights! . . . Sensor! . . . Action!

I ran into a lot of problems getting the Internet part of this system to work.  I started with the Adafruit Internet of Things printer tutorial.

http://learn.adafruit.com/internet-of-things-printer

It teaches you a lot of useful stuff like how to set up a Twitter API account that will enable your Arduino to access the Twittersphere.  There's also a lot of excellent tutorials on scraping sites for useful information.

I read about Parsing HTML

http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html

read about Regular Expressions (Regex)

http://www.regular-expressions.info/quickstart.html

Text Finding with Arduino

http://playground.arduino.cc/Code/TextFinder

Sending Text Files with Arduino

http://stackoverflow.com/questions/10220918/how-to-send-a-text-file-and-append-it-using-arduino

And a cool Internet activated musical bell system.

http://stream.tellart.com/2011/02/13/bells/

Clearly connecting my printer enabled box to the internet is going to open up an incredible world of mashup fun.

For the time being I'm having to hand code the dreams after performing random searches on the internet.  Come back soon though and you'll be able to see how your internet nightmares can be transformed through the edifying filter of Beat Poet Allan Ginsberg!