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