Page 1 of 1

Newby in Phidget programming in Javascript and Node JS

Posted: Tue Jul 09, 2019 10:41 am
by FredrikH
Hi

I have the code I am playing with:

Code: Select all

let vs1_13 = new jFive.Switch(12);
  vs1_13.on('open', function () {
    
    console.log('vs1_13 on');
    //LED on
    LEDBoardObj.board1.chInstance0.open().then(function () {
      // code to execute after open succeeds
      console.log('Utgång 0 - Öpppen');
      LEDBoardObj.board1.chInstance0.setDutyCycle(dimValue);  
    }).catch(function (err) {
      // code to execute if open fails
      console.log('Utgång 0 - Ej öppen');  
    });
    // Status setting
    
    // FlightSim setting
  });

  vs1_13.on('close', function () {
    console.log('vs1_13 off');
    //LED on
    LEDBoardObj.board1.chInstance1.open().then(function () {

      // code to execute after open succeeds
      console.log('Utgång 0 - Öppen');
      LEDBoardObj.board1.chInstance1.setDutyCycle(dimValue);  
    }).catch(function (err) {
      // code to execute if open fails
      console.log('Utgång 0 - Ej öppen');  
    });
    // Status setting
    
    // FlightSim setting
  });
The code are demostrating an switch event with Johnny-Five an phidget. The code for opening the diff. channel are stored in a object "LEDBoardObj". The instance itself is in that obj on key board1 --> chInstance0/1/2/3/4/5 ...... !

So when swith on at my swith the led it should be light and should be closed when the with is off. I have one green led and one red. My goal is switch = on, led green is on. When switch = off a red led is on and the green led is off. Like a blinking light but with two leds :)

I have read about a close function but where am I need placing it in both the vs1_13.on('open'/'close', functions ... to getting it to work?

If you are going to need more info I can share it, no problem!

Best regards Fredrik

Re: Newby in Phidget programming in Javascript and Node JS

Posted: Tue Jul 09, 2019 10:58 am
by mparadis
What happens when you try to run your program? Do the lights turn on at all? Does open succeed?

Typically you only call open once per channel at the beginning of your program and then close at the end of the program. You technically can open and close every time the switch changes if you want, but there's no real advantage to doing it that way.

If you want the green light to turn off when vs1_13 closes, you need to set the duty cycle for both LEDs in the close handler.

Re: Newby in Phidget programming in Javascript and Node JS

Posted: Tue Jul 09, 2019 1:43 pm
by FredrikH
Hi

The leds are lights, in that code so all is fine but they remains on when shiwtich. If doing like bellow as you told i :

Code: Select all

 let vs1_13 = new jFive.Switch(12);
  vs1_13.on('open', function () {
    
    console.log('vs1_13 on');
    //LED on
    LEDBoardObj.board1.chInstance0.open().then(function () {
      // code to execute after open succeeds
      console.log('Utgång 0 - Öppen');
      LEDBoardObj.board1.chInstance0.setDutyCycle(dimValue);  
      [b]LEDBoardObj.board1.chInstance1.close();[/b]
    }).catch(function (err) {
      // code to execute if open fails
      console.log('Utgång 0 - Ej öppen');  
    });
    // Status setting
    
    // FlightSim setting
  });

  vs1_13.on('close', function () {
    console.log('vs1_13 off');
    //LED on
    LEDBoardObj.board1.chInstance1.open().then(function () {
      // code to execute after open succeeds
      console.log('Utgång 0 - Öppen');
      LEDBoardObj.board1.chInstance1.setDutyCycle(dimValue);
      [b]LEDBoardObj.board1.chInstance0.close();[/b]
    }).catch(function (err) {
      // code to execute if open fails
      console.log('Utgång 0 - Ej öppen');  
    });
    // Status setting
    
    // FlightSim setting
  });
You have too explaing what you mean with:?
"If you want the green light to turn off when vs1_13 closes, you need to set the duty cycle for both LEDs in the close handler."

Re: Newby in Phidget programming in Javascript and Node JS

Posted: Tue Jul 09, 2019 2:02 pm
by mparadis
Right now, vs1_13.on(open) sets the duty cycle of channel 0 to "dimValue". It will stay at that value until you tell it to go back to zero. So if you want the green LED to turn off when vs1_13.on(close) happens, you need to set the duty cycle for that LED to 0. Close() doesn't turn off the LED, it just does the opposite of open() - makes it so your program no longer has the ability to affect that channel.

Again, it's not very efficient to be opening and closing the channels every time the switch changes, so you may want to call open() and close() in the once each in the main part of your program instead of in your switch handlers.

Re: Newby in Phidget programming in Javascript and Node JS

Posted: Tue Jul 09, 2019 2:07 pm
by FredrikH
Ofcause, I am just playing with it now to learn. In my real program the channel will remains open until the program is done :)

That was that I thought that I need to ply with the duty cycle, I thought the close() turn off the led right away :)

Thank you for the confirmation in this.