Multi User – PIN CHANGE Event

About Forums Forum Multi User – PIN CHANGE Event

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #756
    Talib
    Moderator

    Hi

    I have my breakout server setup for multiple connections. My aim is to trigger en event if another user changes a pin (IE a PWM pin).

    I used the pwm_led example and modified the code (pasting only relevant parts):

    function onReady(event) {
    // Remove the event listener because it is no longer needed
    arduino.removeEventListener(IOBoardEvent.READY, onReady);

    // Set Pin 11 to PWM
    arduino.setDigitalPinMode(11, Pin.PWM);

    // Create an LED object to interface with the LED wired
    // to the I/O board
    led = arduino.getDigitalPin(11);

    /***** Added event listener *****/
    led.addEventListener(BO.PinEvent.CHANGE, pwmChange);

    // Use jQuery to get a reference to the elements
    // and listen for events
    $(‘#btnStop’).click(function() { changeLed(0.0); });
    $(‘#btnFull’).click(function() { changeLed(1.0); });
    $(‘#btnHalf’).click(function() { changeLed(0.5); });

    $(“#slider”).slider({
    step: 0.05,
    min : 0.0,
    max : 1.0
    });

    $(‘#value’).text(“Value: 0.0”);
    }

    $( “#slider” ).bind( “slide”, function(event, ui) {
    var value = $( “#slider” ).slider( “option”, “value” );
    $(‘#value’).text(“Value: ” + value.toFixed(2));
    led.value = value;
    })

    function changeLed(intensity) {
    led.value = intensity;

    /***** Removed this to test functionality *****/
    //$(“#slider”).slider( “option”, “value”, intensity);
    $(‘#value’).text(“Value: ” + intensity);
    }

    /***** PWM Change event *****/
    function pwmChange(evt)
    {
    $(“#slider”).slider( “option”, “value”, evt.target.value);
    }

    The idea is that when another connection (another user on another PC), changes the PWM and event is triggered and the slider on my PC responds accordingly. Currently the slider is moving when I press my buttons, but not when the other user does.

    Thanks for any assistance

    Talib

    #757
    Talib
    Moderator

    I also found a small bug in your example. The slider does not go back to 0 after changing it, but to 0.5 (0 for some reason becomes the next value when you drag it one step to the right). The solution can be found here:

    http://stackoverflow.com/questions/621147/jquery-ui-slider-cant-slide-to-0

    Specifically the comment to the answer:

    Actually, there’s a subtle difference in all this (I just ran into this myself). You should use “ui.value” to get the value in your slide event handler. That will give you the current value. You will get the previous value if you fetch it by asking the slider for its handle value (e.g. .slider(‘value’) or .slider(‘option’, ‘value’), etc.). This matters if you are trying to say dynamically update a label while the user is sliding the handle around. Summarizing: if you use ui.value to get the value in the slide event handler, you won’t need to use a stop event handler.

     

    #760

    The issue is that you are trying to use an output (PWM) as an input. You cannot listen for a change event on an output pin. What you can do however is attach a potentiometer and listen for a change event on this input and update the slider position on the other computer. There may be some minor issues in multi-client mode as well so let me know if after trying a pot if you are still having problems.

    #761
    Talib
    Moderator

    Would this be a valid feature request? Or would this require custom firmata firmware?

    #762

    I actually use pin change events on output pins within the library to handle pin state. I’ve just never thought of this as a use case outside of managing pin state internally. What would you like to use such a feature for? Monitoring general pin output on the Arduino from multiple clients?

    #763

    Or are you specifically just trying to get the PWM value? In that case I don’t think it is possible by reading pin.value since the value you set on the PWM pin is only accessible within the client that set the value on the PWM pin. There is a method in IOBoard.js that is currently marked as private that may be able to get the current PWM value from the Arduino. It queries the current pin state directly from the arduino board (although it could have adverse effects which is why it’s currently not exposed in the API). I’m traveling today but will try to test tonight when I’m at home.

    #765

    I have looked into this further (now that I’m back home and have access to an Arduino). Reading the PWM value is not possible on the Arduino. You can only set the value using analogWrite (http://arduino.cc/en/Reference/analogWrite), but you cannot read it. The reason you are seeing the value update when listening for the CHANGE event on the led pin is because the Pin object is storing the value that was set. This value is not coming from the Arduino, which is why you are not seeing the slider move in the other client application. It has no way to know that the PWM value changed.

    I understand what you are trying to do, but there is really no easy way to accomplish it and this isn’t a Breakout or Firmata issue. You’d have trouble doing this in c on an Arduino as well (and it would actually take 2 Arduino boards connected via serial just to simulate this outside of Breakout).

    StandardFirmata does make reading output pin data possible because all write values are stored in an array. So you are not getting the value from the pin itself, but rather from the stored value that was written to the pin. As I mentioned, there is a private method queryPinState in IOBoard.js that can get the stored PWM pin value from the Arduino. I could make this method public and you could use it to get the PWM value but you would have to use setInterval to poll for the value which is not ideal.

    The better solution to what you’re trying to accomplish is to open up a websocket connection between your 2 client applications, and send the PWM value from the client that is driving the PWM pin to the other client that is visualizing the pwm value.

    #766
    Talib
    Moderator

    Hi

    Thanks for the amazing reply. Yes my application is partially intended for monitoring. Thus if I move the slider the other sliders on clients should change as well.

    The ideal would be for the CHANGE event to be triggered for a user change as well, and announced to all clients, even if the value is not read from the PWM but from the stored values. However, my experience with firmata is null and not sure if that would be possible with standard firmata.

    I know this might not be in your control and again thank you for your effort. How well does breakout work with custom firmata firmware? Your answer depends on how far I will go to make this work or look at another solution.

    PS The websocket idea might not be viable as the clients might not be operating at the same time. I then might as well look at a Client-Server solution.

    #767

    If I expose queryPinState() in the API you’ll be able to monitor any pin. This purpose for pin state queries is for GUI monitoring of the current state of the Arduino: http://firmata.org/wiki/Protocol#Pin_State_Query_.28added_in_version_2.2.29. I discovered a bug in the query last night that may be on the firmata side. I’ll try to resolve it and make the method public sometime over the weekend. However in order to use the query you will have to poll. There is no way to get an event when an output pin changes. There was also a reason at one time I made this method private but it was so long ago I forgot the exact reason so I’ll have to do a lot of testing and perhaps some refactoring before I make it fully public again.

    Also by writing custom firmata firmware instead of using StandardFirmata you have full control over what data is sent between Arduino and JavaScript… it’s just harder than using StandardFirmata because you have to maintain your own Sysex or String protocols to handle communication. Breakout and the core Firmata library (not to be confused with the StandardFirmata implementation) will provide callbacks and parse the sysex or string values for you but provide no additional value at that point (other than the Breakout Server functionality of course). I’m planning to add a Guide on writing custom firmware for Breakout.

    #768
    Talib
    Moderator

    Thanks, will keep my eye on the progress. Polling might be a temporary solution and is not that hard to implement.

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Forum’ is closed to new topics and replies.