Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Revision of LEDBot from Wed, 09/12/2007 - 19:16

Project Members: 
Hannes Hesse

LEDBot

Description

LEDBot is an IRC bot that listens for commands in a chatroom and controls three LEDs with an Arduino computer. The LEDs are contained in a styrofoam coffee cup. The plastic lid servers as a diffusor.

The low-level LED functionality was implemented with Arduino's development environment. Arduino takes commands through the serial interface.

The high-level application code was written in Python, using the PySerial (pyserial.sourceforge.net) and pyirclib (http://sourceforge.net/projects/pyirclib/) libraries.

Running the application code from Python will instantiate a chatbot which will join the channel #tangible on the IRC sever irc.freenode.net and listen for commands. Other users can control the LEDs by joining this channel and writing commands like 'r255' or 'blue' to the channel. The command 'off' will reset all LEDs.

Hardware Components Used

3 Light Emitting Diodes (LEDs)
3 220 Ohm resistors
Arduino computer
Solderless breadboard
Jumper wires
Styrofoam coffee cup
Plastic lid

Arduino Code

/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorVal>", where "colorCode" is
* one of "r","g",or "b" and "colorVal" is a number 0 to 255.
* E.g. "r0" turns the red LED off.
* "g127" turns the green LED to half brightness
* "b64" turns the blue LED to 1/4 brightness
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;

int redPin = 11 ; // Red LED, connected to digital pin 9
int greenPin = 9; // Green LED, connected to digital pin 10
int bluePin = 10; // 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. 'r43') :");
}

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

colorCode = serInString[0];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
colorVal = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
serInString[1] = 0;
serInString[2] = 0;
serInString[3] = 0;
}

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

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}

Python Code

import pyirclib
import string
import sys
import time
import serial

channel = "#tangible"
nickname = "ledbot"

ser = serial.Serial('/dev/tty.usbserial-A4001nKF', 9600)
irc = pyirclib.Irclib('irc.freenode.net',6667)
irc.setDebug = 1
irc.login(nickname, username = nickname)
irc.join(channel)

irc.privmsg("#tangible", nickname + " is listening")

def parsemessage(msg):
if msg['event'] == "PRIVMSG" and str(msg['text']) == "!NAMES":
print irc.names()

def arduinoWrite(msg, ser):
ser.write(msg)
time.sleep(0.1)

while 1:
message = irc.getmessage()
text = message["text"]
if text != 0:
print "text: <" + text + ">"
if text.startswith("red"):
print("setting lamp to red")
ser.write("r255")
time.sleep(0.1)
ser.write("g0")
time.sleep(0.1)
ser.write("b0")
if text.startswith("green"):
print("setting lamp to green")
ser.write("g255")
time.sleep(0.1)
ser.write("r0")
time.sleep(0.1)
ser.write("b0")
if text.startswith("blue"):
print("setting lamp to blue")
ser.write("b255")
time.sleep(0.1)
ser.write("g0")
time.sleep(0.1)
ser.write("r0")
if text.startswith("off"):
print("turning lamps off")
ser.write("b0")
time.sleep(0.1)
ser.write("g0")
time.sleep(0.1)
ser.write("r0")
else:
ser.write(text[:4])

Item

Photo to follow on Sep 13.


Powered by Drupal - Design by Artinet