I just got the GrovePi+ and am trying to make the led_fade program in NodeJS, as I am slightly more comfortable with it than with python. I however cannot find a way to change the LED’s brightness. I did find one thing in the documentation that might help, but I don’t have a clue how to utilize it.
write(value) - Write a value on the sensor/component
I think this could be used to set the brightness but I haven’t figured it out yet.
Thanks in advance and have a wonderful day!
Edit: After some research, I discovered what I should’ve already known. The write function takes one parameter: when it’s 0, the led turns off, and when it’s 1, it turns the led off. Any number values below 1 result in it turning itself off and any above 1 result in turning on. So when I put .6 it doesn’t change brightness it just turns off.
If you ramp the interval in that led_blink program or change the ratio of on to off time (modulate the pulse width) you may be able to change the apparent brightness of the led.
I figured it out! I posted the solution below for anyone who might run into a similar problem
@cyclicalobsessive, playing with the ratio turnOn/turnOff does work, but it seems like a bit of a hacky way of doing it nothing wrong with that, ofc, I was just looking for a different solution.
@jimrh I am working with a single LED. Thanks for the e-mail address, I’ll definitely use that sometime in the future
now, this is the code that works for me:
const GrovePi = require('../libs').GrovePi;
const Board = GrovePi.board;
const RotaryAngleAnalogSensor = GrovePi.sensors.RotaryAnalog;
const AnalogSensor = require('../libs/').GrovePi.sensors.base.Analog;
const anSens = new AnalogSensor(5);
let board;
function start() {
console.log('starting')
board = new Board({
debug: true,
onError: function (err) {
console.log('TEST ERROR')
console.log(err)
},
onInit: function (res) {
if (res) {
console.log('GrovePi Version :: ' + board.version());
// Analog Port 2 Rotary Angle Sensor
const rotaryAngleSensor = new RotaryAngleAnalogSensor(2);
console.log('Rotary Angle Sensor (start watch)');
rotaryAngleSensor.start();
rotaryAngleSensor.on('data', function (res) {
console.log('Rotary onData value = ' + res);
// potentiometer value * 2.55 (max value of led = 255)
anSens.write(res*2.55)
})
}
}
})
board.init()
}
// happens whe ctrl+c pressed
function onExit(err) {
console.log('ending');
board.close();
process.removeAllListeners();
process.exit();
if (typeof err != 'undefined'){
console.log(err);
}
}
// starts the test
start()
// catches ctrl+c event
process.on('SIGINT', onExit)
Hacky? Perhaps true. Unfortunately if you want a digital event/signal to act like an analog one, this is the fastest and easiest way to do it, especially for simple things like LED’s.
Additionally, the brightness value and the apparent value are related by a complex ratio called “gamma”.
Translation: a brightness value of “50%” results in a visible brightness of over 90%.
To get a truly apparent 50% brightness, you set the brightness value to something closer to 80 instead of 255 or even 128.
You can use this trick to save huge amounts of battery power and increase your run-time.
Glad it worked for you. If I’m reading that correctly you’re reading a sensor and then scaling the value to analog sensor out. I wonder if behind the scenes if that isn’t actually doing a pulse width modulation in the background (vs. an actual analog current reduction)?
/K