Hi all,
I'm new to the forum and programming in general, so actually I'm trying to get my first piece of functional code working.
I want to make a PWM dimmer for an LED using a ultrasonic range sensor. At the moment I have a functional sketch, but there are some things I can't rely work out just yet. So I need some guidance.
I've seen a few posts concerning this idea already, but since everyone is using different hardware and code, it seemed more sensible to start a new topic for this specific question.
The sensor I'm using is the URM37 V3.2 from DFrobot.
http://www.dfrobot.com/index.php?route=product/product&path=36_55&product_id=53The sketch is based on the library by Miles Burton.
http://milesburton.com/index.php/URM37_Ultrasonic_Distance_Measurement_LibraryRight now the LED is 'off' (PWM=0) when the URM gives a value under 30 cm (the values are converted to cm's), it has full brightness (255) above 115cm and inbetween the LED is dimmable according to the distance above the sensor. (brightness=(sensorvalue-30)*3).
This all works fine.
The problem is that right now you have to keep your hand in place in order to keep the LED at the desired brightness. I would like to be able to sort of 'lock' the brightness so that when you remove your hand (after leaving it in the same place for a few seconds for example) the LED keeps burning at the same brightness level, until you interfere with your hand again.
How do i go at this? Any ideas? Has anyone done this already?
Thanks all!
Here's the code:
//URM37 led dimmer; interface distance between 30cm and 115 cm
// URM 1 - ARDUINO VCC
// URM 2 - ARDUINO GND
// URM 8 - ARDUINO 3
// URM 9 - ARDUINO 2
// LED+ - ARDUINO 9 (PWM)
// LEDgnd - ARDUINO GND
#include "URMSerial.h"
// The measurement we're taking
#define DISTANCE 1
#define TEMPERATURE 2
#define ERROR 3
#define NOTREADY 4
int LED = 9;
URMSerial urm;
void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
urm.begin(2,3,9600); // RX Pin, TX Pin, Baud Rate
pinMode(LED, OUTPUT);
}
void loop()
{
// Request a distance reading from the URM37
urm.requestMeasurement(DISTANCE); // You may call this as many times as you like. It will only send once
// Avoid fetching the distance until we're sure the reading is ready
if(urm.hasReading())
{
int value; // This value will be populated
switch(urm.getMeasurement(value)) // Find out the type of request
{
case DISTANCE: // Double check the reading we recieve is of DISTANCE type
Serial.println(value); // Fetch the distance in centimeters from the URM37
if ((value < 30) && (value >= 0)) // if distance is less than 30cm LED brightness is 0 (PWM number between 0 and 255)
{
analogWrite(LED, 0);
}
if((value >= 30) && (value <= 115)) // if distance is between 30cm and 115 cm brightness is determined by value
{
int light; // this value will be populated by formula based on sensor value
light = (value-30)*3;
analogWrite(LED,light);
}
if(value > 115) // distance is greater than 115cm brightness is 255
{
analogWrite(LED,255);
}
delay(200);
break;
}
}}