0

I am an engineering student completely new at using Arduino so my following question might be silly but still. I do not think you'll need to get the entire code so I am only going to expose the idea. So basically, I would like a customer to choose a feature between three of them (let's name them 1, 2, 3). Depending on the feature he chose, the corresponding setup() and the corresponding loop() have to run. For example, if he chooses the second feature, in the main setup(), the setup2() has to run and in the main loop(), the loop2() has to run only. The other setup() functions and loop() functions have to do nothing. I first thought I could do this using switch cases but I am looking for another way to implement that as a teacher told me this method would take too much time.

void setup() {

      setup1(); //only runs if 1st feature chosen
      setup2();
      setup3();
}




void loop() {

      loop1(); //only runs if 1st feature chosen
      loop2();
      loop3();
}

And also, I wrote the three setup() and the three loop() in different files (of course, setup1() is in the same file as a loop1(), etc., ...). Is it possible to include them in the main file (for instance, like <#include nameoffile.h)?

  • What physical method are you using to determine which feature has been chosen? – Majenko Aug 14 '18 at 15:34
  • @Majenko I'll use a web-based configurator for the customer to chose. Its name is MQTT – alexmerchant Aug 14 '18 at 15:43
  • So the device will subscribe to an MQTT topic and receive a retained message to get the configuration? I would say you want to implement a Finite State Machine, since the whole "configuration" system is itself another configuration in its own right. – Majenko Aug 14 '18 at 16:24
  • 2
    I don’t understand what your teacher is talking about. Seems to me that a switch-case would work just fine. – Gerben Aug 14 '18 at 17:02
  • @Majenko yes that is the idea! I have never heard of a Finite State Machine but I am going to make some research about it. – alexmerchant Aug 14 '18 at 17:34
1

You probably want to do something like this:

typedef enum { FEATURE1, FEATURE2, FEATURE3 } Feature;

//implement these
Feature getFeatureFromUser(){}
void setup1(){}
void setup2(){}
void setup3(){}
void loop1(){}
void loop2(){}
void loop3(){}

void setup() {
  Feature f = getFeatureFromUser();

  switch (f) {
    case FEATURE1:
      setup1();
      while (true) {
        loop1();
        if (serialEventRun) serialEventRun();
      }
    case FEATURE2:
      setup2();
      while (true) {
        loop2();
        if (serialEventRun) serialEventRun();
      }
    case FEATURE3:
      setup3();
      while (true) {
        loop3();
        if (serialEventRun) serialEventRun();
      }
  }
}

//never reached
void loop() {}

It's good to know that there's no hard rules about how the Arduino functions should be used. Examples like this demonstrate how Arduino's standard requirements of void setup() and void loop() don't quite match every use case.

EDIT: The serialEventRun lines are normally called behind the scenes after each execution of Arduino's normal loop(), so adding them here will result in the standard behavior of serial events

  • yes that is the idea! thank you very much for helping, this is really helpful! but what do you mean by the void loop() is never reached, why is that? arduino seems much clearer this way – alexmerchant Aug 14 '18 at 20:38
  • After one of the options in the switch statement matches, and the corresponding setup function is run, the loop# function will be called forever inside a while loop. Therefore it will stay in that loop forever and never reach Arduino's loop() function. The blocks in the switch statement are very similar to how Arduino's setup() and loop() work behind the scenes. – sealj553 Aug 15 '18 at 1:42
  • ok that's clear now! thks I'll try this way then – alexmerchant Aug 15 '18 at 8:34
  • The downside of that method is that it breaks serial event handling. If you don't use serial events, then that's not an issue. – Majenko Aug 15 '18 at 10:19
  • @Majenko i thought about it too as I may have other operations to do. Indeed, after the first Setup(), three others Options setup11(), setup12() and Setup13() with their own Loops should run, depending again on the customer's choice (choice could be 11, 12,13) don't know if that is still clear – alexmerchant Aug 15 '18 at 11:15
0

If the feature selection is not at runtime but is at compile time you could do it like this:

#define FEATURE1 true   // Feature 1 is enabled

void setup() {
    #if FEATURE1
    setup1(); //only runs if 1st feature chosen
    #endif
    setup2();
    setup3();
}

void loop() {
    #if FEATURE1
    loop1(); //only runs if 1st feature chosen
    #endif
    loop2();
    loop3();
}

void setup1(void) {}
void setup2(void) {}
void setup3(void) {}

void loop1(void) {}
void loop2(void) {}
void loop3(void) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.