Jump to content

Constant js check loop

Zuccers

So, i'm making this project, which involves sensors and such, and i kinda need a constant check loop, so i wouldn't need to restart the microprocessor :)

function show () {
    basic.showNumber(SensorLeft)
}
function check () {
    if (SensorLeft < 5) {
        radio.sendString("0")
        radio.sendString("right2")
    } else {
        radio.sendString("4")
    }
    if (SensorLeft > 0) {
        show()
    }
}
let SensorLeft = 0
radio.setGroup(1)
radio.setTransmitPower(7)
SensorLeft = sonar.ping(
DigitalPin.P1,
DigitalPin.P0,
PingUnit.Centimeters
)
basic.forever(function () {
    check()
})

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Mira Yurizaki said:

Create a forever loop by doing something like while(true).

 

Though you should also have the micro sleep and wake up sometime later since it's likely the sensors have that fast of a sampling rate.

function show () {
    basic.showNumber(SensorLeft)	
}
let SensorLeft = 0
radio.setGroup(1)
radio.setTransmitPower(7)
SensorLeft = sonar.ping(
DigitalPin.P1,
DigitalPin.P0,
PingUnit.Centimeters
)

basic.forever(function () {
    while (true) {
        if (SensorLeft < 5) {
            radio.sendString("0")
            radio.sendString("right2")
        } else {
            radio.sendString("4")
        }
        if (SensorLeft > 0) {
            show()
        }
    }
})

I actually need fast sampling, but something like this doesn't work :/

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Mira Yurizaki said:

How fast are we talking about?

real fast, we're talking about ultrasonic sensors, to update the host(computer1), and then the host sends it to the controler(computer2) via radio waves, so we actually need speed

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Zuccers said:

real fast, we're talking about ultrasonic sensors, to update the host(computer1) computer, and then the host send it to the controler(computer2) via radio waves

Being ultrasound doesn't really mean much to me. It's the application of whatever you're using. For something like sonar, all a higher sampling rate gives you is higher precision in where something is since all you're looking for is when the signal comes back.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mira Yurizaki said:

Being ultrasound doesn't really mean much to me. It's the application of whatever you're using. For something like sonar, all a higher sampling rate gives you is higher precision in where something is since all you're looking for is when the signal comes back.

Okay, now let's take that and * by 4 :)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Mira Yurizaki said:

Being ultrasound doesn't really mean much to me. It's the application of whatever you're using. For something like sonar, all a higher sampling rate gives you is higher precision in where something is since all you're looking for is when the signal comes back.

But really, i need help with constantly updating the system with information, because right now it only scans once, shows the results, and if i need a different reading i have to reset the "host" manually

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Zuccers said:

Okay, now let's take that and * by 4 :)

Unless there's some requirement that you have to dump the sampling data from when you're interested in collecting it to the end, there's really no need to keep sampling it. Especially when you could probably set up the micro to wake up on an impulse of sorts. But then again I don't know what sensor you're using and for all I know, the micro is the sensor's direct controller.

 

Just now, Zuccers said:

But really, i need help with constantly updating the system with information, because right now it only scans once, shows the results, and if i need a different reading i have to reset the "host" manually

If having a forever loop isn't working, then something you're doing is causing it to hang.

 

So I guess the next important thing is what are you using and what environment is this? Because I've never seen a microcontroller use JavaScript before.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mira Yurizaki said:

Unless there's some requirement that you have to dump the sampling data from when you're interested in collecting it to the end, there's really no need to keep sampling it. Especially when you could probably set up the micro to wake up on an impulse of sorts. But then again I don't know what sensor you're using and for all I know, the micro is the sensor's direct controller.

 

If having a forever loop isn't working, then something you're doing is causing it to hang.

 

So I guess the next important thing is what are you using and what environment is this? Because I've never seen a microcontroller use JavaScript before.

Yeah i know it's weird, I'm using a micro:bit

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mira Yurizaki said:

Then lets step back. Figure out how to make it do something forever.

 it's already a thing in the editor, but it doesn't work...  basic.forever(function () {}

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Zuccers said:

 it's already a thing in the editor, but it doesn't work...  basic.forever(function () {}

Well, I'm not familiar with your micro's environment so I can't really help you figure out why it's not working. But otherwise, that's what you need to figure out what to do.

Link to comment
Share on other sites

Link to post
Share on other sites

Typical javascript engines aren't really designed for behavior like this, but this does sound like a different environment than normally. 

 

Most of them are event based.  To use low level language, interrupts rather than polling. For instance on web pages, you set up so that when an object is click or it's value changed, or whenever you post a form to a server and are waiting for it to responding, you set a function to be called whenever it's ready.  If you have such functionality in your environment, so that whenever a sensor is activated it calls a function you specify, I'd suggest trying that.

 

However if you need/have to poll a sensor at regular intervals, the typical way to do it is to use setTimeOut(), (see here https://www.w3schools.com/js/js_timing.asp for more info).

Assuming it's available for your environment, it takes two parameters

  1. The function you want called
  2. The number of milliseconds you need to wait to call it.

setTimeOut only calls a function once.  To have your function handler call itself with setTimout.

 

If you need submillisecond intervals you're going to have to see if there's anything specific to you're library/environment.

 

Good luck to you!

 

EDIT:  Just noticed there is a function setInterval() that works the same as setTimeOut but will call the function continuously every n milliseconds.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/22/2019 at 12:47 AM, JacobFW said:

EDIT:  Just noticed there is a function setInterval() that works the same as setTimeOut but will call the function continuously every n milliseconds.

in javascript because of the event loop/call stack just because you ask it to call every 100 ms doesn't mean it will, it will be at least 100ms and how close to that will be dependant on what else is currently in the stack.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/25/2019 at 6:10 AM, vorticalbox said:

in javascript because of the event loop/call stack just because you ask it to call every 100 ms doesn't mean it will, it will be at least 100ms and how close to that will be dependant on what else is currently in the stack.

True, but that's the same for any sort of system that runs scheduled tasks.  Javascript is unfortunately just worse about it.

 

I still have no idea what environment this code's being run in.   Depending upon what it is, there may be some asynchrous way to have a process running on the machine that collects the sensor data, then passes it to a callback function in javascript along with the time the data was collected.  The hard truth might be that Javascript isn't the best language for this.

 

Barring that, the best you can do is at every interval, get the current epoch time in milliseconds and store that with the data.  That way at the very least you have some way to measure the deviation from the expected interval.  I'd probably suggest recording the latency it took to get the measurement.  Depending upon the sensors some return the time in less than a millisecond, and some can take up to a second.

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×