Lab 4 - Force Sensors and Photocells

Posted by emily

emily's picture

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

/*
  Phot Fade
  A photo cell controls the brightness of an LED
  and later the volume of a song.
  An FSR acts as an on/off switch.
 */
 
int ledPin = 9;
int photPin = 0;
int photVal = 0;
int fsrPin = 1;
int fsrVal = 0;
int alarmMode = 0;
 
void setup() {                
  // initialize the pulse width modulation pin as an output.
  // Pin 9 has PWD on my Arduino board:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);  
}
 
void loop() {
  photVal = analogRead(photPin);
  fsrVal = analogRead(fsrPin);
  
  if(fsrVal != 0) { //if you hit the fsr, change the alarm mode
    alarmMode++;
    delay(100);
    alarmMode = alarmMode % 2;
    //Serial.print("alarm mode is: ");
    Serial.println(alarmMode);
    
    if(alarmMode != 0) { //alarm mode is off
      analogWrite(ledPin, 0);
    }
    else { //alarm mode is on
      if(photVal > 255) {
        photVal = 255;
        analogWrite(ledPin, photVal);
      }
      else {
        analogWrite(ledPin, photVal);
      }
      //Serial.print("photo cell value is :");
      Serial.println(photVal);
    }
  }
  
  else { //if you did not hit the fsr, check the alarm mode
    alarmMode = alarmMode % 2;
    if(alarmMode != 0) {  //alarm mode is off
      analogWrite(ledPin, 0);
    }
    else { //alarm mode is on
      if(photVal > 255) {
        photVal = 255;
        analogWrite(ledPin, photVal);
      }
      else {
        analogWrite(ledPin, photVal);
      }
      //Serial.print("photo cell value is :");
      Serial.println(photVal);
    }
  }
  
}
 

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

 

Creator Comments

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.

 
 
0
Your rating: None