Lab 4 - Force Sensors and Photocells
Description
I took a little liberty with the requirement's for this week's lab. Since we had just started using photocells and force sensitive resistors, I was itching to see if I could build my own photocell alarm clock. The idea admittedly is not new, but now I felt like I had the tools to try building one. The concept of a photocell alarm clock goes like this: Instead of being woken up in the morning by a jarring buzzer or blaring music, why not wake up gradually as the sun is rising?
My photocell alarm clock demonstrates this principle by co-opting the FSR as an on/off switch and using the photocell as a volume control. The green LED doesn't serve any purpose other than to visually inspect the analog input from the photocell.
I used Arduino to receive analog input from the photocell and control transmission of that data to the serial port. I then used PySerial, appscript, and more Python to parse the serial volume data and play the wake-up song in iTunes.
Components Used
- Arduino board x 1
- breadboard x 1
- wires x many
- Force Sensitive Resistor (FSR) x 1
- photocell x 1
- 10k resistor x 2
- green LED x 1
- 220 Ohm resistor x 1
- catchy song x 1
Arduino Code
Python Code
#!usr/bin/env python # encoding: utf-8 """ phot_alarm.py Created by Emily Wagner on 2011-02-23. """ from appscript import * import serial def get_serial(): ser = serial.Serial('/dev/tty.usbmodem411', 9600) buf = '' volume = 10 app(u'iTunes').tracks[u'Here Comes the Sun'].play() #read from serial port while True: try: value = ser.read() if value == '\r' or value == '\n': volume = int((float(buf.strip())/180)*100) print volume app(u'iTunes').sound_volume.set(volume) buf = '' else: buf = buf + value except: continue #ignore non integer data that is not \r or \n if __name__ == '__main__': get_serial()
Video
The entire contraption is a little finnicky. I had to re-upload the Arduino code each time I ran the Python script in order to receive the serial data.