Difference between revisions of "Workshop1"

From Baltimore Node Wiki
Jump to navigationJump to search
m (Reverted edits by Icisekilupa (Talk) to last revision by Mehuman)
 
(One intermediate revision by one other user not shown)
(No difference)

Latest revision as of 02:17, 18 November 2010

Email sent to Attendees:[edit | edit source]

Event: Baltimore Node - Workshop #1 Ambient Orb

Date: Thursday, July 09, 2009 from 7:00 PM - 10:00 PM (ET)

Location: Mica

113 W North Ave Baltimore, MD 21201

For more information click here: Baltimore Node - Workshop #1 Ambient Orb


What to Bring:

1. A laptop if you have one. It will make the uploading of code the arduinos a lot smoother and we can try and trouble shoot any problems with the development environment. 2. If you bring your laptop try and bring a usb A to B cable. It the one that normally plugs into printers. This will be the interface for connecting the arduino for programming and reading data.


Instructions to get in:

You should proceed to the gate near Joe Squared if no one is there you can ring the bell. The North Avenue door is a separate business entrance. We will probably have someone stationed downstairs but once you enter you will have to sign in with security and then take the stairs to the fourth floor. I will post signs. Its not too hard to find.

Street parking is free by 7 and there is an ungated lot on the East side of the building that usually isn't full. The gated lot is by permit only.


See you all there!

Soldering Wires to the LED[edit | edit source]

The colors are just to help keep everything similar. You could use whatever colors you wanted (or have on hand).

Solder a black wire to the connection of the LED labeled with a minus sign (-)

Solder a blue wire to the connection labeled B+

Solder a red wire to the connection labeled R+

Solder a white wire to the connection labeled G+

Connect wires to the Arduino[edit | edit source]

If you followed the above scheme the red wire goes to digital pin 9, white to 10 and blue to 11.

Code[edit | edit source]

You'll need:

  • Python Ver 2.5, available at http://python.org/. If you're running Mac OS X 10.5 or Linux you've already got it.

Notes on installing on Windows[edit | edit source]

  • In addition to pyserial and python-twitter, you'll need to install pywin32. Note that you have to use Python version 2.5 - these various modules don't work for 2.6 yet.

Notes on Running on OSX[edit | edit source]

If you update to the latest Java and receive a 'Cannot launch java application' error follow the directions here: Link

Moodlamp.pde[edit | edit source]

/*
* Code for cross-fading 3 LEDs, red, green and blue (RGB) 
* To create fades, you need to do two things: 
*  1. Describe the colors you want to be displayed
*  2. List the order you want them to fade in
*
* DESCRIBING A COLOR:
* A color is just an array of three percentages, 0-100, 
*  controlling the red, green and blue LEDs
*
* Red is the red LED at full, blue and green off
*   int red = { 100, 0, 0 }
* Dim white is all three LEDs at 30%
*   int dimWhite = {30, 30, 30}
* etc.
*
* Some common colors are provided below, or make your own
* 
* LISTING THE ORDER:
* In the main part of the program, you need to list the order 
*  you want to colors to appear in, e.g.
*  crossFade(red);
*  crossFade(green);
*  crossFade(blue);
*
* Those colors will appear in that order, fading out of 
*    one color and into the next  
*
* In addition, there are 5 optional settings you can adjust:
* 1. The initial color is set to black (so the first color fades in), but 
*    you can set the initial color to be any other color
* 2. The internal loop runs for 1020 interations; the 'wait' variable
*    sets the approximate duration of a single crossfade. In theory, 
*    a 'wait' of 10 ms should make a crossFade of ~10 seconds. In 
*    practice, the other functions the code is performing slow this 
*    down to ~11 seconds on my board. YMMV.
* 3. If 'repeat' is set to 0, the program will loop indefinitely.
*    if it is set to a number, it will loop that number of times,
*    then stop on the last color in the sequence. (Set 'return' to 1, 
*    and make the last color black if you want it to fade out at the end.)
* 4. There is an optional 'hold' variable, which pasues the 
*    program for 'hold' milliseconds when a color is complete, 
*    but before the next color starts.
* 5. Set the DEBUG flag to 1 if you want debugging output to be
*    sent to the serial monitor.
*
*    The internals of the program aren't complicated, but they
*    are a little fussy -- the inner workings are explained 
*    below the main loop.
*
* April 2007, Clay Shirky <clay.shirky@nyu.edu> 
*/ 

// Output
int redPin = 9;   // Red LED,   connected to digital pin 9
int grnPin = 10;  // Green LED, connected to digital pin 10
int bluPin = 11;  // Blue LED,  connected to digital pin 11

// Color arrays
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1]; 
int bluVal = black[2];

int wait = 10;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 3;     // How many times should we loop before stopping? (0 for no stop)
int j = 0;          // Loop counter for repeat

// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

// Set up the LED outputs
void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 

  if (DEBUG) {           // If we want to see values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput 
  }
}

// Main program: list the order of crossfades
void loop()
{
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}

/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
* 
* The program works like this:
* Imagine a crossfade that moves the red LED from 0-10, 
*   the green from 0-5, and the blue from 10 to 7, in
*   ten steps.
*   We'd want to count the 10 steps and increase or 
*   decrease color values in evenly stepped increments.
*   Imagine a + indicates raising a value by 1, and a -
*   equals lowering it. Our 10 step fade would look like:
* 
*   1 2 3 4 5 6 7 8 9 10
* R + + + + + + + + + +
* G   +   +   +   +   +
* B     -     -     -
* 
* The red rises from 0 to 10 in ten steps, the green from 
* 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
* 
* In the real program, the color percentages are converted to 
* 0-255 values, and there are 1020 steps (255*4).
* 
* To figure out how big a step there should be between one up- or
* down-tick of one of the LED values, we call calculateStep(), 
* which calculates the absolute gap between the start and end values, 
* and then divides that gap by 1020 to determine the size of the step  
* between adjustments in the value.
*/

int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero, 
    step = 1020/step;              //   divide by 1020
  } 
  return step;
}

/* The next function is calculateVal. When the loop value, i,
*  reaches the step size appropriate for one of the
*  colors, it increases or decreases the value of that color by 1. 
*  (R, G, and B are each calculated separately.)
*/

int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    } 
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    } 
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  } 
  else if (val < 0) {
    val = 0;
  }
  return val;
}

/* crossFade() converts the percentage colors to a 
*  0-255 range, then loops 1020 times, checking to see if  
*  the value needs to be updated each time, then writing
*  the color values to the correct pins.
*/

void crossFade(int color[3]) {
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G); 
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);      
    analogWrite(bluPin, bluVal); 

    delay(wait); // Pause for 'wait' milliseconds before resuming the loop

    if (DEBUG) { // If we want serial output, print it at the 
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGB: #");
        Serial.print(i);
        Serial.print(" | ");
        Serial.print(redVal);
        Serial.print(" / ");
        Serial.print(grnVal);
        Serial.print(" / ");  
        Serial.println(bluVal); 
      } 
      DEBUG += 1;
    }
  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

TwitterReader.pde[edit | edit source]

/* 
 * Serial RGB LED TOO
 * ------------------
 * Serial commands control the brightness of R,G,B LEDs 
 *
 * Command structure is "#RRGGBB"
 *
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 */

#define slen 7        // 7 characters, e.g. '#ff6666'
char serInStr[slen];  // array to hold the incoming serial string bytes

int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11

void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   127);   // set them all to mid brightness
  analogWrite(greenPin, 127);   // set them all to mid brightness
  analogWrite(bluePin,  127);   // set them all to mid brightness
  Serial.println("enter color command (e.g. '#ff3333') :");  
}

void loop () {
  //read the serial port and create a string out of what you read
  int spos = readSerialString();

  if(spos==slen && serInStr[0] == '#') {
    long colorVal = strtol(serInStr+1,NULL,16);
    Serial.print("setting color to r:");
    Serial.print((colorVal&0xff0000)>>16);
    Serial.print(" g:");
    Serial.print((colorVal&0x00ff00)>>8);
    Serial.print(" b:");
    Serial.println((colorVal&0x0000ff)>>0);
    memset(serInStr,0,slen);      // indicates we've used this string
    //spos = 0;
    analogWrite(redPin, (colorVal&0xff0000)>>16 );
    analogWrite(greenPin, (colorVal&0x00ff00)>>8 );
    analogWrite(bluePin, (colorVal&0x0000ff)>>0 );
  }

  delay(200);  // wait a bit, for serial data
}

//read a string from the serial and store it in an array
int readSerialString () 
{
  int i=0;
  if(!Serial.available()) 
  {
    return -1;
  }
  while (Serial.available() && i < slen) 
  {
    int c = Serial.read();
      serInStr[i++] = c;
  }
  Serial.println(serInStr);
  return i;
}

twitterreader.py[edit | edit source]

import serial
import re
import twitter
import time
ser = serial.Serial('/dev/tty.usbserial-A70072ix', 9600)
client = twitter.Api(username='bmoreardtwit', password='ardtwit')
while 1:
	latest_posts = client.GetReplies()
	tmp=latest_posts[0].text
	#print latest_posts[0].text
	match=re.search('(#.*)',tmp)
	tmp=match.group(1)
	tmp=tmp.encode("latin1")
	ser.write(tmp)
	time.sleep(30)

Gmail checker[edit | edit source]

This will require feedparser available here http://code.google.com/p/feedparser/downloads/list

import serial, sys, feedparser, time
#Settings - Change these to match your account details
USERNAME="username@gmail.com"
PASSWORD="enteryourpassword"
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"
SERIALPORT = "COM3" # Change this to your serial port!
# Set up serial port
try:
	ser = serial.Serial(SERIALPORT, 9600)
except serial.SerialException:
	print "no device connected - exiting"
	sys.exit()

while 1:
        newmails = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])
   
        # Output data to serial port
        if newmails > 0: 
        	ser.write("#ff0000")
        	print "some mail"
        else: 
        	ser.write("#000000")
        	print "no mail" 
        #print data to terminal

        time.sleep(30)