Lab 6

Submitted by zwasson on Wed, 03/13/2013 - 08:58

 

Description

The purpose of this lab was to explore motion as an output. Using a light sensor, I created an alarm that vibrates when it reaches a certain brightness in the room. There are already existing light alarms and vibrating alarms, and it is perfectly possible to use them together. However, doing so means the user must set and maintain two separate alarms. By triggering the vibration based on a light threshold, the two types of alarms may be integrated seamlessly. Alternatively, a user could use the light-triggered alarm to ensure he/she is out of bed after the sun is up. A potentiometer is added to adjust the intensity of vibration.

Components

  • Arduino Uno
  • Light Sensor
  • Vibration Motor
  • Potentiometer

Code

/*
  Lab6
  This lab uses DC motors and light sensors to create
  a vibrating alarm that's triggered by light.
  
  Zach Wasson
 */
 
#define LIGHT_SAMPLES 32
 
// pin definitions
int motor = 9;
int photoresistor = A0;
int potentiometer = A1;
int avg_dark = 0;
int avg_light = 0;
 
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pins as outputs.
  pinMode(motor, OUTPUT);  
  Serial.begin(115200);
  
  Serial.println("Press enter when the lights are on.");
  while(Serial.read() != '\n');
  for(int i = 0; i < LIGHT_SAMPLES; i++) {
    avg_light += analogRead(photoresistor);
  }
  avg_light /= LIGHT_SAMPLES;
  Serial.println(avg_light);
  
  Serial.println("Press enter when the lights are off.");
  while(Serial.read() != '\n');
  for(int i = 0; i < LIGHT_SAMPLES; i++) {
    avg_dark += analogRead(photoresistor);
  }
  avg_dark /= LIGHT_SAMPLES;
  Serial.println(avg_dark);
}
 
// the loop routine runs over and over again forever:
void loop() {
  int light = analogRead(photoresistor);
  int intensity = analogRead(potentiometer);
  Serial.println(light);
  Serial.println(intensity >> 2);
  if(light > avg_light - (avg_light - avg_dark)/3) {
    analogWrite(motor, intensity >> 3);
    delay(500);
    analogWrite(motor, intensity >> 2);
    delay(500);
    analogWrite(motor, 0);
    delay(1000);
  } else {
    delay(2000);
  }
}
 
0
Your rating: None
Drupal theme by Kiwi Themes.