I felt this was the best place to begin seeking help, if it isn't, I'll gladly follow anyone's lead.
Thank you!
I'm an art student and have coddled together a very rough solution to wanting a Lightbox with (12v) LEDs to increase in intensity as a viewer get's close to it. I found 2 scripts and combined them. One was (should I be calling out the author?) a solution to dim a 12v strip of LEDs and the other, a vanilla Ultra Sonic Range sensor (SKU 2760342) script.
The solution I'm striving for is to have the Lightbox sitting at say 25% intensity when turned on. When an individual walks in front of the Lightbox, say 6' away, the LEDs in the box would begin to increase in intensity. The LEDs would continue to increase in intensity as the viewer got closer to the Lightbox until the viewer was approx 3' from the box, at which point the LED would be at full intensity.
I understand the help I'm asking for might come in several forms ... components used and the programming, so again, if I need to split this post into two or move the whole question somewhere else, I'll be more than happy to do so.
I'm using an UNO board and Radio Shack Range Sensor connected to the 5v pin, Gnd and digital pin 7. For the LED portion I'm using a IRF542 transistor connected to the digital pin 9, Gnd pin and Gnd lead of the LED strip. The positive LED lead is going to the positive side of 12v source.
Can someone "bless" the components I'm using and/or recommend a programming or pinout change? I'd like a solution that was smoother with a "hold" at full intensity for 10 sec or so before reacting to the measurement sensed by the USRF.
Code is as follows:
/***************************************************************************/
// Function: Measure the distance to obstacles in front and print the distance
// value to the serial terminal.The measured distance is from
// the range 0 to 400cm(157 inches).
// Hardware: Ultrasonic Range sensor
// Arduino IDE: Arduino-1.0
// Author: LG
// Date: Jan 17,2013
// Version: v1.0 modified by FrankieChu
// by www.seeedstudio.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/*****************************************************************************/
#include "Arduino.h"
class Ultrasonic
{
public:
Ultrasonic(int pin);
void DistanceMeasure(void);
long microsecondsToCentimeters(void);
long microsecondsToInches(void);
private:
int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
_pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delayMicroseconds(2);
digitalWrite(_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_pin,LOW);
pinMode(_pin,INPUT);
duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
return duration/29/2;
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
return duration/74/2;
}
Ultrasonic ultrasonic(7);
const int transistorPin = 9; // from dimmer sketch
void setup()
{
Serial.begin(9600);
pinMode(transistorPin, OUTPUT);
}
void loop()
{
long RangeInInches;
long RangeInCentimeters;
ultrasonic.DistanceMeasure();// get the current signal time;
RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
Serial.println("The distance to obstacles in front is: ");
Serial.print(RangeInInches);//0~157 inches
Serial.println(" inch");
// Serial.print(RangeInCentimeters);//0~400cm
// Serial.println(" cm");
delay(100);
if (RangeInInches < 156)
{
int brightness = (157-RangeInInches)*2;
//times 2 was close enough for converting inches to 0-255
//transition from side maybe a problem
if(brightness > 254)
{
analogWrite(transistorPin, 255);
}
else
{
analogWrite(transistorPin, brightness);
}
}
else
{
analogWrite(transistorPin, 75); // raise or lower from 75 minimum
Serial.println(" Minimum light value");
}
//for (int brightness = 0; brightness < 255; brightness++) {
// analogWrite(transistorPin, brightness);
// delay(10);
// }
//
// delay(2000);
//
// for (int brightness = 255; brightness >= 0; brightness--) {
// analogWrite(transistorPin, brightness);
// delay(10);
// }
delay(500);
}