generic pause/wait? #1287
Replies: 20 comments
-
Posted at 2014-11-16 by @allObjects There is no such thing as a generic wait function. If you have done something and you want to wait for example for a minute before doing something else or resuming, you use the setTimeout(function,timeout) function. This has though some impact in how you break up your code into function. I assume you want to do something like that - in pseudo code:
The myfunction() construct has to be broken up into two pieces(*):
In case you have first to do something A(), then do something else and repeat it every minute until a condition is met, and then - finally - something B(), the construction looks like this:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-17 by @gfwilliams There's setTimeout, and there is also digitalPulse which can be used to send carefully timed pulses. An actual delay function isn't implemented because it encourages you to write code that causes problems for Espruino. As Espruino doesn't have preemptive multitasking, other tasks cannot execute until the current task has finished. If you make your current task take a long time to execute then it will probably cause problems for other tasks - if serial data or pin changes can't be processed in a sensible time period, the input buffers might overflow and data will be lost. You can still delay your code quite easily ( If you tell us what you want to do, we can maybe suggest a way of doing it? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by d0773d What I am attempting to do is write a function to handle the "i2c read request" for all of the sensor modules; I would also like to repeat the "all the sensor readings" every 5 seconds or possibly longer than 5 seconds. Depending on what command I send to the module indicates how long I should wait in milliseconds. For example if I send the "R" command, I am required to wait 1second or 1000milliseconds to invoke the I2C1.readFrom function. Most of the other commands that I send only require that I wait a total of 300milliseconds before I invoke the I2C1.readFrom function. This just insures that I give the sensor modules enough time to calculate the results before I readFrom the sensor module. I am not sure If i should write multiple functions for the different sensors(ph, ec, temp) or just write one function to read from them all? So far I started to write a function before I asked this question to read from them all, but only started with the ph sensor as I would like to get something working and then branch off from there. Code:
I remed out the geteci2cData(eci2c) function because I wasn't sure if that would execute before the getphi2cData(phi2c) finished processing the data. However, I think I should be ok because as Gordon mentioned, "other tasks cannot execute until the current task has finished."? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by DrAzzy setTimeout() is what you want - that way, you can have it doing other things during that 1 second, like reading other sensors, updating an LCD, etc. Your code is structured right, I see two issues keeping it from working: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by d0773d
And that explaines why I keep getting the error message that d is undefined. Thank you, for pointing that out! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by @allObjects I'd keep a table of profiles of all sensors. A profile includes all the sensor specifics, including what the callback(s)/callback chain is(are) for processing of the read data. At the end of the processing of an individual event, I would calculate next setTimeout() based on the running time and the reading schedule. With calculated timeout time closing in on 0 - and setBusyIndicator(LED#) #=1..3 in the code - you can figure out how busy you can define your schedule and whether your system 'is getting late' or you have still cycles in reserve. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by d0773d @allObjects So far I have:
Is that what you are refering too? I am new to Javascript objects and etc... For example, in order for me to get the sensor result I would invoke getSensorResult and grab the values from the cooresponding Sensor object and setTimeout to whatever the wait value is of the specific cmd? Also, to take a Reading in i2c I am required to pass the temperature along with the R to get a temperature compensated result. I have the default command as R,19.5. How would I update the cmd to the newely taken temperature value? I tried to change the value and I receive the following error:
I fixed the error
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-18 by @allObjects Following the thread of thought I read from your code is that after each command you have to wait a certain time until you can issue the next command or continue with the calculation. For example: A) for a calibrate-clear command you send "Cal,Clear" and then you have to wait 300 [ms] before you can continue. Is that correct? If so, what is the command that has to follow this calibrate-clear command? ...or what is the process/calculation/function that would follow this or any of these commands? May be you describe me in simple use case(s) what the system will do. To be of more concrete help, I read up on all your posts to get an overall understanding. What I understand so far is that you have the following system and process: The system[Sys] reads[read] temperatures[temp] with temperature sensors[Tempsensor], ph values[ph] with ph sensors, etc. The readings[tmp,ph,etc.] are then somehow displayed, transmitted, or made available otherwise for user or other system consummation. Reading and follow up processing are repeated based on some kind of schedule. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-19 by d0773d @allObjects I appologize for not explaining myself. For example I execute:
That tells the ph sensor to calculate the ph at the temp 25.3C. The ph sensor will take 1 second to calculate the ph, therefor, I will be required to wait 1 second before executing:
So depending on the specific command that i invoke will be require to wait what ever the wait value is before I readFrom the sensor. I hope I did a better job at explaining... When you said:
Yes, I am gathering all the sensor data and will eventually send all the calculations to my Intel Edison using UART communication; also out put all the values to 4x 4digit 7-segment displays. Maybe a better example? :
That last example doesn't exactly work. I receive the error:
How do I call getSensorReading() function from Sensor.prototype? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-19 by d0773d I'm still open for ideas and suggestions about my code structure. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-19 by @gfwilliams The structure looks pretty tidy - although I'd really recommend doing:
Instead of your You've also defined getters like In your code
Then you can use it with:
Hope that helps! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-19 by d0773d Hi @gfwilliams The reason why I chose |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-20 by @allObjects @d0773d, np - it explains all well in your world... and over time I may know enough about it to come up with some abstraction of the real world into a model expressed in algorithms and data structures.
This - taken from a previous post - means, that temperature and ph sensors are paired. For each pair, you read a temperature first, then you ask the ph sensor to calculate the temperature compensated ph value, and then you read it, then you display it and/or send it to some place. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2014-11-20 by @allObjects @d0773d, when you say:
you add a function property to the prototype object of Sensor. When you say:
your replace the existing prototype object with a new one (and loose When you do not want to repeat the assignment 'explicitly', you can use the following code:
This is mixing one object's properties into another one. Btw, this 'mixing' shows you how you can dynamically access value and function properties of an object. Assume you have an object with two function properties (methods) f1 and f2.
The following three lines invoke all f2 method of obj object, of which the last one makes is dynamically by the fx variable:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-26 by d0773d @gfwilliams Its been 4 months since I did anything with my project. My computer crashed and I lost all of my code referring to this post. I copied and pasted my code from this post and tried to implement your suggestions again. I am trying to pass in the value of this.address and execute the functions instead of returning the function it self. However, as you explained, I am returning the function itself rather than executing it and getting the result. For example, in
returns:
Code from this thread and memory:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-26 by @gfwilliams As far as I can tell, don't you just want to do:
instead of:
That will execute the function on |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-26 by d0773d @gfwilliams I forgot the () :/ The code now works |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-02-07 by user97622 ...Removed by @gfwilliams as double-posted at http://forum.espruino.com/conversations/330506/ |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-02-07 by Robin Note: Post #19 above was started as a new thread and has a few responses there Home Other Boards ESP8266 Sync delay like Arduino delay function built-in |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-02-07 by @gfwilliams Yes - please don't double-post, and if you have ESP8266-related questions please post them on the ESP8266 forum. But to reiterate, not having a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2014-11-16 by d0773d
Is there a generic wait function i.e. wait(milliseconds)? Or is there only: function setInterval(function, timeout)
And
function setTimeout(function, timeout)
Beta Was this translation helpful? Give feedback.
All reactions