Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always returning 1 as new value during toggling #35

Closed
roccomuso opened this issue Jun 29, 2015 · 7 comments
Closed

Always returning 1 as new value during toggling #35

roccomuso opened this issue Jun 29, 2015 · 7 comments

Comments

@roccomuso
Copy link

Someone can explain why it returns always 'off' in stato? It can't toogle.

var Gpio = require("onoff").Gpio;
led = new Gpio(17, 'out');

   led.read(function (err, value) { // Asynchronous read.
        if (err) throw err;

        led.write(value ^ 1, function (err) { // Asynchronous write.
          if (err) throw err;
          var stato = (value == 1) ? "on": "off"; 
          console.log('Pin new value: '+stato);
        });
  });

For example, when i turn on the pin with a write method, everything seems to work fine, but when i try to read the pin status, the pin turn OFF and the value printed on the console is 0/off.

The weird thing is that it happens even with the rpi-gpio.js library: JamesBarwell/rpi-gpio.js#24

@fivdi
Copy link
Owner

fivdi commented Jun 30, 2015

Is it because stato is being logged to the console instead of value?

@roccomuso
Copy link
Author

No it's just a typo in the question. Fixed.

PS. I'm using Raspbian on a Raspberry Pi B.

@fivdi
Copy link
Owner

fivdi commented Jul 2, 2015

I would expect stato to have a value of off in the example program, so everything appears to be function correctly.

This line of code creates a new Gpio object called led with an initial value of 0:

led = new Gpio(17, 'out');

This line of code reads the value of led and sets value to 0:

led.read(function (err, value) { // Asynchronous read.

This line of code therefore sets stato to off

var stato = (value == 1) ? "on": "off";

What makes you think salto should be equal to on?

PS. Please don't modify previous posts as it makes it difficult/impossible to understand the topic later.

@roccomuso
Copy link
Author

So how can i read the value of led without setting it to 0?

Doesn't exist a way to read the pin state?

And for example how would you implement a toggling function without reading
the current pin value?

@fivdi
Copy link
Owner

fivdi commented Jul 2, 2015

There appears to be a misunderstanding here. Reading the value of a Gpio object for a LED will _NOT_ set it's value to 0. What will set it's value to 0 is creating a new Gpio object for the LED. If the program is continuously creating Gpio objects for the LED, then the value will continuously be set to 0. This should be avoided.

Please try the following program and let us know if it functions correctly. It toggles the state of a LED on GPIO 17 every 200ms, 25 times:

var Gpio = require('onoff').Gpio,
  led = new Gpio(17, 'out');

(function blink(count) {
  if (count <= 0) {
    return led.unexport();
  }

  led.read(function (err, value) {
    led.write(value ^ 1, function (err) {
    });
  });

  setTimeout(function () {
    blink(count - 1);
  }, 200);
}(25));

@roccomuso
Copy link
Author

As you suggested, every new Gpio object sets the value to 0. Thanks, now all works fine ;)

@fivdi
Copy link
Owner

fivdi commented Jul 3, 2015

That's good news :)

@fivdi fivdi closed this as completed Jul 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants