Giter Club home page Giter Club logo

Comments (9)

BiffBish avatar BiffBish commented on August 31, 2024

If you've ever used unity I would like to be able to use the coroutines like unity uses them

from aceroutine.

bxparks avatar bxparks commented on August 31, 2024

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.

BiffBish avatar BiffBish commented on August 31, 2024

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.

bxparks avatar bxparks commented on August 31, 2024

from aceroutine.

BiffBish avatar BiffBish commented on August 31, 2024
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.

BiffBish avatar BiffBish commented on August 31, 2024

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.

bxparks avatar bxparks commented on August 31, 2024

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.

bxparks avatar bxparks commented on August 31, 2024

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.

BiffBish avatar BiffBish commented on August 31, 2024

Sure I'm fine with closing it. Everything makes sense.

from aceroutine.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.