Comments (9)
If you've ever used unity I would like to be able to use the coroutines like unity uses them
from aceroutine.
Hello,
Let's see if I understand your question.
The COROUTINE_DELAY(50)
macro contains an internal "yield()" function. Each time Alarm.runCoroutine()
is called, the coroutine effectively resumes execution just after the "yield()". After the Alarm blinks 10 times, the coroutine reaches its isDone()
state, and all further calls to runCoroutine()
does nothing.
If you want to restart the coroutine to blink 10 times again, you can use the Coroutine::reset()
function that I added just recently in v1.1. Luckily for you, the reset()
method will cause your Alarm1
static variable to reset to 0 (because it causes the runCoroutine()
to start from the top beginning of the method, causing the for
loop to re-init the Alarm1
variable to 0.) In more complicated Coroutines, you may need to use a Manual Coroutine (https://github.com/bxparks/AceRoutine#manual-coroutines-recommended) and provide your own myReset()
method that resets your internal state manually (and then calls Coroutine::reset()
).
If you upgrade to v1.1, note that it now has a dependency to my AceCommon (https://github.com/bxparks/AceCommon) library which you can install through the Arduino Library Manager. I noted this in the CHANGELOG.md, but I notice that I didn't make that clear in the README.md. I'll update that shortly.
Does this help?
(Sorry, I don't have a discord account..)
from aceroutine.
First off. Thanks for the lighting quick reply. Secondly I'll try your solution in a bit. Currently I just have a parallel booliean called AlarmR that gets true in the very beginning of the coroutine and gets false at the end of the corutine. And in the main loop there is
if(alarmM) alarm.runcoroutine();
And it's working for now. But if I run into further problems I'll switch to the coroutine manager. Because currently I'm not using the coroutine manager. Also I'm converting the coroutines to loop coroutines and removing the coroutine_end
from aceroutine.
from aceroutine.
bool AlarmR = false;
COROUTINE(Alarm){
COROUTINE_LOOP() {
AlarmR = true;
static int AlarmI1 = 0;
for (AlarmI1 = 0; AlarmI1 < 10; AlarmI1++)
{
digitalWrite(A5, 1);
digitalWrite(A4, 1);
COROUTINE_DELAY(50);
digitalWrite(A4, 0);
COROUTINE_DELAY(50);
}
AlarmR = false;
COROUTINE_YIELD();
}
}
void Loop(){
if(AlarmR){
Alarm.runCoroutine();
}
}
void StartAlarm(){
Alarm.runCoroutine();
}
from aceroutine.
and there's not going to be one or two loops there going to be like 10 or so. and i need to have one of the if statements for each coroutine
from aceroutine.
That code looks great. In fact, that was the only way to do what you want to do prior to my adding the Coroutine::reset()
method.
Here's a slightly different way to accomplish the same thing, which you might find cleaner (the indentation is probably off, hard to get that right using this web edit box):
#include <AceRoutine.h>
class Alarm: public ace_routine::Coroutine {
public:
Alarm() {}
int runCoroutine() override {
COROUTINE_LOOP() {
if (enable) {
for (counter = 0; counter < 10; counter++) {
digitalWrite(A5, 1);
digitalWrite(A4, 1);
COROUTINE_DELAY(50);
digitalWrite(A4, 0);
COROUTINE_DELAY(50);
} // for
enable = false;
} // if
COROUTINE_YIELD();
} // COROUTINE_LOOP()
}
void setEnable(bool state) { enable = state; }
private:
bool enable = false;
int counter;
};
// Create instance of the Alarm coroutine
Alarm alarm;
void loop(){
alarm.runCoroutine();
}
void StartAlarm(){
alarm.setEnable(true);
}
The nice thing about defining your custom Alarm
class is that you can create multiple instances of it, with slightly different variations if you want. The internal counter
and enable
variables of each alarm don't interfere with each other.
from aceroutine.
Can I close this? I don't think there is anything for me to do. The only additional comment I can make is that if you use the latest version of AceRoutine, you can use Coroutine::reset()
in the StartAlarm()
function, and remove all the code around the Alarm::enable
flag. The code becomes simpler.
from aceroutine.
Sure I'm fine with closing it. Everything makes sense.
from aceroutine.
Related Issues (17)
- delayStartMillis should be a uint32_t instead of uint16_t HOT 2
- Resetting a coroutine HOT 10
- Function equivalent to PT_RESTART? HOT 2
- how to re use coroutine 【fixed】 HOT 1
- Proposal: remove the 2-argument COROUTINE(class, name) macro HOT 1
- Stack overflow from CoroutineScheduler::list() after suspend() -> resume() HOT 5
- `reset()` causing coroutine to break somehow HOT 3
- COROUTINE_DELAY_SECONDS & COROUTINE_DELAY_MICROS not working HOT 5
- Setup() lifecycle method in Manual Routine method. HOT 6
- Implementing manual (Class) based coroutines with .h and .cpp files HOT 7
- for loops inside COROUTINE don't seem to work HOT 2
- Examples don't set LED pinMode HOT 5
- More a suggestion or offer ... of incorporating my local changes of 1.4.3 into 1.5.1 HOT 2
- HelloCoroutine on Platformio on Digispark (Attiny85) fails compile HOT 1
- CoRoutine will stop work when you make type conversion in it... HOT 6
- Microsecond Timing? HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aceroutine.