bonescript analogRead() while loop

Hi guys,

So I want to use the function analogRead() with a while loop, like so:

var b = require(‘bonescript’);
while (true) {
analogRead(‘P9_36’, printStatus);
}
function printStatus(x) {
console.log('x.value = ’ + x.value);
}

But my problem is that nothing is printing out on the terminal. I think the problem with this is that analogRead is asynchronous or something? Anyway, what I think is happening is that beaglebone is waiting for all the analogReads to reply before it will print out all the values on the terminal at once. But what I want is for beaglebone to print the adc values to the terminal as they come.

Thanks for the help.

On Wed, 2 Mar 2016 15:06:05 -0800 (PST), Audrey
<aong@smith.edu> declaimed the following:

Hi guys,

So I want to use the function analogRead() with a while loop, like so:

var b = require('bonescript');
while (true) {
  analogRead('P9_36', printStatus);

  Based upon all the examples I've seen, you need to reference the
analogRead() IN THE "require" MODULE...

  b.analogRead(...)

http://beagleboard.org/Support/BoneScript/analogRead/

Hi sorry, yeah no I did actually use

b.analogRead(‘P9_36’, printStatus);

although I did write it here as just analogRead(‘P9_36’, printStatus);

So I don’t think that’s the issue, although good point.

var b = require(‘bonescript’);
while (true) {
analogRead(‘P9_36’, printStatus);
}
function printStatus(x) {
console.log('x.value = ’ + x.value);
}

I havent seen the prototype for analogRead() but the above code seems wrong. printStatus() takes a single arguement, yet you’re not passing any argument to it. Another thing I would like to point out. All Javascript should be async in nature, since Javascript is callback oriented. This is like one of the major language attractions over other languages.

Anyway, I’m thinking your problem with not getting anything to print, is that you’re not actually passing anything to the printing function . …

I’m not sure why printStatus() doesn’t require a argument when called, but that’s what they used in the documentation (http://beagleboard.org/support/BoneScript/analogRead/) and I have used it for a single data point and it worked. I believe analogRead passes in the argument x into printStatus for you.

Yeah, I was thinking that the asynchronous/callback nature of javascript is the problem for this but I was wondering how to circumvent it.

Yeah, I was thinking that the asynchronous/callback nature of javascript is the problem for this but I was wondering how to circumvent it.

The typical way to deal with this type of situation would be to write a callback. In this case, it might be better to write a timed callback, instead of running a constant loop. loops typically I tend to think of as procedural, where javascript really isnt procedural in nature. More callback / object oriented if that makes sense ?

So perhaps use setTimeout() instead of a loop.

Audrey, and yeah I vaguely remember something about Javascript for single parameter functions using the function with no parameter when called is possible. However, I definitely would not good coding style . . .

However, I definitely would not consider this good coding style*

Yeah I don’t really like it either by the bonescript library documentation doesn’t specify further than what they gave so I wouldn’t know what to put as x even if I did want to put it in.

Yes, setInterval() did the trick. Thanks for your help!

I’m still a bit confused about while loops vs setInterval() though. Like I vaguely get that there is a difference between the two but I don’t really understand javascript all that well.

Why does setInterval() work while the while loop does not? What is the code if I wanted to implement my own setInterval() function?

On Wed, 2 Mar 2016 21:02:38 -0800 (PST), Audrey
<aong@smith.edu> declaimed the following:

Hi sorry, yeah no I did actually use

b.analogRead('P9_36', printStatus);

although I did write it here as just analogRead('P9_36', printStatus);

So I don't think that's the issue, although good point.

  As gets pointed out in comp.lang.python quite often:

  Always use cut&paste to avoid transcription errors that cause others to
go off in the wrong direction... <G>

I’m still a bit confused about while loops vs setInterval() though. Like I vaguely get that there is a difference between the two but I don’t really understand javascript all that well.

Why does setInterval() work while the while loop does not? What is the code if I wanted to implement my own setInterval() function?

Here: http://stackoverflow.com/questions/729921/settimeout-or-setinterval is a good explanation of what setInterval(), and setTimeout() are. But again, Javascript is an event driven language, which in this context is an incredible strength. setInterval(), and setTimeout() are events( or callbacks if you prefer ). They’re meant to “fire” when a certain condition has been met, and execute a callback function. These are all but guaranteed to execute.

On the opposite side of things. while is just a loop. A loop which may repeat so often is actually overwhelms the code that is meant to run . . .But in fact I’m not 100% sure what the problem may be. I have not looked through every bit of related code, and quite honestly I won’t either. My instinct does tell me that there is a glitch in the bonescript code, and just willy nilly running that code in a loop is the root cause.

Thanks so much for the help!