Sarah Morton
Published

Light Sensing "Night Time" Leggings

A pair of leggings that are wired and programmed to sense as light dims around them - ideal for an active person running in evening hours.

IntermediateFull instructions provided464
Light Sensing "Night Time" Leggings

Things used in this project

Hardware components

ProtoSnap - LilyPad Development Simple
SparkFun ProtoSnap - LilyPad Development Simple
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Leggings

Story

Read more

Schematics

Circuit Diagram

This is a diagram indicating what should be connected to which pins. Generated in Sketch.

Code

LPP_10_NightLight.ino

Arduino
Determine which pin in the Lilypad Arduino you will sew the light sensor through. In this example. I chose "A2" as my pin.

You will need to specify how many LEDs will be used, by calling "int numLEDs = (number here);". I only used 2 LED's, so the code reads "int numLEDs = 2;". Like the light sensor, you will need to assign the LEDs pins. I chose pins A3 and A5. To assign the LEDs to these pins, type "int LED[2] = {A3, A5};" after specifying the number of LEDs.

You will have to consider at what darkness threshold the sensor will read, then interact with the LEDs and cause them to blink. I indicated that my threshold is at 50, which reads as "int threshold = 50;" in the code. When the lights drops at or below this specified threshold, that is when the sensor will read the environment as dark enough for the lights to blink.
The exact code used in this project and referenced above is demonstrated in this project.
/*
LilyPad ProtoSnap Plus Activity 10: Twinkling Night Light
SparkFun Electronics
https://www.sparkfun.com/products/14346

Create a twinkling night light that turns on when it gets dark.

Follow the tutorial at: https://learn.sparkfun.com/tutorials/lilypad-protosnap-plus-activity-guide#10-twinkling-night-light-project

This code is released under the MIT License (http://opensource.org/licenses/MIT)

******************************************************************************/

// Create variables for the pins we'll be using:

int lightSensor = A2;

// Array of all the LEDs we'll be twinkling. You can set these to the sewtabs
// you'll be using in your project. Remember to only choose outputs that
// have the "~" symbol that are compatible with analogWrite.

int numLEDs = 2;
int LED[2] = {A3, A5};


int blueLED = 14;

// Threshold for light level (when it's darker than this, twinkle LEDs)

int threshold = 50;

void setup()
{
  int x;

  // Initialize the pins we'll be using
  
  pinMode(lightSensor, INPUT);

  for (x = 0; x <= numLEDs; x++)
  {
    pinMode(LED[x],OUTPUT);
  }

  pinMode(blueLED,OUTPUT);

  // Initialize the serial monitor

  Serial.begin(9600);
}

void loop()
{
  int x,lightLevel,brightness;

  // Read the sensor value (will be 0 to 255):

  lightLevel = analogRead(lightSensor);

  // Print out the sensor reading:

  Serial.print("light level: ");
  Serial.print(lightLevel);
  Serial.print(" threshold: ");
  Serial.print(threshold);
  Serial.print(" twinkle: ");

  // If the light level is below the threshold, twinkle LEDs:

  if (lightLevel < threshold)
  {
    Serial.println("ON");
    digitalWrite(blueLED,HIGH);
    
    // Pick a random LED:
    
    x = random(numLEDs);

    // Quickly ramp up the brightness of the LED from off to on:

    for (brightness = 0; brightness <= 455; brightness++)
    {
      analogWrite(LED[x],brightness);
      delay(1);
    }

    // Quickly ramp down the brightness of the LED from on to off:
    
    for (brightness = 455; brightness >= 0; brightness--)
    {
      analogWrite(LED[x],brightness);
      delay(1);
    }

    // Wait a random amount of time (up to 2 seconds)

    delay(random(2000));
  }
  else
  {
    Serial.println("off");
    digitalWrite(blueLED,LOW);
  }
}

Credits

Sarah Morton
1 project • 0 followers

Comments