Sync delay like Arduino delay function built-in #6070
Replies: 1 comment
-
Posted at 2019-02-07 by @allObjects @user97622, you are looking for a cycle waster? Nope, you ain't go-n-a get one, no matter how much I'm stressed you (try to) pull us over on... But let's start over on the good foot: Welcome @user97622 to Espruino and its forum! We all - including me - we understand where you are coming from... But come on a ride - with a thread of thought and Espruino - something you will never ever regret. Think about this scenario: instead of waiting and watching the clock painfully until it is time to do the next move, you set the timer / alarm clock and put your mind on something more enjoyable then constantly watching the hands moving or digits flipping and being terribly afraid to still miss the moment(s)... Espruino is event driven like JavaScript in the browser. When you do nothing in a Web page on the browser, (usually) nothing is going on. But when you hit a button in the Web page, things move. The way you set that up is that you tell the button '... onClick="doThis();" ...', or generalized: From your code, I cannot read what the application tries to get done. From what I though get is that the servo has to move, then wait for some time, and then move again... Your implementation of waiting until it is time is understandable. It though suffocates Espruino. Matter of fact it kills the whole system: the (error) message you get are its lasts words: "Your wasting of my cycles 'to wait' make me so busy that I cannot even do my home / house work, such as tending to Wifi, for what you like me so much!" :} The way out of this waiting business is to use one of these (time) event driven JavaScript language constructs:
Both use timers internally that take care of the deferring / postponing the business and let you - and your processing resource of the hook - to do something else until then. This is also the reason that the control returns immediately and is not stuck in waiting until time has passed. When set time will come, your business is taken care of without you having to do something. I intentionally use deferring or postponing instead of waiting, because waiting implies sitting idle or twiddling your fingers until the time of your appointment comes to pass... and that's not what you do and more so cannot afford in real life. Your requirement
gives me two options:
For option 1, lets take your alarm clock and your favorite music you want to be woken up by... so no matter when you go to bed, you set your alarm clock... of course you have now a phone that does that job. When you set the alarm and save it, it will give you feedback about how much time will pass until the alarm will ring... that's exactly what my (Android) phone does, and that's also what will implement here: Assume you have to get up at 6:30am... lets also assume you go to lala land What we will do is to take the time now, calculate the time difference to 6:30am tomorrow, and tell Espruino to set the timer - timeout - for that time and upon timeout to turn the music on.To not get bogged down about how we would do the music thing, we just turn the red LED1 on. To get there, we need to know how many milliseconds (ms or MS) it is from now to tomorrow 6:30am Here comes the code... (
And be assured that when power is not breaking down, your favorite music - sorry: just the red LED1 - will turn on 6:40am in the morning... There is a bonus to catch: your boss - friend? - calls you few minutes after you set this all up and tells you: lets hit the gym for 30 minutes before work or school or what ever you planned to do, you can use the
For option 2, you want to lite up the green LED2 for 5 seconds every minute and move the stepper motor 60 steps ahead. The code could look like below. To make it a bit better regarding CS in general and coding in particular, we define a function for what has to happen every minute:
To stop this ongoing blinking and moving, you use the interval Id (or handle) you got from interval setting and use it to clear the interval:
Often, the code needs to know if a set timeout or interval is still going on in order to branch accordingly. For that reason the defining of the handle and clearing of it looks 'better' when done like this, because starts correctly initialized and also clears the variable from the then 'defunct' timer handle...
And if you deal w/ a setTimeout and the timed function is relate to one ant the same timeout handletimeoutId, first thing in the that function you clear the handle: timeoutId = undefined; Back to the way of coding events and times in Espruino: And before you even notice, your thinking has changed to the exciting and very powerful event driven way of doing things versus always running in a circle - down the check list, go back to the top, run down again, and again, and again, and so on and so fort - like in the Arduino main loop with constantly checking all kinds of flags... (that the modules have set on the actual events). So: Why not just attach the code to do to the actual event? There you go: Use Espruino! Driving directly by events is one of the reasons that things can run on a very small battery for a very very long time. The processor is active only when / while something is done... Related to these concepts you will encounter terms such as call back and Promise... but let's save that talk for later. I hope this helps you switch gears... enjoy, and come back and ask more questions, challenge the forum community, share your success to our joy, and pay forward...
Posted at 2019-02-07 by Robin Wed 2019.02.06
Deep breath, mantra tone ummmmmmmmm, another coffee, less coffee? It might be that the Javascript language is the culprit. I find that this resource is a good go-to when ideas are needed: I believe that Option #2 in post #2 is what you are after. Should that darn servo just not want to cooperate, please post that code block using the 'code' button slightly above the edit window. You won't be able to scare us with what you have so far so no need to stress about that. Converting from C to Javascript does take a knack to get the hang of, but once one gets going, it is rather easy. Posted at 2019-02-07 by @gfwilliams Just to add to this - the ESP8266 needs to be able to execute tasks every so often to handle things like WiFi. If you block it executing them, it cannot function. Because of this there's a 'watchdog timer' which resets the whole device if a task doesn't complete in time, and that's what you're seeing. @allObjects above has shown you how to do what you need - the lack of a Posted at 2019-02-07 by asez73 Hi, Those are not necesseraly the answer to your precise question. But If you have very short waits then they will be the beginning of an answer. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2019-02-07 by user97622
Hi guys,
I need your help because I need execute a sync delay similar to Arduino function built-in. Like this:
I tried with:
Contents of module_file.js:
I tried also with:
And the result when tried to call more that 3000 ms is a dump and reset with this log:
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x40100000, len 2408, room 16
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0
tail 8
chksum 0x84
load 0x3ffe8310, len 632, room 0
tail 8
chksum 0xd8
csum 0xd8
2nd boot version : 1.6
SPI Speed : 80MHz
SPI Mode : DIO
SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000
�â�ì��oä�r�ûo|ìd�ldl`��c²�ð��|�r��d�oà��oãl ���o�d��û�þLoading 4948 bytes from flash...
What's happened? I'm very stressed with this.
Thanks and XoXo,
Beta Was this translation helpful? Give feedback.
All reactions