Saturday, May 7, 2011

Turning Twitter into a universal switch

After you get the Arduino talking nicely with Twitter, turning it into a switch is cake. I used a power switch tail which is a pretty cool device in itself. It's basically an extension cord with a built in switch, and when a 5V dc current is supplied, the switch is on, otherwise it is off. This is perfect for the arduino because it's just a high or low pin setting to turn on or off the power. Combined with the twitter control or the arduino, it essentially turns twitter into a universal switch for any household appliance. You could also hook it up to a power strip to turn on or off a bunch of devices, for example turn on or off all the lights in your room.

Sunday, May 1, 2011

Serial Coms with Twitter and Python

For my project, I wanted to make a universal switch that would be operated using twitter. The interaction would be that from anywhere, you can make a tweet and turn on and off any electronic appliance. This could either be done by having a switch tail or by having a servo motor to manually flip the switch. I just got the communication between twitter and the arduino working so I'll post the code.

A few notes: Make sure you install the following packages: pySerial, simpleJson, simpleGeo-oauth2, httpLib2, and python-twitter. Also, make sure you are using a 32 bit version of python because the 64 bit has trouble dealing with serial communications. Another important bit of info is that you cannot have the arduino's serial monitor open during communication because python will not recognize that com port as being open.

Also, you will need a twitter account and a registered app to get the 'key' and 'secret' values. To register and app, go to dev.twitter.com

Anyway, on to code. Here's what you have to be running on the connected PC:
#******************************************#
# Based off of code by by Gregg Horton 2011 #
# Modified by Eric Kenvin #
# *****************************************#

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='key', consumer_secret='secret', access_token_key='key', access_token_secret='secret')

##set to your serial port
print ('before opening')
ser = serial.Serial(2, 19200)

## check serial port
def checkokay():
line=ser.readline()
print line
## Welcome message
print ('Welcome To Would you kindly!')
print ('Awaiting Orders')
def action():
status = [ ]
x = 0

status = api.GetUserTimeline('UserName') ##grab latest statuses

checkIt = [s.text for s in status] ##put status in an array

## check for match and write to serial if match
if checkIt[0] == 'would you kindly turn on?':
print 'Tweet Recieved, turning on'
ser.write('1')
elif checkIt[0] == 'would you kindly turn off?':
ser.write('0')
print 'stopped, awaiting instructions.'
else:
ser.write('0')
print 'Awaiting Tweet'


while 1:
checkokay()
action()
time.sleep(30) ## twitter apps only allow for 350 requests per hour, so you cannot be updating constantly

###################################################################

Here's the arduino side code

int relayPin = 13; // LED connected to digital pin 13
int incomingByte = 0; //declare incoming byte
// The setup() method runs once, when the sketch starts

void setup()
{
// initialize the digital pin as an output:
pinMode(relayPin, OUTPUT);
Serial.begin(19200); // set up Serial library at 19200 bps

Serial.println("Arduino is ready!");
}

void loop()
{
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
Serial.println(incomingByte);
if (incomingByte == 49)
{
digitalWrite(relayPin, HIGH);
}
else
{
digitalWrite(relayPin, LOW);
}

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}


I based this project off of the instructable found here http://www.instructables.com/id/Tweet-a-Pot-Twitter-Enabled-Coffee-Pot/

Sunday, April 24, 2011

EL Wire Bike jacket

My inspiration for this project was this, but I didn't really like the way it looked, so I started thinking of a way to make it at least 20% cooler. After searching on the internet for a while, I found something called EL(electroluminescent) wire so I decided to use that to make not just a turn signal bike jacket, but one that looks like it's out of the new Tron. Here's the process I went through to make this project, and all of the things that I had planned on doing that didn't work.

Step 1: Playing with conductive thread and the lilypad.

I ordered a lilypad arduino off of Sparkfun because I wanted this project to use conductive thread and be a full on wearable device. I started by sewing the lilypad and power supply to a pad of craft felt with conductive thread. Once that was working, I stitched two tact switches on that would be going to either hand. On the coding side, I had the two pins connected to the switches constantly feeding out power in pulses, and a single ground pin. This way, when I held down either switch, the wire on that side would theoretically blink, and if I held down both sides, they would flash like hazards. This worked well on a small pad of felt, but not so well when I tried putting it into my jacket. I think that the conductive thread that I was using had too high of a resistance value so that not enough power reached the lights. I knew this wouldn't work because I hadn't gotten the EL wire inverters in the mail yet, and since it wouldn't even light up a few LEDs, there probably wouldn't be enough power.

My second attempt at this was to hook up switches that ran from the battery to the arduino. When the arduino read in a high value on either input pin, it would start pulsing power to the output pins. This worked much better because I had more overall voltage to work with.

Step 2: Laying out the pattern

I got my EL wire in the mail, but not the inverters so I decided to start constructing the pattern so that I could just drop in the inverters when they arrived. The bottom layer was layed out in foil tape so that it would reflect the light from the back side of the EL wire and get max brightness.
My plan was to put the lilypad on the back center underneath where the adjustable straps are pulled tight.

After putting down the foil tape, I sewed on the EL wire with fishing line. I used fishing line because I had read on another project using EL wire that it was a good method of attaching it.



Step 3: Powering the El wire with...something

When I put the lilypad and the inverters into the jacket, it didn't work. If I replaced the outputs with LEDs, it worked fine. At this point, I had spend too many hours constructing a cool pattern to change my project into the LEDs that I didn't really like much anyway, so I needed to find an alternative. I could light up one side if I connected the wire directly to the power of the battery, but I couldn't light up both sides, and the one that was lit was not particularly bright. I had a spare 9v battery sitting around, so I decided to try that. It worked and the jacket was WAY brighter than the 3-5 volts from the arduino. If I went with this solution, I would have to blink the light manually until I put in some kind of timer to interrupt the circuit. But the project was due in 2 days and I thought it looked cool enough to be sufficient.

Step 4: Polish
Now that I had covered my jacket in what looked like tin foil and blue wire, I need to make it so that I could actually wear it in the daylight without feeling stupid. It was also in my original plans to somehow diffuse or soften the light so that the pattern was wider, and so that it just looked white in the daylight. I was doing some tests with how to make sheet vinyl opaque, and found that a power sanded did the trick pretty well. I had also read that glue was a good way to diffuse lights from LEDs, and it could also be used to put the sanded vinyl on top of the wires. This is what the jacket looked like before the glue and vinyl.


This is what it looked like after.



Overall, I'm satisfied with the result, although I wish I had time to put in a timer so that it would auto blink. I remember seeing an auto flashing LED, which might break the circuit when it's not lit. If that's the case it would be perfect. I might still make some enhancements to the project, since it is my motorcycle jacket and I'll be wearing it a lot when it FINALLY stops snowing.

Anyone who wants to replicate this: here's my material list

Bike jacket: ~$150 (for a good one, although you could really put this on any article of clothing)
20m EL wire: $20
2 3v Power inverters: $20
Foil tape: ~$8
Sheet vinyl: $2/yd
Fishing line: $2

Tools:
Scissors
Sewing needle
Power sander
Hot glue gun
Soldering thingy
Patience

On a side note, I decided to not cut the EL wire because I didn't want to mess around with that. There was an instructable on it if you want to make a smaller pattern here

Saturday, March 19, 2011

Output Test (Blink)

Output test: 5x5 LED grid

Construction: Clusterf&*@. Alright, so I decided to not use the breadboard because the LEDs would be too close together and it's not really set up for grid structures. The way this works is that pins 3-7 will power the rows, and pins 8-12 will be connected to the columns. I got this idea from the instructable here http://www.instructables.com/id/Yet-Another-Daft-Punk-Coffee-TableDisplay/step3/Wire-it-up/ . If you go to the link, you can find the circuit diagram that I based my build on.
The most difficult part of my construction was that I was using solid core wire, so let's do some math... 5x5 grid=25 leds, 25 ledsx2 wires each=50 connections that I had to twist together by hand, and make sure they didn't touch any other connection and create a short circuit. How did I do it? Equal parts insanity and patience. My fingers really hurt right now, but I think it was worth it.

Coding: I basically took the coding from my 2x2 grid and extended it to work with 5x5.

Result:

Input tests (Blink)

Input test: accelerometer (model adxl335)

Wiring: use 3 short wires to connect X, Y, and Z to A0, A1, and A2 respectively.
use 2 more wires (measured to fit) to connect GND (accelerometer) to GND (arduino) and VCC to 3.3V.

Coding:
//Set pins
const int X_PIN = 0;
const int Y_PIN = 1;
const int Z_PIN = 2;
const int NUM_AXES= 3;
const int PINS[NUM_AXES]={X_PIN,Y_PIN,Z_PIN};
//Input comes really fast, so set a buffer for averaging values
const int BUFFER_SIZE=16;

int buffer[NUM_AXES][BUFFER_SIZE];
int buffer_pos[NUM_AXES]={0};

void setup()
{
Serial.begin(9600);
}

int get_axis(const int axis)
{
//Give arduino time to switch pins
delay(1);
//Read values into the buffer
buffer[axis][buffer_pos[axis]]=analogRead(PINS[axis]);
buffer_pos[axis]=(buffer_pos[axis]+1)%BUFFER_SIZE;
long sum=0;
//Average buffer values
for(int i=0;i
{
sum+=buffer[axis][i];
return round(sum/BUFFER_SIZE);
}

int get_x(){return get_axis(0);}
int get_y(){return get_axis(1);}
int get_z(){return get_axis(2);}

void loop()
{
//Print values
Serial.print("X: ");
Serial.print(get_x());
Serial.print("Y: ");
Serial.print(get_y());
Serial.print("Z: ");
Serial.println(get_z());
}


Tweaking:
Values from x and y axes are between ~8 and ~800 while z is between ~200-~750
Using these values, I need to set up a bit of code to detect when the unit is rotated along one of its axes within tolerances. The output should only read once after detecting this change.

Saturday, March 12, 2011

Arduino Stuff, attempt 1

Went to the electronic's store on goodman and bought a bunch of wire and LEDs so I could just start messing around. Installed the software and drivers and booted up the program, did the blink test which was pretty straightforward. I decided that I wanted to make an array of LEDs that could be lit and dimmed individually. To do this, I started out with the simplest, 2X2. I've got 2 wires from pins 6 and 7 (anode) to control the rows, and 2 from 8 and 9 (cathode) to control the columns. To make a light turn on, I just set the power to high on the row(anode) pins, and low on column(cathode) pins so that there is a flow of electricity. To turn a light off, either apply a low charge to the row pin, or if another light in that row should be on, apply a high charge to that column. If the row and column have the same value, there will be no electricity flow. Anyways, here's the code I've got working so far, I didn't get frames working yet but maybe next time.

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/
#define DIM 2
#define DIM1 (DIM-1)
typedef byte Frame[DIM];
int cols[DIM]={8,9};
int rows[DIM]={6,7};
int pinVals[DIM][DIM];
boolean lit=false;
Frame blankFrame={B00,
B00};
//Frame fullFrame={B11,
// B11};
byte *currentFrame=blankFrame;
int curFrame=0;
Frame Frames[4]={
{B10,
B00},
{B01,
B00},
{B00,
B10},
{B00,
B01}};
void setFrame()
{
curFrame++;
if(curFrame>4)
curFrame=0;
currentFrame=Frames[curFrame];

}
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
int i;
for(i=0;i {
pinMode(cols[i],OUTPUT);
pinMode(rows[i],OUTPUT);
}
}

void loop() {
int c=HIGH;
int r=LOW;
if(lit)
{
c=LOW;
r=HIGH;
}
//Just tests connections, doesn't use frames
int i;
for (i=0;i < DIM;i++)
{
digitalWrite(rows[i],LOW);
for (int j=0;j < DIM;j++) {
digitalWrite(cols[j],HIGH);
delay(250);
digitalWrite(cols[j],LOW);
}
digitalWrite(rows[i],HIGH);
}
setFrame();
delay(400); // wait for a second
}