Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
129 lines (108 sloc) 5.46 KB
// Adjust LED brightness according to ambient light
// GrovePi + Light Sensor + LED
// http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor
// http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit
/*
The MIT License(MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2015 Dexter Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using Windows.ApplicationModel.Background;
// Add using statements to the GrovePi libraries
using GrovePi;
using GrovePi.Sensors;
using Windows.System.Threading;
namespace Nightlight
{
public sealed class StartupTask : IBackgroundTask
{
/**** DIGITAL SENSORS AND ACTUATORS ****/
// Connect the Red LED to digital port 6
ILed redLed;
/**** ANALOG SENSORS ****/
// Connect the light sensor to analog port 2
ILightSensor lightSensor;
/**** Constants and Variables ****/
// Decide an a level of ambient light at which the LED should
// be in a completely off state (e.g. sensorValue == 700)
const int ambientLightThreshold = 700;
// Create a variable to track the current LED brightness
private int brightness;
// Create a variable to track the current value from the Light Sensor
private int actualAmbientLight;
// Create a timer to control the rateof sensor and actuator interactions
ThreadPoolTimer timer;
// Create a deferral object to prevent the app from terminating
BackgroundTaskDeferral deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
// Get the deferral instance
deferral = taskInstance.GetDeferral();
// Instantiate the sensors and actuators
redLed = DeviceFactory.Build.Led(Pin.DigitalPin6);
lightSensor = DeviceFactory.Build.LightSensor(Pin.AnalogPin2);
// Start a timer to check the sensor and activate the actuator five times per second
timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(200));
}
private void Timer_Tick(ThreadPoolTimer timer)
{
try
{
// Capture the current value from the Light Sensor
actualAmbientLight = lightSensor.SensorValue();
// If the actual light measurement is lower than the defined threshold
// then define the LED brightness based on the delta between the actual
// ambient light and the threshold value
if (actualAmbientLight < ambientLightThreshold)
{
// Use a range mapping method to conver the difference between the
// actual ambient light and the threshold to a value between 0 and 255
// (the 8-bit range of the LED on D6 - a PWM pin).
// If actual ambient light is low, the differnce between it and the threshold will be
// high resulting in a high brightness value.
brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255);
}
else
{
// If the actual ambient light value is above the threshold then
// the LED should be completely off. Set the brightness to 0
brightness = 0;
}
// AnalogWrite uses Pulse Width Modulation (PWM) to
// control the brightness of the digital LED on pin D6.
redLed.AnalogWrite(Convert.ToByte(brightness));
}
catch (Exception ex)
{
// NOTE: There are frequent exceptions of the following:
// WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '.
// This appears to be caused by the rapid frequency of writes to the GPIO
// These are being swallowed here
// If you want to see the exceptions uncomment the following:
// System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
// This Map function is based on the Arduino Map function
// http://www.arduino.cc/en/Reference/Map
private int Map(int src, int in_min, int in_max, int out_min, int out_max)
{
return (src - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
}
}