LED intensity controlled via Potentiometer

Hey guys,

I’m trying do a simple project. Basically, I have one imp (A) connected to a potentiometer and another imp (B) connected to a LED.

Imp A reads an analog input and sends it to Imp B, that commands the LED brightness through PWM control. Although it may seem simple, I’m having trouble to make the code work! It shows the correct potentiometer value, but then it does not vary the LED brightness… Only when I change out.set(currentValue/65535) to out.set(0.5) it does change the brightness! -_-

Potentiometer code:
`// Reading a potentiometer value

// Write our program start in the logs window
server.log(“Start Potentiometer Reading”);

// Configure pin to be analog input
hardware.pin8.configure(ANALOG_IN);

// Create output port
out <- OutputPort(“PotValue”, “number”);

// Function to check potentiometer value
function checkPotentiometer()
{
// Read potentiometer value
local currentValue = hardware.pin8.read();

// Show value in planner and set it to output port
server.show(format("%.2f", currentValue/65535));
out.set(currentValue/65535);

// Wakeup the imp to check the potentiometer value again
imp.wakeup(0.01, checkPotentiometer);

}

// Configure the imp
imp.configure(“Read Potentiometer”, [], [out]);

// Start reading potentiometer
checkPotentiometer();`

LED code:
`// Control a LED intensity throught PWM Control

// Show program name in logs window
server.log(“Start PWM LED Intensity”);

// Set pin to PWM, with a 20 ms period and duty cycle to 1 (default)
hardware.pin7.configure(PWM_OUT, 0.02, 1.0);

// Create inputPort and correspondent function light the LED
class LEDintensity extends InputPort
{
// Set name and type of input port
name = “InputAnalog”;
type = “number”;

// Create function to light the LED
function set(value)
{
    // Write PWM value accordingly to the input value. Divide by 65535 to get a value between 0 and 1
    hardware.pin7.write(value);
    server.log(format("%.2f", value));
}

}

// Configure imp in the server
imp.configure(“PWM LED Intensity”, [LEDintensity()], []);`

Any help? Thanks :slight_smile:

Looks like a problem with integer division. Try:

server.show(format("%.2f", currentValue/65535.0)); out.set(currentValue/65535.0);

Thank you! It worked!

I am trying to do the same thing, but with the Hannah Board. I have a program written that lights a different color LED depending on a temperature range. The LEDs are very bright and I would like to use the onboard Pot to dim the brightness. I used a combination of the Color Blink and Temp Logger examples to make the code (see attached). Any ideas for adding Pot control of brightness?

Steve

@slibutti
I modified the AMX Color Controller a bit, so you can adjust the color brightness with the potmeter.

Dolf,

Works great. Attached is the script I wrote for a Temperature Monitor that sends the Temp values (C and F) to a Textstar LCD as well as turns on the LED Blue if Temp C < 19, Red if Temp C > 26 and Green for any other Temp. The LED was so bright it was annoying, but I used your color brightness script as you will see and it did the trick. I can now set the brightness level and it will remain at that level no matter what the color change.

Thanks for all of your help,

Steve