1

I have made a 8*8*8 led cube using arduino. I have found two different codes from different developers having different animations in them. i wanted to combine them and get to run all the animations at once. Both the codes have some similar function names and some are different as well. I wanted to know if i can keep both these separate from each other yet run them one after another. Almost all parameters are different including setup() of both codes. Please help

1
  • Define "after". An Arduino program runs constantly until you tell it otherwise (by removing the power, pressing reset, uploading a new program, etc). There is no "after" to an Arduino program.
    – Majenko
    Feb 12 '17 at 9:54
1

You can achieve this fairly simply by using namespaces. Inside a namespace, symbols are relative to that space. So you could put all of your first sketch into the namespace sketch1, and all of the second sketch into the namespace sketch2 (assuming it all fits, of course).

Now you can call the first sketch's setup and then loop, by qualifying it with the namespace name as illustrated below.

// FIRST SKETCH

namespace sketch1
{

  // FUNCTION PROTOTYPES

  void func1(int a, int b);
  void func2();

  void setup()
  {
    Serial.begin (115200);
    Serial.println ("Inside sketch1 setup");
    func1 (5, 6);
  }

  void loop()
  {
    Serial.println ("Inside sketch1 loop");
    delay (1000);
    func2 ();
  }

  void func1(int a, int b)
  {
    // some function
  }

  void func2()
  {
    // some other function
  }

} // end of namespace sketch1

// SECOND SKETCH

namespace sketch2
{

  // DECLARE FUNCTIONS BEFORE CALLING THEM

  void func3(int a, int b)
  {
    // some function
  }

  void func4()
  {
    // some other function
  }

  void setup()
  {
    Serial.begin (115200);
    Serial.println ("Inside sketch2 setup");
    func3 (7, 8);
  }

  void loop()
  {
    Serial.println ("Inside sketch2 loop");
    delay (1000);
    func4 ();
  }

} // end of namespace sketch2

// MAIN LOOP AND SETUP

void setup()
{
  pinMode (10, INPUT_PULLUP);
  if (digitalRead (10) == HIGH)
  {
    sketch1::setup ();
    while (true)
      sketch1::loop ();
  }
  else
  {
    sketch2::setup ();
    while (true)
      sketch2::loop ();
  }
}   // end of setup

void loop()
{

} // end of loop

In my example I am testing a pin (10) to see if sketch1 or sketch2 should run.


yet run them one after another

Yes, well sketches normally keep running inside loop forever, so you would need a way of deciding when to exit the sketch and run the other one. My example shows how you can choose between one or the other based on a digital input.


I tried it but it is showing that the functions are not declared in this scope.

Important note re function prototypes

It looks like the IDE's automatic generation of function prototypes doesn't work inside namespaces. Thus you need to do either:

  • Make your own function prototypes (that is the function declaration without the actual function code) - see sketch 1 above; or

  • Declare a function before it is used (sketch 2 above)

If this is someone else's code you may find it easier to just add the function prototypes as required. When you get the error message, find the function it is referring to, copy the "declaration" part, eg.

void foo (int a, int b)

Add a semicolon, eg.

void foo (int a, int b);

Then put that before it is called. Make sure it is still inside the namespace.


can I call the setup if first namespace then some func. respective to that namespace then call the setup of second namespace and call func. from that namespace and keep all this in an infinite loop?

Well, you can do that, but you need to realize what each sketch is doing. For example if sketch1 turns some pins to output, but sketch2 assumes they are still inputs, then you need to undo that.

And what is it to wrapup a function and 'how' to do it.

You need to inspect the sketches and see what they have done, so you can "undo" it. For example:

void wrapup ()
  {
  // set all pins to inputs and LOW
  for (int i = 0; i < 20; i++)
    {
    pinMode (i, INPUT);
    digitalWrite (i, LOW);
    }
  // if Serial used
  Serial.flush ();  // flush output
  Seral.end ();    // stop serial hardware

  // and so on

  }   // end of wrapup
17
  • I tried it but it is showing that the functions are not declared in this scope. The program didn't compile 2nd Q: And will sketch1::loop (); will run the loop of sketch1 forever?. I want it to run once and next sketch should run. The 2 codes should run one after another indefinitely without any condition. how shall i achieve that?. Or can you please provide me your email so that i can send you the codes and you could combine them if you've got a bit of time. please it would be a great help as i am a newbie and will truly appreciate your work. Feb 12 '17 at 7:21
  • Nick's code compiles for me. The problem is with your changes. Try and fix them, and if you can't, create a minimal, complete, verifiable example and post a new question.
    – Mark Smith
    Feb 12 '17 at 9:42
  • Regarding making them one one after the other, I don't think there's a one-size-fits-all answer for that. Really you need to understand what the code is doing, and find a way to adjust it. That's the fun of Arduinos anyway.
    – Mark Smith
    Feb 12 '17 at 9:43
  • As I said in my answer, most Arduino sketches are designed to loop forever. So to make one proceed after the other you have to redesign that paradigm. And no, I'm not giving you my email address.
    – Nick Gammon
    Feb 12 '17 at 9:46
  • See amended answer about generating function prototypes.
    – Nick Gammon
    Feb 13 '17 at 21:28

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