How to split one sketch into few Tabs and how to keep the relation between them?

Thank you.

You still haven't said whether you tried my suggestion. It illustrates exactly what @johnwasser has explained

1 Like

Thank you.
I tried you code, and print out 7. I still didn't make clear of my question by it.
I guess the problem is I can't and didn't describe my question clearly here, what i worry is if I put few .ino files together:

  1. can they running together, can each file has their own setupand loop?

  2. or just run one 'main' file only which has setup and loop ? if so, how to make the relationship between them?
    Best

My example illustrated what you can do. As you will have seen there is only one setup() and loop() function even though there are 2 tabs

The IDE puts all of the .ino files together and then compiles them. You cannot have more than one setup() or loop() function in a sketch, nor are they needed

No. Only one setup() and one loop() in all files combined. You can use a full sketch in a tab but you will need to rename setup() and loop() and call them from your main sketch.

For example, if you want the 'BlinkWithoutDelay' sketch to be a tab in your sketch, name setup() and loop() something like 'BlinkWithoutDelay_setup()' and 'BlinkWithoutDelay_loop()'

In the setup() in your main sketch, put:
BlinkWithoutDelay_setup();
and in the loop() of your main sketch, put:
` BlinkWithoutDelay_loop();'

1 Like

Thank you.
Actually I did exactly what you said ref. to:

But always errors came out. it's why I keep asking for some new solutions.
Like this one no get yet:

You just have to figure out what the compiler errors are telling you about and fix the problems. For example if you have two sketches that use the same name for a global variable or function you will get a redefinition error.

This use of a sketch in a tab compiles and runs without error or warning. When I added the "Dimmer.ino" tab I got a redefinition error for 'ledPin' because both BlinkWithoutDelay.ino and Dimmer.ino use that name. I changed the name in Dimmer.ino to fix the error.

Main.ino:

void setup()
{
  BlinkWithoutDelay_setup();
  Dimmer_setup();
}

void loop()
{
  BlinkWithoutDelay_loop();
  Dimmer_loop();
}

BlinkWithoutDelay.ino tab:


// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void BlinkWithoutDelay_setup()
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void BlinkWithoutDelay_loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Dimmer.ino tab:

const int Dimmer_ledPin = 9;      // the pin that the LED is attached to

void Dimmer_setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(Dimmer_ledPin, OUTPUT);
}

void Dimmer_loop()
{
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available())
  {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(Dimmer_ledPin, brightness);
  }
}
1 Like

Thank you for the wonderful example.
I made the sub tabs (tabs without setup ) as xxxx.h and asclass xxxx followed the link above or set them by #pragma once at head followed another link.
I guess many errors came from these set.
I think it's OK now just put them together and change the setups and loops.

Did you really name them as .h files ?

1 Like

Thanks.
I did name the sub Tabs xxxx.h and also added class xxxx as the picture shown.

What changes did you make yo your MAIN sketch ?

Please post it here

1 Like

Thanks.
Sorry of the code got many changes and hard to find the information when I made post.

What I was getting at is that if you move part of your MAIN .ino file into a .h file on a tab then the MAIN file will not know that it exists and compilation will fail unless to tell the MAIN file that the .h file is there

If, however, you move part of your MAIN sketch into a .ino file then that is all you have to do

1 Like

Sorry bother you again.
if I have few .ino s all used the same device say LCD, the error redefinition of 'Adafruit_SSD1306 display' came when compiling, how to do with this?
Thanks

Thanks.
Yes, I did the split code followed one link who did that way, move some part from MAIN to one .h, and some to another .h. and make all .h as classes. but I failed.

Just as you would declare a global variable only once in your MAIN file then create the instance named display only once and do it in your MAIN file

Remember, the code in all of the .ino code tabs is combined into one .ino file before that is compiled

1 Like

Thank you so much.
Have a great day!