bonescript analogRead() while loop

130 views
Skip to first unread message

Audrey

unread,
Mar 2, 2016, 6:06:05 PM3/2/16
to BeagleBoard

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.

Dennis Lee Bieber

unread,
Mar 2, 2016, 11:07:41 PM3/2/16
to beagl...@googlegroups.com
On Wed, 2 Mar 2016 15:06:05 -0800 (PST), Audrey
<ao...@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/
--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Audrey

unread,
Mar 3, 2016, 12:02:38 AM3/3/16
to BeagleBoard, wlf...@ix.netcom.com
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.

William Hermans

unread,
Mar 3, 2016, 1:37:49 AM3/3/16
to beagl...@googlegroups.com
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 . ..

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Audrey

unread,
Mar 3, 2016, 1:54:13 AM3/3/16
to BeagleBoard
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.

William Hermans

unread,
Mar 3, 2016, 2:15:14 AM3/3/16
to beagl...@googlegroups.com
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.

William Hermans

unread,
Mar 3, 2016, 2:48:56 AM3/3/16
to beagl...@googlegroups.com
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 . . .

William Hermans

unread,
Mar 3, 2016, 2:49:56 AM3/3/16
to beagl...@googlegroups.com
However, I definitely would not consider this good coding style*

Audrey

unread,
Mar 3, 2016, 5:04:53 AM3/3/16
to BeagleBoard
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?

Dennis Lee Bieber

unread,
Mar 3, 2016, 7:56:55 AM3/3/16
to beagl...@googlegroups.com
On Wed, 2 Mar 2016 21:02:38 -0800 (PST), Audrey
<ao...@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>

William Hermans

unread,
Mar 3, 2016, 12:46:02 PM3/3/16
to beagl...@googlegroups.com
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.

Audrey

unread,
Mar 4, 2016, 5:16:28 PM3/4/16
to BeagleBoard
Thanks so much for the help!
Reply all
Reply to author
Forward
0 new messages